Advertisement
spacechase0

Logger

Nov 7th, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #ifndef GE_LOGGER_H
  2. #define GE_LOGGER_H
  3.  
  4. #include <algorithm>
  5. #include <string>
  6. #include <fstream>
  7. #include <iostream>
  8.  
  9. namespace ge
  10. {
  11.     class Logger
  12.     {
  13.         public:
  14.             Logger() = delete;
  15.             Logger( const Logger& logger ) = delete;
  16.             Logger( const std::string& filename, bool theStandardOutput = true );
  17.             ~Logger();
  18.            
  19.             void Log( const std::string& str );
  20.            
  21.             #define stream(val) \
  22.                 file << ( val ); \
  23.                 if ( standardOutput ) \
  24.                 { \
  25.                     std::cout << ( val ); \
  26.                 }
  27.            
  28.             // http://en.wikipedia.org/wiki/Variadic_templates
  29.             template< typename T, typename... Args >
  30.             void Log( const std::string& str, T value, Args... args )
  31.             {
  32.                 for ( auto it = str.begin(); it != str.end(); ++it )
  33.                 {
  34.                     size_t dist = std::distance( str.begin(), it );
  35.                    
  36.                     char cc = ( * it );
  37.                     if ( dist >= str.size() - 1 )
  38.                     {
  39.                         stream( cc );
  40.                         break;
  41.                     }
  42.                    
  43.                     char nc = ( * ( it + 1 ) );
  44.                     if ( cc == '%' and nc != '%' )
  45.                     {
  46.                         stream( value );
  47.                         Log( str.substr( dist + 1 ), args... );
  48.                         return;
  49.                     }
  50.                    
  51.                     stream( cc );
  52.                 }
  53.             }
  54.            
  55.             #undef stream
  56.        
  57.         private:
  58.             std::fstream file;
  59.             bool standardOutput;
  60.     };
  61. }
  62.  
  63. #endif // GE_LOGGER_H
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement