logger.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2015-2108 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
9 // set_error_handler())
10 // Has name, log level, vector of std::shared sink pointers and formatter
11 // Upon each log write the logger:
12 // 1. Checks if its log level is enough to log the message and if yes:
13 // 2. Call the underlying sinks to do the job.
14 // 3. Each sink use its own private copy of a formatter to format the message
15 // and send to its destination.
16 //
17 // The use of private formatter per sink provides the opportunity to cache some
18 // formatted data,
19 // and support customize format per each sink.
20 
21 #include "spdlog/common.h"
22 #include "spdlog/formatter.h"
23 #include "spdlog/sinks/sink.h"
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 namespace spdlog {
30 
31 class logger
32 {
33 public:
34  logger(std::string name, sink_ptr single_sink);
36 
37  template<typename It>
38  logger(std::string name, It begin, It end);
39 
40  virtual ~logger();
41 
42  logger(const logger &) = delete;
43  logger &operator=(const logger &) = delete;
44 
45  template<typename... Args>
46  void log(level::level_enum lvl, const char *fmt, const Args &... args);
47 
48  template<typename... Args>
49  void log(source_loc loc, level::level_enum lvl, const char *fmt, const Args &... args);
50 
51  void log(level::level_enum lvl, const char *msg);
52 
53  void log(source_loc loc, level::level_enum lvl, const char *msg);
54 
55  template<typename... Args>
56  void trace(const char *fmt, const Args &... args);
57 
58  template<typename... Args>
59  void debug(const char *fmt, const Args &... args);
60 
61  template<typename... Args>
62  void info(const char *fmt, const Args &... args);
63 
64  template<typename... Args>
65  void warn(const char *fmt, const Args &... args);
66 
67  template<typename... Args>
68  void error(const char *fmt, const Args &... args);
69 
70  template<typename... Args>
71  void critical(const char *fmt, const Args &... args);
72 
73 #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
74 #ifndef _WIN32
75 #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
76 #else
77  template<typename... Args>
78  void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args);
79 
80  template<typename... Args>
81  void log(source_loc source, level::level_enum lvl, const wchar_t *fmt, const Args &... args);
82 
83  template<typename... Args>
84  void trace(const wchar_t *fmt, const Args &... args);
85 
86  template<typename... Args>
87  void debug(const wchar_t *fmt, const Args &... args);
88 
89  template<typename... Args>
90  void info(const wchar_t *fmt, const Args &... args);
91 
92  template<typename... Args>
93  void warn(const wchar_t *fmt, const Args &... args);
94 
95  template<typename... Args>
96  void error(const wchar_t *fmt, const Args &... args);
97 
98  template<typename... Args>
99  void critical(const wchar_t *fmt, const Args &... args);
100 #endif // _WIN32
101 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
102 
103  // T can be statically converted to string_view
105  void log(level::level_enum lvl, const T &);
106 
107  // T can be statically converted to string_view
109  void log(source_loc loc, level::level_enum lvl, const T &);
110 
111  // T cannot be statically converted to string_view
113  void log(level::level_enum lvl, const T &);
114 
115  // T cannot be statically converted to string_view
117  void log(source_loc loc, level::level_enum lvl, const T &);
118 
119  template<typename T>
120  void trace(const T &msg);
121 
122  template<typename T>
123  void debug(const T &msg);
124 
125  template<typename T>
126  void info(const T &msg);
127 
128  template<typename T>
129  void warn(const T &msg);
130 
131  template<typename T>
132  void error(const T &msg);
133 
134  template<typename T>
135  void critical(const T &msg);
136 
137  bool should_log(level::level_enum msg_level) const;
138  void set_level(level::level_enum log_level);
139 
141  level::level_enum level() const;
142  const std::string &name() const;
143 
144  // set formatting for the sinks in this logger.
145  // each sink will get a seperate instance of the formatter object.
146  void set_formatter(std::unique_ptr<formatter> formatter);
148 
149  // flush functions
150  void flush();
151  void flush_on(level::level_enum log_level);
153 
154  // sinks
155  const std::vector<sink_ptr> &sinks() const;
156  std::vector<sink_ptr> &sinks();
157 
158  // error handler
159  void set_error_handler(log_err_handler err_handler);
161 
162  // create new logger with same sinks and configuration.
163  virtual std::shared_ptr<logger> clone(std::string logger_name);
164 
165 protected:
166  virtual void sink_it_(details::log_msg &msg);
167  virtual void flush_();
168 
169  bool should_flush_(const details::log_msg &msg);
170 
171  // default error handler.
172  // print the error to stderr with the max rate of 1 message/minute.
173  void default_err_handler_(const std::string &msg);
174 
175  // increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER))
177 
179  std::vector<sink_ptr> sinks_;
183  std::atomic<time_t> last_err_time_{0};
184  std::atomic<size_t> msg_counter_{1};
185 };
186 } // namespace spdlog
187 
188 #include "details/logger_impl.h"
const CharType(& source)[N]
Definition: pointer.h:1147
level::level_enum flush_level() const
Definition: logger_impl.h:348
std::atomic< time_t > last_err_time_
Definition: logger.h:183
void incr_msg_counter_(details::log_msg &msg)
Definition: logger_impl.h:419
void error(const char *fmt, const Args &...args)
Definition: logger_impl.h:175
log_err_handler error_handler() const
Definition: logger_impl.h:329
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:81
void warn(const char *fmt, const Args &...args)
Definition: logger_impl.h:169
log_err_handler err_handler_
Definition: logger.h:182
spdlog::level_t level_
Definition: logger.h:180
void msg(const char *fmt,...)
Definition: message.cpp:107
logger & operator=(const logger &)=delete
std::string string
Definition: nybbler.cc:12
std::atomic< size_t > msg_counter_
Definition: logger.h:184
void log(level::level_enum lvl, const char *fmt, const Args &...args)
Definition: logger_impl.h:80
void info(const char *fmt, const Args &...args)
Definition: logger_impl.h:163
const std::vector< sink_ptr > & sinks() const
Definition: logger_impl.h:424
static QCString args
Definition: declinfo.cpp:674
std::shared_ptr< sinks::sink > sink_ptr
Definition: common.h:80
const std::string name_
Definition: logger.h:178
void set_pattern(std::string pattern, pattern_time_type time_type=pattern_time_type::local)
Definition: logger_impl.h:54
Definition: async.h:27
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
static level::level_enum default_level()
Definition: logger_impl.h:359
std::vector< sink_ptr > sinks_
Definition: logger.h:179
bool should_flush_(const details::log_msg &msg)
Definition: logger_impl.h:353
void default_err_handler_(const std::string &msg)
Definition: logger_impl.h:405
pattern_time_type
Definition: common.h:163
virtual ~logger()
void critical(const char *fmt, const Args &...args)
Definition: logger_impl.h:181
virtual std::shared_ptr< logger > clone(std::string logger_name)
Definition: logger_impl.h:434
void debug(const char *fmt, const Args &...args)
Definition: logger_impl.h:157
bool should_log(level::level_enum msg_level) const
Definition: logger_impl.h:369
spdlog::level_t flush_level_
Definition: logger.h:181
level::level_enum level() const
Definition: logger_impl.h:364
void set_formatter(std::unique_ptr< formatter > formatter)
Definition: logger_impl.h:46
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
void trace(const char *fmt, const Args &...args)
Definition: logger_impl.h:151
std::string pattern
Definition: regex_t.cc:33
virtual void flush_()
Definition: logger_impl.h:397
Definition: bin_to_hex.h:69
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:67
std::atomic< int > level_t
Definition: common.h:94
void set_error_handler(log_err_handler err_handler)
Definition: logger_impl.h:324
const std::string & name() const
Definition: logger_impl.h:314
std::function< void(const std::string &err_msg)> log_err_handler
Definition: common.h:82
logger(std::string name, sink_ptr single_sink)
Definition: logger_impl.h:39
void set_level(level::level_enum log_level)
Definition: logger_impl.h:319
void flush_on(level::level_enum log_level)
Definition: logger_impl.h:343
virtual void sink_it_(details::log_msg &msg)
Definition: logger_impl.h:378