Livox SDK API  V2.2.0
spdlog.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2015-2018 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 // spdlog main header file.
6 // see example.cpp for usage example
7 
8 #ifndef SPDLOG_H
9 #define SPDLOG_H
10 #pragma once
11 
12 #include "spdlog/common.h"
14 #include "spdlog/logger.h"
15 #include "spdlog/version.h"
16 
17 #include <chrono>
18 #include <functional>
19 #include <memory>
20 #include <string>
21 
22 namespace spdlog {
23 
24 // Default logger factory- creates synchronous loggers
26 {
27  template<typename Sink, typename... SinkArgs>
28  static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... args)
29  {
30  auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
31  auto new_logger = std::make_shared<logger>(std::move(logger_name), std::move(sink));
33  return new_logger;
34  }
35 };
36 
38 
39 // Create and register a logger with a templated sink type
40 // The logger's level, formatter and flush level will be set according the
41 // global settings.
42 // Example:
43 // spdlog::create<daily_file_sink_st>("logger_name", "dailylog_filename", 11, 59);
44 template<typename Sink, typename... SinkArgs>
45 inline std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... sink_args)
46 {
47  return default_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
48 }
49 
50 // Return an existing logger or nullptr if a logger with such name doesn't
51 // exist.
52 // example: spdlog::get("my_logger")->info("hello {}", "world");
53 inline std::shared_ptr<logger> get(const std::string &name)
54 {
55  return details::registry::instance().get(name);
56 }
57 
58 // Set global formatter. Each sink in each logger will get a clone of this object
59 inline void set_formatter(std::unique_ptr<spdlog::formatter> formatter)
60 {
61  details::registry::instance().set_formatter(std::move(formatter));
62 }
63 
64 // Set global format string.
65 // example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
66 inline void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local)
67 {
68  set_formatter(std::unique_ptr<spdlog::formatter>(new pattern_formatter(std::move(pattern), time_type)));
69 }
70 
71 // Set global logging level
72 inline void set_level(level::level_enum log_level)
73 {
75 }
76 
77 // Set global flush level
78 inline void flush_on(level::level_enum log_level)
79 {
81 }
82 
83 // Start/Restart a periodic flusher thread
84 // Warning: Use only if all your loggers are thread safe!
85 inline void flush_every(std::chrono::seconds interval)
86 {
88 }
89 
90 // Set global error handler
91 inline void set_error_handler(log_err_handler handler)
92 {
93  details::registry::instance().set_error_handler(std::move(handler));
94 }
95 
96 // Register the given logger with the given name
97 inline void register_logger(std::shared_ptr<logger> logger)
98 {
99  details::registry::instance().register_logger(std::move(logger));
100 }
101 
102 // Apply a user defined function on all registered loggers
103 // Example:
104 // spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
105 inline void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun)
106 {
108 }
109 
110 // Drop the reference to the given logger
111 inline void drop(const std::string &name)
112 {
114 }
115 
116 // Drop all references from the registry
117 inline void drop_all()
118 {
120 }
121 
122 // stop any running threads started by spdlog and clean registry loggers
123 inline void shutdown()
124 {
126 }
127 
128 // Automatic registration of loggers when using spdlog::create() or spdlog::create_async
129 inline void set_automatic_registration(bool automatic_registation)
130 {
132 }
133 
134 // API for using default logger (stdout_color_mt),
135 // e.g: spdlog::info("Message {}", 1);
136 //
137 // The default logger object can be accessed using the spdlog::default_logger():
138 // For example, to add another sink to it:
139 // spdlog::default_logger()->sinks()->push_back(some_sink);
140 //
141 // The default logger can replaced using spdlog::set_default_logger(new_logger).
142 // For example, to replace it with a file logger.
143 //
144 // IMPORTANT:
145 // The default API is thread safe (for _mt loggers), but:
146 // set_default_logger() *should not* be used concurrently with the default API.
147 // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another.
148 
149 inline std::shared_ptr<spdlog::logger> default_logger()
150 {
152 }
153 
155 {
157 }
158 
159 inline void set_default_logger(std::shared_ptr<spdlog::logger> default_logger)
160 {
161  details::registry::instance().set_default_logger(std::move(default_logger));
162 }
163 
164 template<typename... Args>
165 inline void log(source_loc source, level::level_enum lvl, const char *fmt, const Args &... args)
166 {
167  default_logger_raw()->log(source, lvl, fmt, args...);
168 }
169 
170 template<typename... Args>
171 inline void log(level::level_enum lvl, const char *fmt, const Args &... args)
172 {
173  default_logger_raw()->log(source_loc{}, lvl, fmt, args...);
174 }
175 
176 template<typename... Args>
177 inline void trace(const char *fmt, const Args &... args)
178 {
179  default_logger_raw()->trace(fmt, args...);
180 }
181 
182 template<typename... Args>
183 inline void debug(const char *fmt, const Args &... args)
184 {
185  default_logger_raw()->debug(fmt, args...);
186 }
187 
188 template<typename... Args>
189 inline void info(const char *fmt, const Args &... args)
190 {
191  default_logger_raw()->info(fmt, args...);
192 }
193 
194 template<typename... Args>
195 inline void warn(const char *fmt, const Args &... args)
196 {
197  default_logger_raw()->warn(fmt, args...);
198 }
199 
200 template<typename... Args>
201 inline void error(const char *fmt, const Args &... args)
202 {
203  default_logger_raw()->error(fmt, args...);
204 }
205 
206 template<typename... Args>
207 inline void critical(const char *fmt, const Args &... args)
208 {
209  default_logger_raw()->critical(fmt, args...);
210 }
211 
212 template<typename T>
213 inline void log(level::level_enum lvl, const T &msg)
214 {
215  default_logger_raw()->log(lvl, msg);
216 }
217 
218 template<typename T>
219 inline void trace(const T &msg)
220 {
221  default_logger_raw()->trace(msg);
222 }
223 
224 template<typename T>
225 inline void debug(const T &msg)
226 {
227  default_logger_raw()->debug(msg);
228 }
229 
230 template<typename T>
231 inline void info(const T &msg)
232 {
233  default_logger_raw()->info(msg);
234 }
235 
236 template<typename T>
237 inline void warn(const T &msg)
238 {
239  default_logger_raw()->warn(msg);
240 }
241 
242 template<typename T>
243 inline void error(const T &msg)
244 {
245  default_logger_raw()->error(msg);
246 }
247 
248 template<typename T>
249 inline void critical(const T &msg)
250 {
252 }
253 
254 #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
255 template<typename... Args>
256 inline void log(level::level_enum lvl, const wchar_t *fmt, const Args &... args)
257 {
258  default_logger_raw()->log(lvl, fmt, args...);
259 }
260 
261 template<typename... Args>
262 inline void trace(const wchar_t *fmt, const Args &... args)
263 {
264  default_logger_raw()->trace(fmt, args...);
265 }
266 
267 template<typename... Args>
268 inline void debug(const wchar_t *fmt, const Args &... args)
269 {
270  default_logger_raw()->debug(fmt, args...);
271 }
272 
273 template<typename... Args>
274 inline void info(const wchar_t *fmt, const Args &... args)
275 {
276  default_logger_raw()->info(fmt, args...);
277 }
278 
279 template<typename... Args>
280 inline void warn(const wchar_t *fmt, const Args &... args)
281 {
282  default_logger_raw()->warn(fmt, args...);
283 }
284 
285 template<typename... Args>
286 inline void error(const wchar_t *fmt, const Args &... args)
287 {
288  default_logger_raw()->error(fmt, args...);
289 }
290 
291 template<typename... Args>
292 inline void critical(const wchar_t *fmt, const Args &... args)
293 {
294  default_logger_raw()->critical(fmt, args...);
295 }
296 
297 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
298 
299 } // namespace spdlog
300 
301 //
302 // enable/disable log calls at compile time according to global level.
303 //
304 // define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h):
305 // SPDLOG_LEVEL_TRACE,
306 // SPDLOG_LEVEL_DEBUG,
307 // SPDLOG_LEVEL_INFO,
308 // SPDLOG_LEVEL_WARN,
309 // SPDLOG_LEVEL_ERROR,
310 // SPDLOG_LEVEL_CRITICAL,
311 // SPDLOG_LEVEL_OFF
312 //
313 
314 #define SPDLOG_LOGGER_CALL(logger, level, ...) \
315  if (logger->should_log(level)) \
316  logger->log(spdlog::source_loc{SPDLOG_FILE_BASENAME(__FILE__), __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
317 
318 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
319 #define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
320 #define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
321 #else
322 #define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
323 #define SPDLOG_TRACE(...) (void)0
324 #endif
325 
326 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
327 #define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
328 #define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
329 #else
330 #define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
331 #define SPDLOG_DEBUG(...) (void)0
332 #endif
333 
334 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
335 #define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
336 #define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
337 #else
338 #define SPDLOG_LOGGER_INFO(logger, ...) (void)0
339 #define SPDLOG_INFO(...) (void)0
340 #endif
341 
342 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
343 #define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
344 #define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
345 #else
346 #define SPDLOG_LOGGER_WARN(logger, ...) (void)0
347 #define SPDLOG_WARN(...) (void)0
348 #endif
349 
350 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
351 #define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
352 #define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
353 #else
354 #define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
355 #define SPDLOG_ERROR(...) (void)0
356 #endif
357 
358 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
359 #define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
360 #define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__)
361 #else
362 #define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0
363 #define SPDLOG_CRITICAL(...) (void)0
364 #endif
365 
366 #endif // SPDLOG_H
void error(const char *fmt, const Args &...args)
Definition: logger_impl.h:169
void warn(const char *fmt, const Args &...args)
Definition: logger_impl.h:163
void apply_all(const std::function< void(const std::shared_ptr< logger >)> &fun)
Definition: registry.h:166
void info(const char *fmt, const Args &...args)
Definition: spdlog.h:189
void set_formatter(std::unique_ptr< formatter > formatter)
Definition: registry.h:119
void log(level::level_enum lvl, const char *fmt, const Args &...args)
Definition: logger_impl.h:80
void flush_on(level::level_enum log_level)
Definition: registry.h:139
void info(const char *fmt, const Args &...args)
Definition: logger_impl.h:157
void flush_every(std::chrono::seconds interval)
Definition: spdlog.h:85
void drop(const std::string &name)
Definition: spdlog.h:111
void set_automatic_registration(bool automatic_registation)
Definition: spdlog.h:129
static registry & instance()
Definition: registry.h:228
void set_error_handler(log_err_handler handler)
Definition: registry.h:156
void flush_on(level::level_enum log_level)
Definition: spdlog.h:78
static std::shared_ptr< spdlog::logger > create(std::string logger_name, SinkArgs &&...args)
Definition: spdlog.h:28
Definition: async.h:27
void critical(const char *fmt, const Args &...args)
Definition: spdlog.h:207
void set_error_handler(log_err_handler handler)
Definition: spdlog.h:91
std::shared_ptr< logger > get(const std::string &logger_name)
Definition: registry.h:67
void set_default_logger(std::shared_ptr< spdlog::logger > default_logger)
Definition: spdlog.h:159
void set_level(level::level_enum log_level)
Definition: registry.h:129
pattern_time_type
Definition: common.h:166
spdlog::logger * default_logger_raw()
Definition: spdlog.h:154
void initialize_logger(std::shared_ptr< logger > new_logger)
Definition: registry.h:48
void shutdown()
Definition: spdlog.h:123
void critical(const char *fmt, const Args &...args)
Definition: logger_impl.h:175
void set_default_logger(std::shared_ptr< logger > new_default_logger)
Definition: registry.h:91
void warn(const char *fmt, const Args &...args)
Definition: spdlog.h:195
void debug(const char *fmt, const Args &...args)
Definition: logger_impl.h:151
void log(source_loc source, level::level_enum lvl, const char *fmt, const Args &...args)
Definition: spdlog.h:165
std::shared_ptr< spdlog::logger > default_logger()
Definition: spdlog.h:149
void set_automatic_registration(bool automatic_regsistration)
Definition: registry.h:222
void set_pattern(std::string pattern, pattern_time_type time_type=pattern_time_type::local)
Definition: spdlog.h:66
void register_logger(std::shared_ptr< logger > logger)
Definition: spdlog.h:97
void flush_every(std::chrono::seconds interval)
Definition: registry.h:149
void trace(const char *fmt, const Args &...args)
Definition: logger_impl.h:145
void debug(const char *fmt, const Args &...args)
Definition: spdlog.h:183
Definition: bin_to_hex.h:69
void apply_all(const std::function< void(std::shared_ptr< logger >)> &fun)
Definition: spdlog.h:105
void drop(const std::string &logger_name)
Definition: registry.h:184
std::function< void(const std::string &err_msg)> log_err_handler
Definition: common.h:82
void register_logger(std::shared_ptr< logger > new_logger)
Definition: registry.h:42
void set_formatter(std::unique_ptr< spdlog::formatter > formatter)
Definition: spdlog.h:59
logger * get_default_raw()
Definition: registry.h:84
void set_level(level::level_enum log_level)
Definition: spdlog.h:72
void trace(const char *fmt, const Args &...args)
Definition: spdlog.h:177
std::shared_ptr< logger > default_logger()
Definition: registry.h:74
void drop_all()
Definition: spdlog.h:117
void error(const char *fmt, const Args &...args)
Definition: spdlog.h:201