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> |
13 | #include <spdlog/details/registry.h> |
14 | #include <spdlog/logger.h> |
15 | #include <spdlog/version.h> |
16 | #include <spdlog/details/synchronous_factory.h> |
17 | |
18 | #include <chrono> |
19 | #include <functional> |
20 | #include <memory> |
21 | #include <string> |
22 | |
23 | namespace spdlog { |
24 | |
25 | using default_factory = synchronous_factory; |
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 |
65 | SPDLOG_API void disable_backtrace(); |
66 | |
67 | // call dump backtrace on default logger |
68 | SPDLOG_API void dump_backtrace(); |
69 | |
70 | // Get global logging level |
71 | SPDLOG_API level::level_enum get_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 |
77 | SPDLOG_API bool should_log(level::level_enum lvl); |
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 | template<typename Rep, typename Period> |
85 | inline void flush_every(std::chrono::duration<Rep, Period> interval) |
86 | { |
87 | details::registry::instance().flush_every(interval); |
88 | } |
89 | |
90 | // Set global error handler |
91 | SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg)); |
92 | |
93 | // Register the given logger with the given name |
94 | SPDLOG_API void register_logger(std::shared_ptr<logger> logger); |
95 | |
96 | // Apply a user defined function on all registered loggers |
97 | // Example: |
98 | // spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();}); |
99 | SPDLOG_API void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun); |
100 | |
101 | // Drop the reference to the given logger |
102 | SPDLOG_API void drop(const std::string &name); |
103 | |
104 | // Drop all references from the registry |
105 | SPDLOG_API void drop_all(); |
106 | |
107 | // stop any running threads started by spdlog and clean registry loggers |
108 | SPDLOG_API void shutdown(); |
109 | |
110 | // Automatic registration of loggers when using spdlog::create() or spdlog::create_async |
111 | SPDLOG_API void set_automatic_registration(bool automatic_registration); |
112 | |
113 | // API for using default logger (stdout_color_mt), |
114 | // e.g: spdlog::info("Message {}", 1); |
115 | // |
116 | // The default logger object can be accessed using the spdlog::default_logger(): |
117 | // For example, to add another sink to it: |
118 | // spdlog::default_logger()->sinks().push_back(some_sink); |
119 | // |
120 | // The default logger can replaced using spdlog::set_default_logger(new_logger). |
121 | // For example, to replace it with a file logger. |
122 | // |
123 | // IMPORTANT: |
124 | // The default API is thread safe (for _mt loggers), but: |
125 | // set_default_logger() *should not* be used concurrently with the default API. |
126 | // e.g do not call set_default_logger() from one thread while calling spdlog::info() from another. |
127 | |
128 | SPDLOG_API std::shared_ptr<spdlog::logger> default_logger(); |
129 | |
130 | SPDLOG_API spdlog::logger *default_logger_raw(); |
131 | |
132 | SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger); |
133 | |
134 | template<typename... Args> |
135 | inline void log(source_loc source, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args) |
136 | { |
137 | default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...); |
138 | } |
139 | |
140 | template<typename... Args> |
141 | inline void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args) |
142 | { |
143 | default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...); |
144 | } |
145 | |
146 | template<typename... Args> |
147 | inline void trace(format_string_t<Args...> fmt, Args &&... args) |
148 | { |
149 | default_logger_raw()->trace(fmt, std::forward<Args>(args)...); |
150 | } |
151 | |
152 | template<typename... Args> |
153 | inline void debug(format_string_t<Args...> fmt, Args &&... args) |
154 | { |
155 | default_logger_raw()->debug(fmt, std::forward<Args>(args)...); |
156 | } |
157 | |
158 | template<typename... Args> |
159 | inline void info(format_string_t<Args...> fmt, Args &&... args) |
160 | { |
161 | default_logger_raw()->info(fmt, std::forward<Args>(args)...); |
162 | } |
163 | |
164 | template<typename... Args> |
165 | inline void warn(format_string_t<Args...> fmt, Args &&... args) |
166 | { |
167 | default_logger_raw()->warn(fmt, std::forward<Args>(args)...); |
168 | } |
169 | |
170 | template<typename... Args> |
171 | inline void error(format_string_t<Args...> fmt, Args &&... args) |
172 | { |
173 | default_logger_raw()->error(fmt, std::forward<Args>(args)...); |
174 | } |
175 | |
176 | template<typename... Args> |
177 | inline void critical(format_string_t<Args...> fmt, Args &&... args) |
178 | { |
179 | default_logger_raw()->critical(fmt, std::forward<Args>(args)...); |
180 | } |
181 | |
182 | template<typename T> |
183 | inline void log(source_loc source, level::level_enum lvl, const T &msg) |
184 | { |
185 | default_logger_raw()->log(source, lvl, msg); |
186 | } |
187 | |
188 | template<typename T> |
189 | inline void log(level::level_enum lvl, const T &msg) |
190 | { |
191 | default_logger_raw()->log(lvl, msg); |
192 | } |
193 | |
194 | #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT |
195 | template<typename... Args> |
196 | inline void log(source_loc source, level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args) |
197 | { |
198 | default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...); |
199 | } |
200 | |
201 | template<typename... Args> |
202 | inline void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&... args) |
203 | { |
204 | default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...); |
205 | } |
206 | |
207 | template<typename... Args> |
208 | inline void trace(wformat_string_t<Args...> fmt, Args &&... args) |
209 | { |
210 | default_logger_raw()->trace(fmt, std::forward<Args>(args)...); |
211 | } |
212 | |
213 | template<typename... Args> |
214 | inline void debug(wformat_string_t<Args...> fmt, Args &&... args) |
215 | { |
216 | default_logger_raw()->debug(fmt, std::forward<Args>(args)...); |
217 | } |
218 | |
219 | template<typename... Args> |
220 | inline void info(wformat_string_t<Args...> fmt, Args &&... args) |
221 | { |
222 | default_logger_raw()->info(fmt, std::forward<Args>(args)...); |
223 | } |
224 | |
225 | template<typename... Args> |
226 | inline void warn(wformat_string_t<Args...> fmt, Args &&... args) |
227 | { |
228 | default_logger_raw()->warn(fmt, std::forward<Args>(args)...); |
229 | } |
230 | |
231 | template<typename... Args> |
232 | inline void error(wformat_string_t<Args...> fmt, Args &&... args) |
233 | { |
234 | default_logger_raw()->error(fmt, std::forward<Args>(args)...); |
235 | } |
236 | |
237 | template<typename... Args> |
238 | inline void critical(wformat_string_t<Args...> fmt, Args &&... args) |
239 | { |
240 | default_logger_raw()->critical(fmt, std::forward<Args>(args)...); |
241 | } |
242 | #endif |
243 | |
244 | template<typename T> |
245 | inline void trace(const T &msg) |
246 | { |
247 | default_logger_raw()->trace(msg); |
248 | } |
249 | |
250 | template<typename T> |
251 | inline void debug(const T &msg) |
252 | { |
253 | default_logger_raw()->debug(msg); |
254 | } |
255 | |
256 | template<typename T> |
257 | inline void info(const T &msg) |
258 | { |
259 | default_logger_raw()->info(msg); |
260 | } |
261 | |
262 | template<typename T> |
263 | inline void warn(const T &msg) |
264 | { |
265 | default_logger_raw()->warn(msg); |
266 | } |
267 | |
268 | template<typename T> |
269 | inline void error(const T &msg) |
270 | { |
271 | default_logger_raw()->error(msg); |
272 | } |
273 | |
274 | template<typename T> |
275 | inline void critical(const T &msg) |
276 | { |
277 | default_logger_raw()->critical(msg); |
278 | } |
279 | |
280 | } // namespace spdlog |
281 | |
282 | // |
283 | // enable/disable log calls at compile time according to global level. |
284 | // |
285 | // define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h): |
286 | // SPDLOG_LEVEL_TRACE, |
287 | // SPDLOG_LEVEL_DEBUG, |
288 | // SPDLOG_LEVEL_INFO, |
289 | // SPDLOG_LEVEL_WARN, |
290 | // SPDLOG_LEVEL_ERROR, |
291 | // SPDLOG_LEVEL_CRITICAL, |
292 | // SPDLOG_LEVEL_OFF |
293 | // |
294 | |
295 | #ifndef SPDLOG_NO_SOURCE_LOC |
296 | # define SPDLOG_LOGGER_CALL(logger, level, ...) \ |
297 | (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) |
298 | #else |
299 | # define SPDLOG_LOGGER_CALL(logger, level, ...) (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__) |
300 | #endif |
301 | |
302 | #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE |
303 | # define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) |
304 | # define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__) |
305 | #else |
306 | # define SPDLOG_LOGGER_TRACE(logger, ...) (void)0 |
307 | # define SPDLOG_TRACE(...) (void)0 |
308 | #endif |
309 | |
310 | #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG |
311 | # define SPDLOG_LOGGER_DEBUG(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__) |
312 | # define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__) |
313 | #else |
314 | # define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0 |
315 | # define SPDLOG_DEBUG(...) (void)0 |
316 | #endif |
317 | |
318 | #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO |
319 | # define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__) |
320 | # define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__) |
321 | #else |
322 | # define SPDLOG_LOGGER_INFO(logger, ...) (void)0 |
323 | # define SPDLOG_INFO(...) (void)0 |
324 | #endif |
325 | |
326 | #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN |
327 | # define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__) |
328 | # define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__) |
329 | #else |
330 | # define SPDLOG_LOGGER_WARN(logger, ...) (void)0 |
331 | # define SPDLOG_WARN(...) (void)0 |
332 | #endif |
333 | |
334 | #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR |
335 | # define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__) |
336 | # define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__) |
337 | #else |
338 | # define SPDLOG_LOGGER_ERROR(logger, ...) (void)0 |
339 | # define SPDLOG_ERROR(...) (void)0 |
340 | #endif |
341 | |
342 | #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL |
343 | # define SPDLOG_LOGGER_CRITICAL(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__) |
344 | # define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__) |
345 | #else |
346 | # define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0 |
347 | # define SPDLOG_CRITICAL(...) (void)0 |
348 | #endif |
349 | |
350 | #ifdef SPDLOG_HEADER_ONLY |
351 | # include "spdlog-inl.h" |
352 | #endif |
353 | |
354 | #endif // SPDLOG_H |
355 | |