Tapkee
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>
12 
13 #define LEVEL_ENABLED_FIELD(X) bool X##_enabled
14 #define LEVEL_ENABLED_FIELD_INITIALIZER(X,value) X##_enabled(value)
15 #define LEVEL_HANDLERS(LEVEL) \
16  void enable_##LEVEL() { LEVEL##_enabled = true; }; \
17  void disable_##LEVEL() { LEVEL##_enabled = false; }; \
18  bool is_##LEVEL##_enabled() { return LEVEL##_enabled; };\
19  void message_##LEVEL(const std::string& msg) \
20  { \
21  if (LEVEL##_enabled) \
22  impl->message_##LEVEL(msg); \
23  }
24 #define LEVEL_HANDLERS_DECLARATION(LEVEL) \
25  virtual void message_##LEVEL(const std::string& msg) = 0
26 #define LEVEL_HANDLERS_DEFAULT_IMPL(STREAM, LEVEL) \
27  virtual void message_##LEVEL(const std::string& msg) \
28  { \
29  if (STREAM && STREAM->good()) \
30  (*STREAM) << "["#LEVEL"] " << msg << "\n"; \
31  }
32 
33 namespace tapkee
34 {
35 
38 {
39 public:
41  virtual ~LoggerImplementation() {};
46  LEVEL_HANDLERS_DECLARATION(benchmark);
47 private:
50 };
51 
54 {
55 public:
56  DefaultLoggerImplementation() : sout(&std::cout), serr(&std::cerr) {}
63 private:
66 
67  std::ostream* sout;
68  std::ostream* serr;
69 };
70 
75 {
76  private:
79  LEVEL_ENABLED_FIELD_INITIALIZER(warning,true),
82  LEVEL_ENABLED_FIELD_INITIALIZER(benchmark,false)
83  {
84  };
86  {
87  delete impl;
88  }
90  void operator=(const LoggingSingleton& ls);
91 
93 
98  LEVEL_ENABLED_FIELD(benchmark);
99 
100  public:
103  {
104  static LoggingSingleton s;
105  return s;
106  }
107 
113  void set_logger_impl(LoggerImplementation* i) { delete impl; impl = i; }
114 
116  LEVEL_HANDLERS(warning);
119  LEVEL_HANDLERS(benchmark);
120 
121 };
122 
123 #undef LEVEL_HANDLERS
124 #undef LEVEL_HANDLERS_DECLARATION
125 #undef LEVEL_HANDLERS_DEFAULT_IMPL
126 #undef LEVEL_ENABLED_FIELD
127 #undef LEVEL_ENABLED_FIELD_INITIALIZER
128 }
129 
130 #endif
#define LEVEL_ENABLED_FIELD_INITIALIZER(X, value)
Definition: logging.hpp:14
DefaultLoggerImplementation & operator=(const DefaultLoggerImplementation &)
STL namespace.
Default std::cout implementation of LoggerImplementation.
Definition: logging.hpp:53
void set_logger_impl(LoggerImplementation *i)
setter for logger implementation
Definition: logging.hpp:113
#define LEVEL_ENABLED_FIELD(X)
Definition: logging.hpp:13
#define LEVEL_HANDLERS(LEVEL)
Definition: logging.hpp:15
A base class for logger required by the library.
Definition: logging.hpp:37
Main logging singleton used by the library. Can use provided LoggerImplementation if necessary...
Definition: logging.hpp:74
#define LEVEL_HANDLERS_DEFAULT_IMPL(STREAM, LEVEL)
Definition: logging.hpp:26
#define LEVEL_HANDLERS_DECLARATION(LEVEL)
Definition: logging.hpp:24
LoggerImplementation & operator=(const LoggerImplementation &)
void operator=(const LoggingSingleton &ls)
static LoggingSingleton & instance()
Definition: logging.hpp:102
LoggerImplementation * impl
Definition: logging.hpp:92
LoggerImplementation * get_logger_impl() const
getter for logger implementation
Definition: logging.hpp:110