Coverage Report

Created: 2026-12-01 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spdlog/include/spdlog/common.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
#pragma once
5
6
#include <spdlog/details/null_mutex.h>
7
#include <spdlog/tweakme.h>
8
9
#include <atomic>
10
#include <chrono>
11
#include <cstdio>
12
#include <exception>
13
#include <functional>
14
#include <initializer_list>
15
#include <memory>
16
#include <string>
17
#include <type_traits>
18
19
#ifdef SPDLOG_USE_STD_FORMAT
20
#include <version>
21
#if __cpp_lib_format >= 202207L
22
#include <format>
23
#else
24
#include <string_view>
25
#endif
26
#endif
27
28
#ifdef SPDLOG_COMPILED_LIB
29
#undef SPDLOG_HEADER_ONLY
30
#if defined(SPDLOG_SHARED_LIB)
31
#if defined(_WIN32)
32
#ifdef spdlog_EXPORTS
33
#define SPDLOG_API __declspec(dllexport)
34
#else  // !spdlog_EXPORTS
35
#define SPDLOG_API __declspec(dllimport)
36
#endif
37
#else  // !defined(_WIN32)
38
#define SPDLOG_API __attribute__((visibility("default")))
39
#endif
40
#else  // !defined(SPDLOG_SHARED_LIB)
41
#define SPDLOG_API
42
#endif
43
#define SPDLOG_INLINE
44
#else  // !defined(SPDLOG_COMPILED_LIB)
45
#define SPDLOG_API
46
#define SPDLOG_HEADER_ONLY
47
#define SPDLOG_INLINE inline
48
#endif  // #ifdef SPDLOG_COMPILED_LIB
49
50
#include <spdlog/fmt/fmt.h>
51
52
#if !defined(SPDLOG_USE_STD_FORMAT) && \
53
    FMT_VERSION >= 80000  // backward compatibility with fmt versions older than 8
54
#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
55
12.4k
#define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
56
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
57
#include <spdlog/fmt/xchar.h>
58
#endif
59
#else
60
#define SPDLOG_FMT_RUNTIME(format_string) format_string
61
#define SPDLOG_FMT_STRING(format_string) format_string
62
#endif
63
64
// visual studio up to 2013 does not support noexcept nor constexpr
65
#if defined(_MSC_VER) && (_MSC_VER < 1900)
66
#define SPDLOG_NOEXCEPT _NOEXCEPT
67
#define SPDLOG_CONSTEXPR
68
#else
69
#define SPDLOG_NOEXCEPT noexcept
70
#define SPDLOG_CONSTEXPR constexpr
71
#endif
72
73
// If building with std::format, can just use constexpr, otherwise if building with fmt
74
// SPDLOG_CONSTEXPR_FUNC needs to be set the same as FMT_CONSTEXPR to avoid situations where
75
// a constexpr function in spdlog could end up calling a non-constexpr function in fmt
76
// depending on the compiler
77
// If fmt determines it can't use constexpr, we should inline the function instead
78
#ifdef SPDLOG_USE_STD_FORMAT
79
#define SPDLOG_CONSTEXPR_FUNC constexpr
80
#else  // Being built with fmt
81
#if FMT_USE_CONSTEXPR
82
#define SPDLOG_CONSTEXPR_FUNC FMT_CONSTEXPR
83
#else
84
#define SPDLOG_CONSTEXPR_FUNC inline
85
#endif
86
#endif
87
88
#if defined(__GNUC__) || defined(__clang__)
89
#define SPDLOG_DEPRECATED __attribute__((deprecated))
90
#elif defined(_MSC_VER)
91
#define SPDLOG_DEPRECATED __declspec(deprecated)
92
#else
93
#define SPDLOG_DEPRECATED
94
#endif
95
96
// disable thread local on msvc 2013
97
#ifndef SPDLOG_NO_TLS
98
#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
99
#define SPDLOG_NO_TLS 1
100
#endif
101
#endif
102
103
#ifndef SPDLOG_FUNCTION
104
16.5k
#define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
105
#endif
106
107
#ifdef SPDLOG_NO_EXCEPTIONS
108
#define SPDLOG_TRY
109
#define SPDLOG_THROW(ex)                               \
110
    do {                                               \
111
        printf("spdlog fatal error: %s\n", ex.what()); \
112
        std::abort();                                  \
113
    } while (0)
114
#define SPDLOG_CATCH_STD
115
#else
116
41.2k
#define SPDLOG_TRY try
117
0
#define SPDLOG_THROW(ex) throw(ex)
118
#define SPDLOG_CATCH_STD             \
119
    catch (const std::exception &) { \
120
    }
121
#endif
122
123
#include "./namespace.h"
124
125
SPDLOG_NAMESPACE_BEGIN
126
127
class formatter;
128
129
namespace sinks {
130
class sink;
131
}
132
133
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
134
using filename_t = std::wstring;
135
// allow macro expansion to occur in SPDLOG_FILENAME_T
136
#define SPDLOG_FILENAME_T_INNER(s) L##s
137
#define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s)
138
#else
139
using filename_t = std::string;
140
2
#define SPDLOG_FILENAME_T(s) s
141
#endif
142
143
using log_clock = std::chrono::system_clock;
144
using sink_ptr = std::shared_ptr<sinks::sink>;
145
using sinks_init_list = std::initializer_list<sink_ptr>;
146
using err_handler = std::function<void(const std::string &err_msg)>;
147
#ifdef SPDLOG_USE_STD_FORMAT
148
namespace fmt_lib = std;
149
150
using string_view_t = std::string_view;
151
using memory_buf_t = std::string;
152
153
template <typename... Args>
154
#if __cpp_lib_format >= 202207L
155
using format_string_t = std::format_string<Args...>;
156
#else
157
using format_string_t = std::string_view;
158
#endif
159
160
template <class T, class Char = char>
161
struct is_convertible_to_basic_format_string
162
    : std::integral_constant<bool, std::is_convertible<T, std::basic_string_view<Char>>::value> {};
163
164
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
165
using wstring_view_t = std::wstring_view;
166
using wmemory_buf_t = std::wstring;
167
168
template <typename... Args>
169
#if __cpp_lib_format >= 202207L
170
using wformat_string_t = std::wformat_string<Args...>;
171
#else
172
using wformat_string_t = std::wstring_view;
173
#endif
174
#endif
175
#define SPDLOG_BUF_TO_STRING(x) x
176
#else  // use fmt lib instead of std::format
177
namespace fmt_lib = fmt;
178
179
using string_view_t = fmt::basic_string_view<char>;
180
using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
181
182
template <typename... Args>
183
using format_string_t = fmt::format_string<Args...>;
184
185
template <class T>
186
using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
187
188
template <typename Char>
189
#if FMT_VERSION >= 90101
190
using fmt_runtime_string = fmt::runtime_format_string<Char>;
191
#else
192
using fmt_runtime_string = fmt::basic_runtime<Char>;
193
#endif
194
195
// clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the
196
// condition from basic_format_string here, in addition, fmt::basic_runtime<Char> is only
197
// convertible to basic_format_string<Char> but not basic_string_view<Char>
198
template <class T, class Char = char>
199
struct is_convertible_to_basic_format_string
200
    : std::integral_constant<bool,
201
                             std::is_convertible<T, fmt::basic_string_view<Char>>::value ||
202
                                 std::is_same<remove_cvref_t<T>, fmt_runtime_string<Char>>::value> {
203
};
204
205
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
206
using wstring_view_t = fmt::basic_string_view<wchar_t>;
207
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
208
209
template <typename... Args>
210
using wformat_string_t = fmt::wformat_string<Args...>;
211
#endif
212
#define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
213
#endif
214
215
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
216
#ifndef _WIN32
217
#error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
218
#endif  // _WIN32
219
#endif  // SPDLOG_WCHAR_TO_UTF8_SUPPORT
220
221
template <class T>
222
struct is_convertible_to_any_format_string
223
    : std::integral_constant<bool,
224
                             is_convertible_to_basic_format_string<T, char>::value ||
225
                                 is_convertible_to_basic_format_string<T, wchar_t>::value> {};
226
227
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
228
using level_t = details::null_atomic_int;
229
#else
230
using level_t = std::atomic<int>;
231
#endif
232
233
#define SPDLOG_LEVEL_TRACE 0
234
#define SPDLOG_LEVEL_DEBUG 1
235
#define SPDLOG_LEVEL_INFO 2
236
#define SPDLOG_LEVEL_WARN 3
237
#define SPDLOG_LEVEL_ERROR 4
238
#define SPDLOG_LEVEL_CRITICAL 5
239
#define SPDLOG_LEVEL_OFF 6
240
241
#if !defined(SPDLOG_ACTIVE_LEVEL)
242
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
243
#endif
244
245
// Log level enum
246
namespace level {
247
enum level_enum : int {
248
    trace = SPDLOG_LEVEL_TRACE,
249
    debug = SPDLOG_LEVEL_DEBUG,
250
    info = SPDLOG_LEVEL_INFO,
251
    warn = SPDLOG_LEVEL_WARN,
252
    err = SPDLOG_LEVEL_ERROR,
253
    critical = SPDLOG_LEVEL_CRITICAL,
254
    off = SPDLOG_LEVEL_OFF,
255
    n_levels
256
};
257
258
#define SPDLOG_LEVEL_NAME_TRACE SPDLOG_NAMESPACE::string_view_t("trace", 5)
259
#define SPDLOG_LEVEL_NAME_DEBUG SPDLOG_NAMESPACE::string_view_t("debug", 5)
260
#define SPDLOG_LEVEL_NAME_INFO SPDLOG_NAMESPACE::string_view_t("info", 4)
261
#define SPDLOG_LEVEL_NAME_WARNING SPDLOG_NAMESPACE::string_view_t("warning", 7)
262
#define SPDLOG_LEVEL_NAME_ERROR SPDLOG_NAMESPACE::string_view_t("error", 5)
263
#define SPDLOG_LEVEL_NAME_CRITICAL SPDLOG_NAMESPACE::string_view_t("critical", 8)
264
#define SPDLOG_LEVEL_NAME_OFF SPDLOG_NAMESPACE::string_view_t("off", 3)
265
266
#if !defined(SPDLOG_LEVEL_NAMES)
267
#define SPDLOG_LEVEL_NAMES                                                                  \
268
    {                                                                                       \
269
        SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO,           \
270
            SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL, \
271
            SPDLOG_LEVEL_NAME_OFF                                                           \
272
    }
273
#endif
274
275
#if !defined(SPDLOG_SHORT_LEVEL_NAMES)
276
277
#define SPDLOG_SHORT_LEVEL_NAMES \
278
    { "T", "D", "I", "W", "E", "C", "O" }
279
#endif
280
281
SPDLOG_API const string_view_t &to_string_view(level::level_enum l) SPDLOG_NOEXCEPT;
282
SPDLOG_API const char *to_short_c_str(level::level_enum l) SPDLOG_NOEXCEPT;
283
SPDLOG_API level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
284
285
}  // namespace level
286
287
//
288
// Color mode used by sinks with color support.
289
//
290
enum class color_mode { always, automatic, never };
291
292
//
293
// Pattern time - specific time getting to use for pattern_formatter.
294
// local time by default
295
//
296
enum class pattern_time_type {
297
    local,  // log localtime
298
    utc     // log utc
299
};
300
301
//
302
// Log exception
303
//
304
class SPDLOG_API spdlog_ex : public std::exception {
305
public:
306
    explicit spdlog_ex(std::string msg);
307
    spdlog_ex(const std::string &msg, int last_errno);
308
    const char *what() const SPDLOG_NOEXCEPT override;
309
310
private:
311
    std::string msg_;
312
};
313
314
[[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno);
315
[[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg);
316
317
struct source_loc {
318
24.8k
    SPDLOG_CONSTEXPR source_loc() = default;
319
    SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
320
16.5k
        : filename{filename_in},
321
16.5k
          line{line_in},
322
16.5k
          funcname{funcname_in} {}
323
324
8.19k
    SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT { return line <= 0; }
325
    const char *filename{nullptr};
326
    int line{0};
327
    const char *funcname{nullptr};
328
};
329
330
struct file_event_handlers {
331
    file_event_handlers()
332
1
        : before_open(nullptr),
333
1
          after_open(nullptr),
334
1
          before_close(nullptr),
335
1
          after_close(nullptr) {}
336
337
    std::function<void(const filename_t &filename)> before_open;
338
    std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
339
    std::function<void(const filename_t &filename, std::FILE *file_stream)> before_close;
340
    std::function<void(const filename_t &filename)> after_close;
341
};
342
343
namespace details {
344
345
// make_unique support for pre c++14
346
#if __cplusplus >= 201402L  // C++14 and beyond
347
using std::enable_if_t;
348
using std::make_unique;
349
#else
350
template <bool B, class T = void>
351
using enable_if_t = typename std::enable_if<B, T>::type;
352
353
template <typename T, typename... Args>
354
std::unique_ptr<T> make_unique(Args &&...args) {
355
    static_assert(!std::is_array<T>::value, "arrays not supported");
356
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
357
}
358
#endif
359
360
// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
361
template <typename T, typename U, enable_if_t<!std::is_same<T, U>::value, int> = 0>
362
constexpr T conditional_static_cast(U value) {
363
    return static_cast<T>(value);
364
}
365
366
template <typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
367
0
constexpr T conditional_static_cast(U value) {
368
0
    return value;
369
0
}
370
371
}  // namespace details
372
SPDLOG_NAMESPACE_END
373
374
#ifdef SPDLOG_HEADER_ONLY
375
#include "common-inl.h"
376
#endif