spdlog/logger.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 // Thread safe logger (except for set_pattern(..), set_formatter(..) and set_error_handler())
9 // Has name, log level, vector of std::shared sink pointers and formatter
10 // Upon each log write the logger:
11 // 1. Checks if its log level is enough to log the message
12 // 2. Format the message using the formatter function
13 // 3. Pass the formatted message to its sinks to performa the actual logging
14 
16 #include "opc/spdlog/common.h"
17 
18 #include <vector>
19 #include <memory>
20 #include <string>
21 
22 namespace spdlog
23 {
24 
25 class logger
26 {
27 public:
28  logger(const std::string& logger_name, sink_ptr single_sink);
30  template<class It>
31  logger(const std::string& name, const It& begin, const It& end);
32 
33  virtual ~logger();
34  logger(const logger&) = delete;
35  logger& operator=(const logger&) = delete;
36 
37 
38  template <typename... Args> void log(level::level_enum lvl, const char* fmt, const Args&... args);
39  template <typename... Args> void log(level::level_enum lvl, const char* msg);
40  template <typename Arg1, typename... Args> void trace(const char* fmt, const Arg1&, const Args&... args);
41  template <typename Arg1, typename... Args> void debug(const char* fmt, const Arg1&, const Args&... args);
42  template <typename Arg1, typename... Args> void info(const char* fmt, const Arg1&, const Args&... args);
43  template <typename Arg1, typename... Args> void warn(const char* fmt, const Arg1&, const Args&... args);
44  template <typename Arg1, typename... Args> void error(const char* fmt, const Arg1&, const Args&... args);
45  template <typename Arg1, typename... Args> void critical(const char* fmt, const Arg1&, const Args&... args);
46 
47  template <typename... Args> void log_if(const bool flag, level::level_enum lvl, const char* fmt, const Args&... args);
48  template <typename... Args> void log_if(const bool flag, level::level_enum lvl, const char* msg);
49  template <typename Arg1, typename... Args> void trace_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
50  template <typename Arg1, typename... Args> void debug_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
51  template <typename Arg1, typename... Args> void info_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
52  template <typename Arg1, typename... Args> void warn_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
53  template <typename Arg1, typename... Args> void error_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
54  template <typename Arg1, typename... Args> void critical_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
55 
56 #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
57  template <typename... Args> void log(level::level_enum lvl, const wchar_t* msg);
58  template <typename... Args> void log(level::level_enum lvl, const wchar_t* fmt, const Args&... args);
59  template <typename... Args> void trace(const wchar_t* fmt, const Args&... args);
60  template <typename... Args> void debug(const wchar_t* fmt, const Args&... args);
61  template <typename... Args> void info(const wchar_t* fmt, const Args&... args);
62  template <typename... Args> void warn(const wchar_t* fmt, const Args&... args);
63  template <typename... Args> void error(const wchar_t* fmt, const Args&... args);
64  template <typename... Args> void critical(const wchar_t* fmt, const Args&... args);
65 
66  template <typename... Args> void log_if(const bool flag, level::level_enum lvl, const wchar_t* msg);
67  template <typename... Args> void log_if(const bool flag, level::level_enum lvl, const wchar_t* fmt, const Args&... args);
68  template <typename... Args> void trace_if(const bool flag, const wchar_t* fmt, const Args&... args);
69  template <typename... Args> void debug_if(const bool flag, const wchar_t* fmt, const Args&... args);
70  template <typename... Args> void info_if(const bool flag, const wchar_t* fmt, const Args&... args);
71  template <typename... Args> void warn_if(const bool flag, const wchar_t* fmt, const Args&... args);
72  template <typename... Args> void error_if(const bool flag, const wchar_t* fmt, const Args&... args);
73  template <typename... Args> void critical_if(const bool flag, const wchar_t* fmt, const Args&... args);
74 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
75 
76  template <typename T> void log(level::level_enum lvl, const T&);
77  template <typename T> void trace(const T&);
78  template <typename T> void debug(const T&);
79  template <typename T> void info(const T&);
80  template <typename T> void warn(const T&);
81  template <typename T> void error(const T&);
82  template <typename T> void critical(const T&);
83 
84  template <typename T> void log_if(const bool flag, level::level_enum lvl, const T&);
85  template <typename T> void trace_if(const bool flag, const T&);
86  template <typename T> void debug_if(const bool flag, const T&);
87  template <typename T> void info_if(const bool flag, const T&);
88  template <typename T> void warn_if(const bool flag, const T&);
89  template <typename T> void error_if(const bool flag, const T&);
90  template <typename T> void critical_if(const bool flag, const T&);
91 
92  bool should_log(level::level_enum) const;
94  level::level_enum level() const;
95  const std::string& name() const;
98 
99  // automatically call flush() if message level >= log_level
100  void flush_on(level::level_enum log_level);
101 
102  virtual void flush();
103 
104  const std::vector<sink_ptr>& sinks() const;
105 
106  // error handler
107  virtual void set_error_handler(log_err_handler);
108  virtual log_err_handler error_handler();
109 
110 protected:
111  virtual void _sink_it(details::log_msg&);
112  virtual void _set_pattern(const std::string&, pattern_time_type);
113  virtual void _set_formatter(formatter_ptr);
114 
115  // default error handler: print the error to stderr with the max rate of 1 message/minute
116  virtual void _default_err_handler(const std::string &msg);
117 
118  // return true if the given message level should trigger a flush
119  bool _should_flush_on(const details::log_msg&);
120 
122  std::vector<sink_ptr> _sinks;
127  std::atomic<time_t> _last_err_time;
128  std::atomic<size_t> _msg_counter;
129 };
130 }
131 
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
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)
logger & operator=(const logger &)=delete
void log(level::level_enum lvl, const char *fmt, const Args &...args)
Definition: logger_impl.h:63
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
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
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::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
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
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
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
std::atomic< int > level_t
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
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