Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <iostream>
- #include <string>
- #include <utility>
- namespace cppx {
- using
- std::function,
- std::string,
- std::ostream, std::clog, std::endl,
- std::move;
- class Movable
- {
- private:
- Movable( const Movable& ) = delete;
- auto operator=( const Movable ) -> Movable& = delete;
- public:
- Movable() noexcept = default;
- Movable( Movable&& ) noexcept = default;
- auto operator=( Movable&& ) noexcept -> Movable& = default;
- };
- class Logger
- {
- using Func = function<void(ostream&)>;
- enum Direction{ entering, leaving };
- string m_funcname;
- Func m_custom_output;
- static auto nesting_level()
- -> int&
- {
- static int the_level = 0;
- return the_level;
- }
- static auto indent( const int level )
- -> string
- { return string( 4*level, ' ' ); }
- void log( const Direction dir, const int level ) const
- {
- clog << (dir == entering? "-> " : "<- ") << indent( level ) << m_funcname;
- if( m_custom_output ) {
- clog << " (";
- m_custom_output( clog );
- clog << ")";
- }
- clog << endl;
- }
- public:
- ~Logger()
- {
- log( leaving, --nesting_level() );
- }
- Logger( string funcname, Func custom_output = {} ):
- m_funcname( move( funcname ) ),
- m_custom_output( move( custom_output ) )
- {
- log( entering, nesting_level()++ );
- }
- };
- } // namespace cppx
- namespace app {
- using std::ostream, std::cout, std::endl;
- using cppx::Logger;
- auto gcd( const int a, const int b )
- -> int
- {
- Logger logger( __func__, [&]( ostream& stream )
- {
- stream << "a = " << a << ", b = " << b;
- } );
- if( b == 0 ) {
- return a;
- } else {
- return gcd( b, a% b );
- }
- }
- void run()
- {
- const int r = gcd( 42, 12 );
- cout << "gcd( 42, 12 ) = " << r << endl;
- }
- } // namespace app
- auto main() -> int { app::run(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement