Livox SDK API  V2.2.0
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);
35  logger(std::string name, sinks_init_list sinks);
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  template<class T>
104  void log(level::level_enum lvl, const T &);
105 
106  // T can be statically converted to string_view
107  template<class T, typename std::enable_if<std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr>
108  void log(source_loc loc, level::level_enum lvl, const T &);
109 
110  // T cannot be statically converted to string_view
111  template<class T, typename std::enable_if<!std::is_convertible<T, spdlog::string_view_t>::value, T>::type * = nullptr>
112  void log(source_loc loc, level::level_enum lvl, const T &);
113 
114  template<typename T>
115  void trace(const T &msg);
116 
117  template<typename T>
118  void debug(const T &msg);
119 
120  template<typename T>
121  void info(const T &msg);
122 
123  template<typename T>
124  void warn(const T &msg);
125 
126  template<typename T>
127  void error(const T &msg);
128 
129  template<typename T>
130  void critical(const T &msg);
131 
132  bool should_log(level::level_enum msg_level) const;
133  void set_level(level::level_enum log_level);
134 
136  level::level_enum level() const;
137  const std::string &name() const;
138 
139  // set formatting for the sinks in this logger.
140  // each sink will get a seperate instance of the formatter object.
141  void set_formatter(std::unique_ptr<formatter> formatter);
142  void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
143 
144  // flush functions
145  void flush();
146  void flush_on(level::level_enum log_level);
148 
149  // sinks
150  const std::vector<sink_ptr> &sinks() const;
151  std::vector<sink_ptr> &sinks();
152 
153  // error handler
154  void set_error_handler(log_err_handler err_handler);
156 
157  // create new logger with same sinks and configuration.
158  virtual std::shared_ptr<logger> clone(std::string logger_name);
159 
160 protected:
161  virtual void sink_it_(details::log_msg &msg);
162  virtual void flush_();
163 
164  bool should_flush_(const details::log_msg &msg);
165 
166  // default error handler.
167  // print the error to stderr with the max rate of 1 message/minute.
168  void default_err_handler_(const std::string &msg);
169 
170  // increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER))
172 
173  const std::string name_;
174  std::vector<sink_ptr> sinks_;
177  log_err_handler err_handler_{[this](const std::string &msg) { this->default_err_handler_(msg); }};
178  std::atomic<time_t> last_err_time_{0};
179  std::atomic<size_t> msg_counter_{1};
180 };
181 } // namespace spdlog
182 
183 #include "details/logger_impl.h"
auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
level::level_enum flush_level() const
Definition: logger_impl.h:342
std::atomic< time_t > last_err_time_
Definition: logger.h:178
void incr_msg_counter_(details::log_msg &msg)
Definition: logger_impl.h:413
void error(const char *fmt, const Args &...args)
Definition: logger_impl.h:169
log_err_handler error_handler() const
Definition: logger_impl.h:323
std::initializer_list< sink_ptr > sinks_init_list
Definition: common.h:81
void warn(const char *fmt, const Args &...args)
Definition: logger_impl.h:163
log_err_handler err_handler_
Definition: logger.h:177
spdlog::level_t level_
Definition: logger.h:175
logger & operator=(const logger &)=delete
std::atomic< size_t > msg_counter_
Definition: logger.h:179
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:157
const std::vector< sink_ptr > & sinks() const
Definition: logger_impl.h:418
std::shared_ptr< sinks::sink > sink_ptr
Definition: common.h:80
const std::string name_
Definition: logger.h:173
void set_pattern(std::string pattern, pattern_time_type time_type=pattern_time_type::local)
Definition: logger_impl.h:54
Definition: async.h:27
static level::level_enum default_level()
Definition: logger_impl.h:353
std::vector< sink_ptr > sinks_
Definition: logger.h:174
bool should_flush_(const details::log_msg &msg)
Definition: logger_impl.h:347
void default_err_handler_(const std::string &msg)
Definition: logger_impl.h:399
pattern_time_type
Definition: common.h:166
virtual ~logger()
void critical(const char *fmt, const Args &...args)
Definition: logger_impl.h:175
virtual std::shared_ptr< logger > clone(std::string logger_name)
Definition: logger_impl.h:428
void debug(const char *fmt, const Args &...args)
Definition: logger_impl.h:151
bool should_log(level::level_enum msg_level) const
Definition: logger_impl.h:363
spdlog::level_t flush_level_
Definition: logger.h:176
level::level_enum level() const
Definition: logger_impl.h:358
void set_formatter(std::unique_ptr< formatter > formatter)
Definition: logger_impl.h:46
void trace(const char *fmt, const Args &...args)
Definition: logger_impl.h:145
virtual void flush_()
Definition: logger_impl.h:391
Definition: bin_to_hex.h:69
std::atomic< int > level_t
Definition: common.h:94
void set_error_handler(log_err_handler err_handler)
Definition: logger_impl.h:318
const std::string & name() const
Definition: logger_impl.h:308
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
auto end(const C &c) -> decltype(c.end())
Definition: format.h:257
void set_level(level::level_enum log_level)
Definition: logger_impl.h:313
void flush_on(level::level_enum log_level)
Definition: logger_impl.h:337
virtual void sink_it_(details::log_msg &msg)
Definition: logger_impl.h:372