spdlog
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
18#ifdef SPDLOG_COMPILED_LIB
19# undef SPDLOG_HEADER_ONLY
20# if defined(_WIN32) && defined(SPDLOG_SHARED_LIB)
21# ifdef spdlog_EXPORTS
22# define SPDLOG_API __declspec(dllexport)
23# else
24# define SPDLOG_API __declspec(dllimport)
25# endif
26# else // !defined(_WIN32) || !defined(SPDLOG_SHARED_LIB)
27# define SPDLOG_API
28# endif
29# define SPDLOG_INLINE
30#else // !defined(SPDLOG_COMPILED_LIB)
31# define SPDLOG_API
32# define SPDLOG_HEADER_ONLY
33# define SPDLOG_INLINE inline
34#endif // #ifdef SPDLOG_COMPILED_LIB
35
36#include <spdlog/fmt/fmt.h>
37
38// backward compatibility with fmt versions older than 8
39#if FMT_VERSION >= 80000
40# define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
41# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
42# include <spdlog/fmt/xchar.h>
43# endif
44#else
45# define SPDLOG_FMT_RUNTIME(format_string) format_string
46#endif
47
48// visual studio upto 2013 does not support noexcept nor constexpr
49#if defined(_MSC_VER) && (_MSC_VER < 1900)
50# define SPDLOG_NOEXCEPT _NOEXCEPT
51# define SPDLOG_CONSTEXPR
52#else
53# define SPDLOG_NOEXCEPT noexcept
54# define SPDLOG_CONSTEXPR constexpr
55#endif
56
57#if defined(__GNUC__) || defined(__clang__)
58# define SPDLOG_DEPRECATED __attribute__((deprecated))
59#elif defined(_MSC_VER)
60# define SPDLOG_DEPRECATED __declspec(deprecated)
61#else
62# define SPDLOG_DEPRECATED
63#endif
64
65// disable thread local on msvc 2013
66#ifndef SPDLOG_NO_TLS
67# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
68# define SPDLOG_NO_TLS 1
69# endif
70#endif
71
72#ifndef SPDLOG_FUNCTION
73# define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
74#endif
75
76#ifdef SPDLOG_NO_EXCEPTIONS
77# define SPDLOG_TRY
78# define SPDLOG_THROW(ex) \
79 do \
80 { \
81 printf("spdlog fatal error: %s\n", ex.what()); \
82 std::abort(); \
83 } while (0)
84# define SPDLOG_CATCH_STD
85#else
86# define SPDLOG_TRY try
87# define SPDLOG_THROW(ex) throw(ex)
88# define SPDLOG_CATCH_STD \
89 catch (const std::exception &) {}
90#endif
91
92namespace spdlog {
93
94class formatter;
95
96namespace sinks {
97class sink;
98}
99
100#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
102// allow macro expansion to occur in SPDLOG_FILENAME_T
103# define SPDLOG_FILENAME_T_INNER(s) L##s
104# define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s)
105#else
107# define SPDLOG_FILENAME_T(s) s
108#endif
109
113using err_handler = std::function<void(const std::string &err_msg)>;
114using string_view_t = fmt::basic_string_view<char>;
115using wstring_view_t = fmt::basic_string_view<wchar_t>;
116using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
117using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
118
119template<class T>
121
122// clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the condition from basic_format_string here,
123// in addition, fmt::basic_runtime<Char> is only convertible to basic_format_string<Char> but not basic_string_view<Char>
124template<class T, class Char = char>
127 std::is_convertible<T, fmt::basic_string_view<Char>>::value || std::is_same<remove_cvref_t<T>, fmt::basic_runtime<Char>>::value>
128{};
129
130#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
131# ifndef _WIN32
132# error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
133# endif // _WIN32
134#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
135
136template<class T>
137struct is_convertible_to_any_format_string : std::integral_constant<bool, is_convertible_to_basic_format_string<T, char>::value ||
138 is_convertible_to_basic_format_string<T, wchar_t>::value>
139{};
140
141#if defined(SPDLOG_NO_ATOMIC_LEVELS)
143#else
145#endif
146
147#define SPDLOG_LEVEL_TRACE 0
148#define SPDLOG_LEVEL_DEBUG 1
149#define SPDLOG_LEVEL_INFO 2
150#define SPDLOG_LEVEL_WARN 3
151#define SPDLOG_LEVEL_ERROR 4
152#define SPDLOG_LEVEL_CRITICAL 5
153#define SPDLOG_LEVEL_OFF 6
154
155#if !defined(SPDLOG_ACTIVE_LEVEL)
156# define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
157#endif
158
159// Log level enum
160namespace level {
172
173#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
174#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
175#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
176#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
177#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
178#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
179#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
180
181#if !defined(SPDLOG_LEVEL_NAMES)
182# define SPDLOG_LEVEL_NAMES \
183 { \
184 SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, \
185 SPDLOG_LEVEL_NAME_CRITICAL, SPDLOG_LEVEL_NAME_OFF \
186 }
187#endif
188
189#if !defined(SPDLOG_SHORT_LEVEL_NAMES)
190
191# define SPDLOG_SHORT_LEVEL_NAMES \
192 { \
193 "T", "D", "I", "W", "E", "C", "O" \
194 }
195#endif
196
200
201} // namespace level
202
203//
204// Color mode used by sinks with color support.
205//
206enum class color_mode
207{
208 always,
209 automatic,
210 never
211};
212
213//
214// Pattern time - specific time getting to use for pattern_formatter.
215// local time by default
216//
218{
219 local, // log localtime
220 utc // log utc
221};
222
223//
224// Log exception
225//
227{
228public:
229 explicit spdlog_ex(std::string msg);
230 spdlog_ex(const std::string &msg, int last_errno);
231 const char *what() const SPDLOG_NOEXCEPT override;
232
233private:
235};
236
237[[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno);
238[[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg);
239
241{
243 SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
244 : filename{filename_in}
245 , line{line_in}
246 , funcname{funcname_in}
247 {}
248
250 {
251 return line == 0;
252 }
253 const char *filename{nullptr};
254 int line{0};
255 const char *funcname{nullptr};
256};
257
258namespace details {
259// make_unique support for pre c++14
260
261#if __cplusplus >= 201402L // C++14 and beyond
262using std::make_unique;
263#else
264template<typename T, typename... Args>
266{
267 static_assert(!std::is_array<T>::value, "arrays not supported");
268 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
269}
270#endif
271} // namespace details
272} // namespace spdlog
273
274#ifdef SPDLOG_HEADER_ONLY
275# include "common-inl.h"
276#endif
std::string msg_
Definition common.h:234
#define SPDLOG_NOEXCEPT
Definition common.h:53
#define SPDLOG_LEVEL_INFO
Definition common.h:149
#define SPDLOG_LEVEL_DEBUG
Definition common.h:148
#define SPDLOG_API
Definition common.h:31
#define SPDLOG_LEVEL_TRACE
Definition common.h:147
#define SPDLOG_LEVEL_ERROR
Definition common.h:151
#define SPDLOG_CONSTEXPR
Definition common.h:54
#define SPDLOG_LEVEL_OFF
Definition common.h:153
#define SPDLOG_LEVEL_CRITICAL
Definition common.h:152
#define SPDLOG_LEVEL_WARN
Definition common.h:150
type
Definition core.h:1048
T
Definition core.h:320
T make_shared(T... args)
T make_unique(T... args)
std::unique_ptr< T > make_unique(Args &&...args)
Definition common.h:265
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
Definition common-inl.h:33
SPDLOG_INLINE const char * to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition common-inl.h:28
Definition async.h:25
fmt::basic_memory_buffer< wchar_t, 250 > wmemory_buf_t
Definition common.h:117
fmt::basic_string_view< wchar_t > wstring_view_t
Definition common.h:115
typename std::remove_cv< typename std::remove_reference< T >::type >::type remove_cvref_t
Definition common.h:120
fmt::basic_string_view< char > string_view_t
Definition common.h:114
color_mode
Definition common.h:207
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:68
pattern_time_type
Definition common.h:218
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116
const char * funcname
Definition common.h:255
SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT
Definition common.h:249
SPDLOG_CONSTEXPR source_loc()=default
SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
Definition common.h:243
const char * filename
Definition common.h:253