Advertisement
Guest User

logging of calls and returns

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