Advertisement
Guest User

logging of calls and returns - fixed

a guest
Feb 26th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <functional>
  2. #include <iostream>
  3. #include <string>
  4. #include <utility>
  5.  
  6. namespace cppx {
  7.     using
  8.         std::function,
  9.         std::string,
  10.         std::ostream, std::clog, std::endl,
  11.         std::move;
  12.  
  13.     class Movable
  14.     {
  15.     private:
  16.         Movable( const Movable& )                           = delete;
  17.         auto operator=( const Movable ) -> Movable&         = delete;
  18.     public:
  19.         Movable() noexcept                                  = default;
  20.         Movable( Movable&& ) noexcept                       = default;
  21.         auto operator=( Movable&& ) noexcept -> Movable&    = default;
  22.     };
  23.  
  24.     class Logger
  25.     {
  26.         using Func = function<void(ostream&)>;
  27.         enum Direction{ entering, leaving };
  28.  
  29.         string      m_funcname;
  30.         Func        m_custom_output;
  31.        
  32.         static auto nesting_level()
  33.             -> int&
  34.         {
  35.             static int the_level = 0;
  36.             return the_level;
  37.         }
  38.  
  39.         static auto indent( const int level )
  40.             -> string
  41.         { return string( 4*level, ' ' ); }
  42.  
  43.         void log( const Direction dir, const int level ) const
  44.         {
  45.             clog << (dir == entering? "-> " : "<- ") << indent( level ) << m_funcname;
  46.             if( m_custom_output ) {
  47.                 clog << " (";
  48.                 m_custom_output( clog );
  49.                 clog << ")";
  50.             }
  51.             clog << endl;
  52.         }
  53.  
  54.     public:
  55.         ~Logger()
  56.         {
  57.             log( leaving, --nesting_level() );
  58.         }
  59.  
  60.         Logger( string funcname, Func custom_output = {} ):
  61.             m_funcname( move( funcname ) ),
  62.             m_custom_output( move( custom_output ) )
  63.         {
  64.             log( entering, nesting_level()++ );
  65.         }
  66.     };
  67.  
  68. }  // namespace cppx
  69.  
  70. namespace app {
  71.     using std::ostream, std::cout, std::endl;
  72.     using cppx::Logger;
  73.  
  74.     auto gcd( const int a, const int b )
  75.         -> int
  76.     {
  77.         Logger logger( __func__, [&]( ostream& stream )
  78.         {
  79.             stream << "a = " << a << ", b = " << b;
  80.         } );
  81.        
  82.         if( b == 0 ) {
  83.             return a;
  84.         } else {
  85.             return gcd( b, a% b );
  86.         }
  87.     }
  88.    
  89.     void run()
  90.     {
  91.         const int r = gcd( 42, 12 );
  92.         cout << "gcd( 42, 12 ) = " << r << endl;
  93.     }
  94. }  // namespace app
  95.  
  96. auto main() -> int { app::run(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement