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