Tapkee
logging.hpp
Go to the documentation of this file.
00001 /* This software is distributed under BSD 3-clause license (see LICENSE file).
00002  *
00003  * Copyright (c) 2012-2013 Sergey Lisitsyn
00004  */
00005 
00006 #ifndef TAPKEE_LOGGING_H_
00007 #define TAPKEE_LOGGING_H_
00008 
00009 #include <iostream>
00010 #include <string>
00011 
00012 #define LEVEL_ENABLED_FIELD(X) bool X##_enabled
00013 #define LEVEL_ENABLED_FIELD_INITIALIZER(X,value) X##_enabled(value)
00014 #define LEVEL_HANDLERS(LEVEL) \
00015         void enable_##LEVEL() { LEVEL##_enabled = true; };      \
00016         void disable_##LEVEL() { LEVEL##_enabled = false; };    \
00017         bool is_##LEVEL##_enabled() { return LEVEL##_enabled; };\
00018         void message_##LEVEL(const std::string& msg)            \
00019         {                                                       \
00020             if (LEVEL##_enabled)                                \
00021                 impl->message_##LEVEL(msg);                     \
00022         }
00023 #define LEVEL_HANDLERS_DECLARATION(LEVEL) \
00024         virtual void message_##LEVEL(const std::string& msg) = 0
00025 #define LEVEL_HANDLERS_DEFAULT_IMPL(LEVEL) \
00026         virtual void message_##LEVEL(const std::string& msg)    \
00027         {                                                       \
00028             if (os_ && os_->good())                             \
00029                 (*os_) << "["#LEVEL"] " << msg << "\n";         \
00030         }
00031 
00032 namespace tapkee
00033 {
00034 
00036 class LoggerImplementation
00037 {
00038 public:
00039     LoggerImplementation() {};
00040     virtual ~LoggerImplementation() {};
00041     LEVEL_HANDLERS_DECLARATION(info);
00042     LEVEL_HANDLERS_DECLARATION(warning);
00043     LEVEL_HANDLERS_DECLARATION(debug);
00044     LEVEL_HANDLERS_DECLARATION(error);
00045     LEVEL_HANDLERS_DECLARATION(benchmark);
00046 private:
00047     LoggerImplementation& operator=(const LoggerImplementation&);
00048     LoggerImplementation(const LoggerImplementation&);
00049 };
00050 
00052 class DefaultLoggerImplementation : public LoggerImplementation
00053 {
00054 public:
00055     DefaultLoggerImplementation() : os_(&std::cout) {}
00056     virtual ~DefaultLoggerImplementation() {}
00057     LEVEL_HANDLERS_DEFAULT_IMPL(info);
00058     LEVEL_HANDLERS_DEFAULT_IMPL(warning);
00059     LEVEL_HANDLERS_DEFAULT_IMPL(debug);
00060     LEVEL_HANDLERS_DEFAULT_IMPL(error);
00061     LEVEL_HANDLERS_DEFAULT_IMPL(benchmark)
00062 private:
00063     DefaultLoggerImplementation& operator=(const DefaultLoggerImplementation&);
00064     DefaultLoggerImplementation(const DefaultLoggerImplementation&);
00065 
00066     std::ostream* os_;
00067 };
00068 
00072 class LoggingSingleton
00073 {
00074     private:
00075         LoggingSingleton() : impl(new DefaultLoggerImplementation),
00076             LEVEL_ENABLED_FIELD_INITIALIZER(info,false),
00077             LEVEL_ENABLED_FIELD_INITIALIZER(warning,true),
00078             LEVEL_ENABLED_FIELD_INITIALIZER(debug,false),
00079             LEVEL_ENABLED_FIELD_INITIALIZER(error,true),
00080             LEVEL_ENABLED_FIELD_INITIALIZER(benchmark,false)
00081         {
00082         };
00083         ~LoggingSingleton()
00084         {
00085             delete impl;
00086         }
00087         LoggingSingleton(const LoggingSingleton& ls);
00088         void operator=(const LoggingSingleton& ls);
00089 
00090         LoggerImplementation* impl;
00091 
00092         LEVEL_ENABLED_FIELD(info);
00093         LEVEL_ENABLED_FIELD(warning);
00094         LEVEL_ENABLED_FIELD(debug);
00095         LEVEL_ENABLED_FIELD(error);
00096         LEVEL_ENABLED_FIELD(benchmark);
00097 
00098     public:
00100         static LoggingSingleton& instance()
00101         {
00102             static LoggingSingleton s;
00103             return s;
00104         }
00105 
00108         LoggerImplementation* get_logger_impl() const { return impl; }
00111         void set_logger_impl(LoggerImplementation* i) { delete impl; impl = i; }
00112 
00113         LEVEL_HANDLERS(info);
00114         LEVEL_HANDLERS(warning);
00115         LEVEL_HANDLERS(debug);
00116         LEVEL_HANDLERS(error);
00117         LEVEL_HANDLERS(benchmark);
00118 
00119 };
00120 
00121 #undef LEVEL_HANDLERS
00122 #undef LEVEL_HANDLERS_DECLARATION
00123 #undef LEVEL_HANDLERS_DEFAULT_IMPL
00124 #undef LEVEL_ENABLED_FIELD
00125 #undef LEVEL_ENABLED_FIELD_INITIALIZER
00126 }
00127 
00128 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines