MADARA  3.4.1
Logger.h
Go to the documentation of this file.
1 #ifndef _MADARA_LOGGER_LOGGER_H_
2 #define _MADARA_LOGGER_LOGGER_H_
3 
4 #include "madara/MadaraExport.h"
5 #include "madara/LockType.h"
6 #include <vector>
7 #include <atomic>
8 #include <string>
9 #include <stdio.h>
11 
20 #define madara_logger_log(loggering, level, ...) \
21  if (madara::logger::Logger::get_thread_level() >= 0) \
22  { \
23  if (level <= madara::logger::Logger::get_thread_level()) \
24  { \
25  loggering.log(level, __VA_ARGS__); \
26  } \
27  } \
28  else if (level <= loggering.get_level()) \
29  { \
30  loggering.log(level, __VA_ARGS__); \
31  }
32 
41 #define madara_logger_ptr_log(loggering, level, ...) \
42  if (madara::logger::Logger::get_thread_level() >= 0) \
43  { \
44  if (level <= madara::logger::Logger::get_thread_level()) \
45  { \
46  loggering->log(level, __VA_ARGS__); \
47  } \
48  } \
49  else if (level <= loggering->get_level()) \
50  { \
51  loggering->log(level, __VA_ARGS__); \
52  }
53 
62 #define madara_logger_checked_ptr_log(loggering, level, ...) \
63  if (loggering && madara::logger::Logger::get_thread_level() >= 0) \
64  { \
65  if (level <= madara::logger::Logger::get_thread_level()) \
66  { \
67  loggering->log(level, __VA_ARGS__); \
68  } \
69  } \
70  else if (loggering && level <= loggering->get_level()) \
71  { \
72  loggering->log(level, __VA_ARGS__); \
73  }
74 
83 #define madara_logger_cond_log_ptrs( \
84  conditional, logger_ptr, alt_logger_ptr, level, ...) \
85  if (conditional && \
86  (madara::logger::Logger::get_thread_level() >= 0)) \
87  { \
88  if (level <= madara::logger::Logger::get_thread_level()) \
89  { \
90  logger_ptr->log(level, __VA_ARGS__); \
91  } \
92  else \
93  { \
94  alt_logger_ptr->log(level, __VA_ARGS__); \
95  } \
96  } \
97  else if (conditional && level <= logger_ptr->get_level()) \
98  { \
99  logger_ptr->log(level, __VA_ARGS__); \
100  } \
101  else \
102  { \
103  alt_logger_ptr->log(level, __VA_ARGS__); \
104  }
105 
114 #define madara_logger_cond_log( \
115  conditional, loggering, alt_logger_ptr, level, ...) \
116  if (conditional && (madara::logger::Logger::get_thread_level() >= 0)) \
117  { \
118  if (level <= madara::logger::Logger::get_thread_level()) \
119  { \
120  loggering.log(level, __VA_ARGS__); \
121  } \
122  else \
123  { \
124  alt_logger_ptr->log(level, __VA_ARGS__); \
125  } \
126  } \
127  else if (conditional && level <= loggering.get_level()) \
128  { \
129  loggering.log(level, __VA_ARGS__); \
130  } \
131  else \
132  { \
133  alt_logger_ptr->log(level, __VA_ARGS__); \
134  }
135 
136 namespace madara
137 {
138 namespace logger
139 {
144 {
153  LOG_MADARA_MAX = 6
154 };
155 
157 const double TLS_THREAD_HZ_DEFAULT = 0.0;
158 
164 class MADARA_EXPORT Logger
165 {
166 public:
171  Logger(bool log_to_stderr = true);
172 
176  ~Logger();
177 
183  void log(int level, const char* message, ...);
184 
189  void add_file(const std::string& filename);
190 
194  void add_term(void);
195 
199  void add_syslog(void);
200 
205  void set_level(int level);
206 
211  std::string get_tag(void);
212 
217  void set_tag(const std::string& tag);
218 
223  int get_level(void);
224 
228  void clear(void);
229 
237  void set_timestamp_format(const std::string& format = "%x %X: ");
238 
243  static int get_thread_level(void);
244 
245 #if 0
250  static std::string get_thread_name(void);
251 
256  static double get_thread_hertz(void);
257 
262  static void set_thread_level(int level);
263 
268  static void set_thread_name(const std::string name);
269 
274  static void set_thread_hertz(double hertz);
275 #endif
276 
277 private:
278 #if 0
279  static thread_local int thread_level_;
280  static thread_local std::string thread_name_;
281  static thread_local double thread_hertz_;
282 #endif
283 
292  std::string search_and_insert_custom_tstamp(
293  const std::string& buf, const std::string& ts_str);
294 
302  std::string strip_custom_tstamp(
303  const std::string in_str, const std::string ts_str);
304 
306 
308  typedef std::vector<FILE*> FileVectors;
309 
311  mutable MADARA_LOCK_TYPE mutex_;
312 
315 
317  std::atomic<int> level_;
318 
321 
324 
327 
330 
332 
334  const char* MADARA_GET_TIME_MGT_ = "%MGT";
335 
337  const char* MADARA_THREAD_NAME_ = "%MTN";
338 
340  const char* MADARA_THREAD_HERTZ_ = "%MTZ";
341 };
342 
343 } // end logger namespace
344 } // end madara namespace
345 
346 #include "Logger.inl"
347 
348 #endif // _MADARA_LOGGER_LOGGER_H_
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:165
std::string tag_
the tag used for logging to system logs
Definition: Logger.h:326
std::vector< FILE * > FileVectors
guard for access and changes
Definition: Logger.h:308
MADARA_LOCK_TYPE mutex_
mutex for changes
Definition: Logger.h:311
std::string timestamp_format_
the timestamp format.
Definition: Logger.h:329
bool syslog_added_
tracks whether the system log has been added
Definition: Logger.h:323
bool term_added_
tracks whether terminal output has been added
Definition: Logger.h:320
std::atomic< int > level_
the maximum detail level for logging
Definition: Logger.h:317
FileVectors files_
list of all log outputs
Definition: Logger.h:314
constexpr string_t string
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:12
const int TLS_THREAD_LEVEL_DEFAULT
Definition: Logger.h:156
LogLevels
Logging levels available for MADARA library.
Definition: Logger.h:144
@ LOG_MADARA_MAX
Definition: Logger.h:153
const double TLS_THREAD_HZ_DEFAULT
Definition: Logger.h:157
Copyright(c) 2020 Galois.