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