AIToolbox
A library that offers tools for AI problem solving.
Logging.hpp
Go to the documentation of this file.
1 
61 #ifndef AI_TOOLBOX_LOGGING_HEADER_FILE
62 #define AI_TOOLBOX_LOGGING_HEADER_FILE
63 
64 #ifndef AI_LOGGING_ENABLED
65 #define AI_LOGGING_ENABLED 0
66 #endif
67 
68 #define AI_SEVERITY_DEBUG 0
69 #define AI_SEVERITY_INFO 1
70 #define AI_SEVERITY_WARNING 2
71 #define AI_SEVERITY_ERROR 3
72 
73 #if AI_LOGGING_ENABLED == 1
74 
75 #include <sstream>
76 
77 namespace AIToolbox {
78  // ##################################################################
79  //
80  // THESE HERE ARE THE ONLY THINGS YOU NEED TO CARE ABOUT IN YOUR OWN
81  // PROGRAMS IN THIS ENTIRE FILE. THE REST ARE IMPLEMENTATION DETAILS!
82  //
83  using AILoggerFun = void(int, const char *);
89  inline AILoggerFun * AILogger = nullptr;
90  //
91  //
92  // ##################################################################
93 
94  namespace Impl {
95  // We use this to dump logs in.
96  inline char logBuffer[500] = {0};
97  }
98 }
99 
100 #else
101 // Fake logger to keep static checks
102 namespace AIToolbox::Impl {
103  struct FakeLogger {
104  FakeLogger(int) {}
105  template <typename T>
106  FakeLogger & operator<<(const T&) { return *this; }
107  };
108 }
109 
110 #endif
111 
112 #if AI_LOGGING_ENABLED == 1
113 // Actual logging if logging is enabled.
114 #define AI_LOGGER(SEV, ARGS) \
115  do { \
116  if (AILogger) { \
117  std::stringstream internal_stringstream_; \
118  internal_stringstream_.rdbuf()->pubsetbuf( \
119  AIToolbox::Impl::logBuffer, \
120  sizeof(AIToolbox::Impl::logBuffer) - 1 \
121  ); \
122  internal_stringstream_ << ARGS << '\0'; \
123  AILogger(SEV, AIToolbox::Impl::logBuffer); \
124  } \
125  } while(0)
126 
127 #else
128 // Statement to enable static checks on inputs if logging is disabled.
129 #define AI_LOGGER(SEV, ARGS) \
130  while (0) { \
131  AIToolbox::Impl::FakeLogger(SEV) << ARGS; \
132  }
133 #endif
134 
135 #endif
AIToolbox::Impl::logBuffer
char logBuffer[500]
Definition: Logging.hpp:96
AIToolbox::operator<<
std::ostream & operator<<(std::ostream &os, const Statistics &rh)
This function writes the output of the Statistics to the stream.
AIToolbox::AILogger
AILoggerFun * AILogger
This pointer defines the function used to log.
Definition: Logging.hpp:89
AIToolbox::AILoggerFun
void(int, const char *) AILoggerFun
Definition: Logging.hpp:83
AIToolbox
Definition: Experience.hpp:6
AIToolbox::Impl
Definition: FunctionMatching.hpp:8