logger_impl.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 #include "opc/spdlog/logger.h"
10 
11 #include <memory>
12 #include <string>
13 
14 
15 // create logger with given name, sinks and the default pattern formatter
16 // all other ctors will call this one
17 template<class It>
18 inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end):
19  _name(logger_name),
20  _sinks(begin, end),
21  _formatter(std::make_shared<pattern_formatter>("%+")),
22  _level(level::info),
23  _flush_level(level::off),
24  _last_err_time(0),
25  _msg_counter(1) // message counter will start from 1. 0-message id will be reserved for controll messages
26 {
27  _err_handler = [this](const std::string &msg)
28  {
29  this->_default_err_handler(msg);
30  };
31 }
32 
33 // ctor with sinks as init list
34 inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list):
35  logger(logger_name, sinks_list.begin(), sinks_list.end())
36 {}
37 
38 
39 // ctor with single sink
40 inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink):
41  logger(logger_name,
42 {
43  single_sink
44 })
45 {}
46 
47 
48 inline spdlog::logger::~logger() = default;
49 
50 
52 {
53  _set_formatter(msg_formatter);
54 }
55 
56 inline void spdlog::logger::set_pattern(const std::string& pattern, pattern_time_type pattern_time)
57 {
58  _set_pattern(pattern, pattern_time);
59 }
60 
61 
62 template <typename... Args>
63 inline void spdlog::logger::log(level::level_enum lvl, const char* fmt, const Args&... args)
64 {
65  if (!should_log(lvl)) return;
66 
67  try
68  {
69  details::log_msg log_msg(&_name, lvl);
70  log_msg.raw.write(fmt, args...);
71  _sink_it(log_msg);
72  }
73  catch (const std::exception &ex)
74  {
75  _err_handler(ex.what());
76  }
77  catch (...)
78  {
79  _err_handler("Unknown exception");
80  }
81 }
82 
83 template <typename... Args>
84 inline void spdlog::logger::log(level::level_enum lvl, const char* msg)
85 {
86  if (!should_log(lvl)) return;
87  try
88  {
89  details::log_msg log_msg(&_name, lvl);
90  log_msg.raw << msg;
91  _sink_it(log_msg);
92  }
93  catch (const std::exception &ex)
94  {
95  _err_handler(ex.what());
96  }
97  catch (...)
98  {
99  _err_handler("Unknown exception");
100  }
101 
102 }
103 
104 template<typename T>
105 inline void spdlog::logger::log(level::level_enum lvl, const T& msg)
106 {
107  if (!should_log(lvl)) return;
108  try
109  {
110  details::log_msg log_msg(&_name, lvl);
111  log_msg.raw << msg;
112  _sink_it(log_msg);
113  }
114  catch (const std::exception &ex)
115  {
116  _err_handler(ex.what());
117  }
118  catch (...)
119  {
120  _err_handler("Unknown exception");
121  }
122 }
123 
124 
125 template <typename Arg1, typename... Args>
126 inline void spdlog::logger::trace(const char* fmt, const Arg1 &arg1, const Args&... args)
127 {
128  log(level::trace, fmt, arg1, args...);
129 }
130 
131 template <typename Arg1, typename... Args>
132 inline void spdlog::logger::debug(const char* fmt, const Arg1 &arg1, const Args&... args)
133 {
134  log(level::debug, fmt, arg1, args...);
135 }
136 
137 template <typename Arg1, typename... Args>
138 inline void spdlog::logger::info(const char* fmt, const Arg1 &arg1, const Args&... args)
139 {
140  log(level::info, fmt, arg1, args...);
141 }
142 
143 template <typename Arg1, typename... Args>
144 inline void spdlog::logger::warn(const char* fmt, const Arg1 &arg1, const Args&... args)
145 {
146  log(level::warn, fmt, arg1, args...);
147 }
148 
149 template <typename Arg1, typename... Args>
150 inline void spdlog::logger::error(const char* fmt, const Arg1 &arg1, const Args&... args)
151 {
152  log(level::err, fmt, arg1, args...);
153 }
154 
155 template <typename Arg1, typename... Args>
156 inline void spdlog::logger::critical(const char* fmt, const Arg1 &arg1, const Args&... args)
157 {
158  log(level::critical, fmt, arg1, args...);
159 }
160 
161 template <typename... Args>
162 inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const char* msg)
163 {
164  if (flag)
165  {
166  log(lvl, msg);
167  }
168 }
169 
170 template<typename T>
171 inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const T& msg)
172 {
173  if (flag)
174  {
175  log(lvl, msg);
176  }
177 }
178 
179 template <typename Arg1, typename... Args>
180 inline void spdlog::logger::trace_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
181 {
182  if (flag)
183  {
184  log(level::trace, fmt, arg1, args...);
185  }
186 }
187 
188 template <typename Arg1, typename... Args>
189 inline void spdlog::logger::debug_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
190 {
191  if (flag)
192  {
193  log(level::debug, fmt, arg1, args...);
194  }
195 }
196 
197 template <typename Arg1, typename... Args>
198 inline void spdlog::logger::info_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
199 {
200  if (flag)
201  {
202  log(level::info, fmt, arg1, args...);
203  }
204 }
205 
206 template <typename Arg1, typename... Args>
207 inline void spdlog::logger::warn_if(const bool flag, const char* fmt, const Arg1& arg1, const Args&... args)
208 {
209  if (flag)
210  {
211  log(level::warn, fmt, arg1, args...);
212  }
213 }
214 
215 template <typename Arg1, typename... Args>
216 inline void spdlog::logger::error_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
217 {
218  if (flag)
219  {
220  log(level::err, fmt, arg1, args...);
221  }
222 }
223 
224 template <typename Arg1, typename... Args>
225 inline void spdlog::logger::critical_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
226 {
227  if (flag)
228  {
229  log(level::critical, fmt, arg1, args...);
230  }
231 }
232 
233 
234 template<typename T>
235 inline void spdlog::logger::trace(const T& msg)
236 {
237  log(level::trace, msg);
238 }
239 
240 template<typename T>
241 inline void spdlog::logger::debug(const T& msg)
242 {
243  log(level::debug, msg);
244 }
245 
246 
247 template<typename T>
248 inline void spdlog::logger::info(const T& msg)
249 {
250  log(level::info, msg);
251 }
252 
253 
254 template<typename T>
255 inline void spdlog::logger::warn(const T& msg)
256 {
257  log(level::warn, msg);
258 }
259 
260 template<typename T>
261 inline void spdlog::logger::error(const T& msg)
262 {
263  log(level::err, msg);
264 }
265 
266 template<typename T>
267 inline void spdlog::logger::critical(const T& msg)
268 {
269  log(level::critical, msg);
270 }
271 
272 template<typename T>
273 inline void spdlog::logger::trace_if(const bool flag, const T& msg)
274 {
275  if (flag)
276  {
277  log(level::trace, msg);
278  }
279 }
280 
281 template<typename T>
282 inline void spdlog::logger::debug_if(const bool flag, const T& msg)
283 {
284  if (flag)
285  {
286  log(level::debug, msg);
287  }
288 }
289 
290 template<typename T>
291 inline void spdlog::logger::info_if(const bool flag, const T& msg)
292 {
293  if (flag)
294  {
295  log(level::info, msg);
296  }
297 }
298 
299 template<typename T>
300 inline void spdlog::logger::warn_if(const bool flag, const T& msg)
301 {
302  if (flag)
303  {
304  log(level::warn, msg);
305  }
306 }
307 
308 template<typename T>
309 inline void spdlog::logger::error_if(const bool flag, const T& msg)
310 {
311  if (flag)
312  {
313  log(level::err, msg);
314  }
315 }
316 
317 template<typename T>
318 inline void spdlog::logger::critical_if(const bool flag, const T& msg)
319 {
320  if (flag)
321  {
322  log(level::critical, msg);
323  }
324 }
325 
326 
327 #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
328 #include <codecvt>
329 
330 template <typename... Args>
331 inline void spdlog::logger::log(level::level_enum lvl, const wchar_t* msg)
332 {
333  std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
334 
335  log(lvl, conv.to_bytes(msg));
336 }
337 
338 template <typename... Args>
339 inline void spdlog::logger::log(level::level_enum lvl, const wchar_t* fmt, const Args&... args)
340 {
341  fmt::WMemoryWriter wWriter;
342 
343  wWriter.write(fmt, args...);
344  log(lvl, wWriter.c_str());
345 }
346 
347 template <typename... Args>
348 inline void spdlog::logger::trace(const wchar_t* fmt, const Args&... args)
349 {
350  log(level::trace, fmt, args...);
351 }
352 
353 template <typename... Args>
354 inline void spdlog::logger::debug(const wchar_t* fmt, const Args&... args)
355 {
356  log(level::debug, fmt, args...);
357 }
358 
359 template <typename... Args>
360 inline void spdlog::logger::info(const wchar_t* fmt, const Args&... args)
361 {
362  log(level::info, fmt, args...);
363 }
364 
365 
366 template <typename... Args>
367 inline void spdlog::logger::warn(const wchar_t* fmt, const Args&... args)
368 {
369  log(level::warn, fmt, args...);
370 }
371 
372 template <typename... Args>
373 inline void spdlog::logger::error(const wchar_t* fmt, const Args&... args)
374 {
375  log(level::err, fmt, args...);
376 }
377 
378 template <typename... Args>
379 inline void spdlog::logger::critical(const wchar_t* fmt, const Args&... args)
380 {
381  log(level::critical, fmt, args...);
382 }
383 
384 //
385 // conditional logging
386 //
387 
388 template <typename... Args>
389 inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const wchar_t* msg)
390 {
391  if (flag)
392  {
393  log(lvl, msg);
394  }
395 }
396 
397 template <typename... Args>
398 inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const wchar_t* fmt, const Args&... args)
399 {
400  if (flag)
401  {
402  log(lvl, fmt, args);
403  }
404 }
405 
406 template <typename... Args>
407 inline void spdlog::logger::trace_if(const bool flag, const wchar_t* fmt, const Args&... args)
408 {
409  if (flag)
410  {
411  log(level::trace, fmt, args...);
412  }
413 }
414 
415 template <typename... Args>
416 inline void spdlog::logger::debug_if(const bool flag, const wchar_t* fmt, const Args&... args)
417 {
418  if (flag)
419  {
420  log(level::debug, fmt, args...);
421  }
422 }
423 
424 template <typename... Args>
425 inline void spdlog::logger::info_if(const bool flag, const wchar_t* fmt, const Args&... args)
426 {
427  if (flag)
428  {
429  log(level::info, fmt, args...);
430  }
431 }
432 
433 
434 template <typename... Args>
435 inline void spdlog::logger::warn_if(const bool flag, const wchar_t* fmt, const Args&... args)
436 {
437  if (flag)
438  {
439  log(level::warn, fmt, args...);
440  }
441 }
442 
443 template <typename... Args>
444 inline void spdlog::logger::error_if(const bool flag, const wchar_t* fmt, const Args&... args)
445 {
446  if (flag)
447  {
448  log(level::err, fmt, args...);
449  }
450 }
451 
452 template <typename... Args>
453 inline void spdlog::logger::critical_if(const bool flag, const wchar_t* fmt, const Args&... args)
454 {
455  if (flag)
456  {
457  log(level::critical, fmt, args...);
458  }
459 }
460 
461 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
462 
463 
464 
465 //
466 // name and level
467 //
468 inline const std::string& spdlog::logger::name() const
469 {
470  return _name;
471 }
472 
474 {
475  _level.store(log_level);
476 }
477 
479 {
480  _err_handler = err_handler;
481 }
482 
484 {
485  return _err_handler;
486 }
487 
488 
490 {
491  _flush_level.store(log_level);
492 }
493 
495 {
496  return static_cast<spdlog::level::level_enum>(_level.load(std::memory_order_relaxed));
497 }
498 
500 {
501  return msg_level >= _level.load(std::memory_order_relaxed);
502 }
503 
504 //
505 // protected virtual called at end of each user log call (if enabled) by the line_logger
506 //
508 {
509 #if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
510  msg.msg_id = _msg_counter.fetch_add(1, std::memory_order_relaxed);
511 #endif
512  _formatter->format(msg);
513  for (auto &sink : _sinks)
514  {
515  if( sink->should_log( msg.level))
516  {
517  sink->log(msg);
518  }
519  }
520 
521  if(_should_flush_on(msg))
522  flush();
523 }
524 
525 inline void spdlog::logger::_set_pattern(const std::string& pattern, pattern_time_type pattern_time)
526 {
527  _formatter = std::make_shared<pattern_formatter>(pattern, pattern_time);
528 }
530 {
531  _formatter = msg_formatter;
532 }
533 
535 {
536  for (auto& sink : _sinks)
537  sink->flush();
538 }
539 
541 {
542  auto now = time(nullptr);
543  if (now - _last_err_time < 60)
544  return;
545  auto tm_time = details::os::localtime(now);
546  char date_buf[100];
547  std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
548  details::log_msg err_msg;
549  err_msg.formatted.write("[*** LOG ERROR ***] [{}] [{}] [{}]{}", name(), msg, date_buf, details::os::eol);
550  sinks::stderr_sink_mt::instance()->log(err_msg);
552 }
553 
555 {
556  const auto flush_level = _flush_level.load(std::memory_order_relaxed);
557  return (msg.level >= flush_level) && (msg.level != level::off);
558 }
559 
560 inline const std::vector<spdlog::sink_ptr>& spdlog::logger::sinks() const
561 {
562  return _sinks;
563 }
void error_if(const bool flag, const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:216
void error(const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:150
time
Definition: server.py:52
virtual log_err_handler error_handler()
Definition: logger_impl.h:483
void set_formatter(formatter_ptr)
Definition: logger_impl.h:51
void log_if(const bool flag, level::level_enum lvl, const char *fmt, const Args &...args)
void log(level::level_enum lvl, const char *fmt, const Args &...args)
Definition: logger_impl.h:63
void write(BasicCStringRef< Char > format, ArgList args)
Definition: format.h:2918
std::shared_ptr< spdlog::formatter > formatter_ptr
void warn(const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:144
std::shared_ptr< sinks::sink > sink_ptr
virtual void flush()
Definition: logger_impl.h:534
void warn_if(const bool flag, const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:207
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
const std::vector< sink_ptr > & sinks() const
Definition: logger_impl.h:560
spdlog::log_clock::time_point now()
Definition: os.h:64
void set_level(level::level_enum)
Definition: logger_impl.h:473
void trace_if(const bool flag, const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:180
spdlog::level_t _flush_level
level::level_enum level
Definition: log_msg.h:42
void set_pattern(const std::string &, pattern_time_type=pattern_time_type::local)
Definition: logger_impl.h:56
void trace(const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:126
bool should_log(level::level_enum) const
Definition: logger_impl.h:499
std::atomic< size_t > _msg_counter
std::tm localtime(const std::time_t &time_tt)
Definition: os.h:80
std::vector< sink_ptr > _sinks
void info_if(const bool flag, const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:198
virtual ~logger()
void debug_if(const bool flag, const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:189
fmt::MemoryWriter formatted
Definition: log_msg.h:46
void critical_if(const bool flag, const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:225
const std::string _name
spdlog::level_t _level
static SPDLOG_CONSTEXPR const char * eol
Definition: os.h:143
void info(const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:138
void critical(const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:156
virtual void set_error_handler(log_err_handler)
Definition: logger_impl.h:478
std::atomic< time_t > _last_err_time
fmt::MemoryWriter raw
Definition: log_msg.h:45
level::level_enum level() const
Definition: logger_impl.h:494
formatter_ptr _formatter
std::initializer_list< sink_ptr > sinks_init_list
virtual void _default_err_handler(const std::string &msg)
Definition: logger_impl.h:540
Definition: format.cc:81
virtual void _set_formatter(formatter_ptr)
Definition: logger_impl.h:529
virtual void _sink_it(details::log_msg &)
Definition: logger_impl.h:507
const std::string & name() const
Definition: logger_impl.h:468
const Char * c_str() const
Definition: format.h:2875
void debug(const char *fmt, const Arg1 &, const Args &...args)
Definition: logger_impl.h:132
bool _should_flush_on(const details::log_msg &)
Definition: logger_impl.h:554
std::function< void(const std::string &err_msg)> log_err_handler
log_err_handler _err_handler
virtual void _set_pattern(const std::string &, pattern_time_type)
Definition: logger_impl.h:525
logger(const std::string &logger_name, sink_ptr single_sink)
Definition: logger_impl.h:40
void flush_on(level::level_enum log_level)
Definition: logger_impl.h:489


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:06