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