Tapkee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
logging.hpp
Go to the documentation of this file.
1 /* This software is distributed under BSD 3-clause license (see LICENSE file).
2  *
3  * Copyright (c) 2012-2013 Sergey Lisitsyn
4  */
5 
6 #ifndef TAPKEE_LOGGING_H_
7 #define TAPKEE_LOGGING_H_
8 
9 #include <iostream>
10 #include <string>
11 
12 #define LEVEL_ENABLED_FIELD(X) bool X##_enabled
13 #define LEVEL_ENABLED_FIELD_INITIALIZER(X,value) X##_enabled(value)
14 #define LEVEL_HANDLERS(LEVEL) \
15  void enable_##LEVEL() { LEVEL##_enabled = true; }; \
16  void disable_##LEVEL() { LEVEL##_enabled = false; }; \
17  bool is_##LEVEL##_enabled() { return LEVEL##_enabled; };\
18  void message_##LEVEL(const std::string& msg) \
19  { \
20  if (LEVEL##_enabled) \
21  impl->message_##LEVEL(msg); \
22  }
23 #define LEVEL_HANDLERS_DECLARATION(LEVEL) \
24  virtual void message_##LEVEL(const std::string& msg) = 0
25 #define LEVEL_HANDLERS_DEFAULT_IMPL(LEVEL) \
26  virtual void message_##LEVEL(const std::string& msg) \
27  { \
28  if (os_ && os_->good()) \
29  (*os_) << "["#LEVEL"] " << msg << "\n"; \
30  }
31 
32 namespace tapkee
33 {
34 
37 {
38 public:
40  virtual ~LoggerImplementation() {};
45  LEVEL_HANDLERS_DECLARATION(benchmark);
46 private:
49 };
50 
53 {
54 public:
55  DefaultLoggerImplementation() : os_(&std::cout) {}
62 private:
64  DefaultLoggerImplementation(const DefaultLoggerImplementation&);
65 
66  std::ostream* os_;
67 };
68 
73 {
74  private:
75  LoggingSingleton() : impl(new DefaultLoggerImplementation),
77  LEVEL_ENABLED_FIELD_INITIALIZER(warning,true),
80  LEVEL_ENABLED_FIELD_INITIALIZER(benchmark,false)
81  {
82  };
84  {
85  delete impl;
86  }
87  LoggingSingleton(const LoggingSingleton& ls);
88  void operator=(const LoggingSingleton& ls);
89 
91 
96  LEVEL_ENABLED_FIELD(benchmark);
97 
98  public:
100  static LoggingSingleton& instance()
101  {
102  static LoggingSingleton s;
103  return s;
104  }
105 
108  LoggerImplementation* get_logger_impl() const { return impl; }
111  void set_logger_impl(LoggerImplementation* i) { delete impl; impl = i; }
112 
114  LEVEL_HANDLERS(warning);
117  LEVEL_HANDLERS(benchmark);
118 
119 };
120 
121 #undef LEVEL_HANDLERS
122 #undef LEVEL_HANDLERS_DECLARATION
123 #undef LEVEL_HANDLERS_DEFAULT_IMPL
124 #undef LEVEL_ENABLED_FIELD
125 #undef LEVEL_ENABLED_FIELD_INITIALIZER
126 }
127 
128 #endif
#define LEVEL_ENABLED_FIELD_INITIALIZER(X, value)
Definition: logging.hpp:13
DefaultLoggerImplementation & operator=(const DefaultLoggerImplementation &)
LoggerImplementation * get_logger_impl() const
getter for logger implementation
Definition: logging.hpp:108
Default std::cout implementation of LoggerImplementation.
Definition: logging.hpp:52
void set_logger_impl(LoggerImplementation *i)
setter for logger implementation
Definition: logging.hpp:111
#define LEVEL_ENABLED_FIELD(X)
Definition: logging.hpp:12
#define LEVEL_HANDLERS(LEVEL)
Definition: logging.hpp:14
A base class for logger required by the library.
Definition: logging.hpp:36
Main logging singleton used by the library. Can use provided LoggerImplementation if necessary...
Definition: logging.hpp:72
#define LEVEL_HANDLERS_DECLARATION(LEVEL)
Definition: logging.hpp:23
LoggerImplementation & operator=(const LoggerImplementation &)
static LoggingSingleton & instance()
Definition: logging.hpp:100
#define LEVEL_HANDLERS_DEFAULT_IMPL(LEVEL)
Definition: logging.hpp:25
LoggerImplementation * impl
Definition: logging.hpp:90