Coverage Report

Created: 2026-02-14 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spdlog/include/spdlog/spdlog.h
Line
Count
Source
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/details/synchronous_factory.h>
15
#include <spdlog/logger.h>
16
#include <spdlog/version.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 to 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
    return default_factory::create<Sink>(std::move(logger_name),
36
                                         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 a 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,
60
                            pattern_time_type time_type = pattern_time_type::local);
61
62
// enable global backtrace support
63
SPDLOG_API void enable_backtrace(size_t n_messages);
64
65
// disable global backtrace support
66
SPDLOG_API void disable_backtrace();
67
68
// call dump backtrace on default logger
69
SPDLOG_API void dump_backtrace();
70
71
// Get global logging level
72
SPDLOG_API level::level_enum get_level();
73
74
// Set the global logging level
75
SPDLOG_API void set_level(level::level_enum log_level);
76
77
// Determine whether the default logger should log messages with a certain level
78
SPDLOG_API bool should_log(level::level_enum log_level);
79
80
// Set a global flush level
81
SPDLOG_API void flush_on(level::level_enum log_level);
82
83
// Start/Restart a periodic flusher thread
84
// Warning: Use only if all your loggers are thread safe!
85
template <typename Rep, typename Period>
86
inline void flush_every(std::chrono::duration<Rep, Period> interval) {
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
// Will throw if a logger with the same name already exists.
95
SPDLOG_API void register_logger(std::shared_ptr<logger> logger);
96
97
// Register the given logger with the given name
98
// Will replace any existing logger with the same name.
99
SPDLOG_API void register_or_replace(std::shared_ptr<logger> logger);
100
101
// Apply a user-defined function on all registered loggers
102
// Example:
103
// spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
104
SPDLOG_API void apply_all(const std::function<void(std::shared_ptr<logger>)> &fun);
105
106
// Drop the reference to the given logger
107
SPDLOG_API void drop(const std::string &name);
108
109
// Drop all references from the registry
110
SPDLOG_API void drop_all();
111
112
// stop any running threads started by spdlog and clean registry loggers
113
SPDLOG_API void shutdown();
114
115
// Automatic registration of loggers when using spdlog::create() or spdlog::create_async
116
SPDLOG_API void set_automatic_registration(bool automatic_registration);
117
118
// API for using default logger (stdout_color_mt),
119
// e.g.: spdlog::info("Message {}", 1);
120
//
121
// The default logger object can be accessed using the spdlog::default_logger():
122
// For example, to add another sink to it:
123
// spdlog::default_logger()->sinks().push_back(some_sink);
124
//
125
// The default logger can be replaced using spdlog::set_default_logger(new_logger).
126
// For example, to replace it with a file logger.
127
//
128
// IMPORTANT:
129
// The default API is thread safe (for _mt loggers), but:
130
// set_default_logger() *should not* be used concurrently with the default API.
131
// e.g., do not call set_default_logger() from one thread while calling spdlog::info() from another.
132
133
SPDLOG_API std::shared_ptr<spdlog::logger> default_logger();
134
135
SPDLOG_API spdlog::logger *default_logger_raw();
136
137
SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
138
139
// Initialize logger level based on environment configs.
140
//
141
// Useful for applying SPDLOG_LEVEL to manually created loggers.
142
//
143
// Example:
144
//   auto mylogger = std::make_shared<spdlog::logger>("mylogger", ...);
145
//   spdlog::apply_logger_env_levels(mylogger);
146
SPDLOG_API void apply_logger_env_levels(std::shared_ptr<logger> logger);
147
148
template <typename... Args>
149
inline void log(source_loc source,
150
                level::level_enum lvl,
151
                format_string_t<Args...> fmt,
152
                Args &&...args) {
153
    default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
154
}
155
156
template <typename... Args>
157
inline void log(level::level_enum lvl, format_string_t<Args...> fmt, Args &&...args) {
158
    default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
159
}
160
161
template <typename... Args>
162
906
inline void trace(format_string_t<Args...> fmt, Args &&...args) {
163
906
    default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
164
906
}
165
166
template <typename... Args>
167
906
inline void debug(format_string_t<Args...> fmt, Args &&...args) {
168
906
    default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
169
906
}
170
171
template <typename... Args>
172
906
inline void info(format_string_t<Args...> fmt, Args &&...args) {
173
906
    default_logger_raw()->info(fmt, std::forward<Args>(args)...);
174
906
}
175
176
template <typename... Args>
177
906
inline void warn(format_string_t<Args...> fmt, Args &&...args) {
178
906
    default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
179
906
}
180
181
template <typename... Args>
182
906
inline void error(format_string_t<Args...> fmt, Args &&...args) {
183
906
    default_logger_raw()->error(fmt, std::forward<Args>(args)...);
184
906
}
185
186
template <typename... Args>
187
906
inline void critical(format_string_t<Args...> fmt, Args &&...args) {
188
906
    default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
189
906
}
190
191
template <typename T>
192
inline void log(source_loc source, level::level_enum lvl, const T &msg) {
193
    default_logger_raw()->log(source, lvl, msg);
194
}
195
196
template <typename T>
197
inline void log(level::level_enum lvl, const T &msg) {
198
    default_logger_raw()->log(lvl, msg);
199
}
200
201
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
202
template <typename... Args>
203
inline void log(source_loc source,
204
                level::level_enum lvl,
205
                wformat_string_t<Args...> fmt,
206
                Args &&...args) {
207
    default_logger_raw()->log(source, lvl, fmt, std::forward<Args>(args)...);
208
}
209
210
template <typename... Args>
211
inline void log(level::level_enum lvl, wformat_string_t<Args...> fmt, Args &&...args) {
212
    default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
213
}
214
215
template <typename... Args>
216
inline void trace(wformat_string_t<Args...> fmt, Args &&...args) {
217
    default_logger_raw()->trace(fmt, std::forward<Args>(args)...);
218
}
219
220
template <typename... Args>
221
inline void debug(wformat_string_t<Args...> fmt, Args &&...args) {
222
    default_logger_raw()->debug(fmt, std::forward<Args>(args)...);
223
}
224
225
template <typename... Args>
226
inline void info(wformat_string_t<Args...> fmt, Args &&...args) {
227
    default_logger_raw()->info(fmt, std::forward<Args>(args)...);
228
}
229
230
template <typename... Args>
231
inline void warn(wformat_string_t<Args...> fmt, Args &&...args) {
232
    default_logger_raw()->warn(fmt, std::forward<Args>(args)...);
233
}
234
235
template <typename... Args>
236
inline void error(wformat_string_t<Args...> fmt, Args &&...args) {
237
    default_logger_raw()->error(fmt, std::forward<Args>(args)...);
238
}
239
240
template <typename... Args>
241
inline void critical(wformat_string_t<Args...> fmt, Args &&...args) {
242
    default_logger_raw()->critical(fmt, std::forward<Args>(args)...);
243
}
244
#endif
245
246
template <typename T>
247
inline void trace(const T &msg) {
248
    default_logger_raw()->trace(msg);
249
}
250
251
template <typename T>
252
inline void debug(const T &msg) {
253
    default_logger_raw()->debug(msg);
254
}
255
256
template <typename T>
257
inline void info(const T &msg) {
258
    default_logger_raw()->info(msg);
259
}
260
261
template <typename T>
262
inline void warn(const T &msg) {
263
    default_logger_raw()->warn(msg);
264
}
265
266
template <typename T>
267
inline void error(const T &msg) {
268
    default_logger_raw()->error(msg);
269
}
270
271
template <typename T>
272
inline void critical(const T &msg) {
273
    default_logger_raw()->critical(msg);
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
#ifndef SPDLOG_NO_SOURCE_LOC
292
#define SPDLOG_LOGGER_CALL(logger, level, ...) \
293
3.62k
    (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__)
294
#else
295
#define SPDLOG_LOGGER_CALL(logger, level, ...) \
296
    (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__)
297
#endif
298
299
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
300
#define SPDLOG_LOGGER_TRACE(logger, ...) \
301
    SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__)
302
#define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__)
303
#else
304
#define SPDLOG_LOGGER_TRACE(logger, ...) (void)0
305
906
#define SPDLOG_TRACE(...) (void)0
306
#endif
307
308
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG
309
#define SPDLOG_LOGGER_DEBUG(logger, ...) \
310
    SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__)
311
#define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__)
312
#else
313
#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0
314
906
#define SPDLOG_DEBUG(...) (void)0
315
#endif
316
317
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO
318
906
#define SPDLOG_LOGGER_INFO(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__)
319
906
#define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__)
320
#else
321
#define SPDLOG_LOGGER_INFO(logger, ...) (void)0
322
#define SPDLOG_INFO(...) (void)0
323
#endif
324
325
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN
326
906
#define SPDLOG_LOGGER_WARN(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__)
327
906
#define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__)
328
#else
329
#define SPDLOG_LOGGER_WARN(logger, ...) (void)0
330
#define SPDLOG_WARN(...) (void)0
331
#endif
332
333
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR
334
906
#define SPDLOG_LOGGER_ERROR(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__)
335
906
#define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__)
336
#else
337
#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0
338
#define SPDLOG_ERROR(...) (void)0
339
#endif
340
341
#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL
342
#define SPDLOG_LOGGER_CRITICAL(logger, ...) \
343
906
    SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__)
344
906
#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