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> |
7 | #include <spdlog/details/null_mutex.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 _WIN32 |
19 | #ifndef NOMINMAX |
20 | #define NOMINMAX // prevent windows redefining min/max |
21 | #endif |
22 | |
23 | #ifndef WIN32_LEAN_AND_MEAN |
24 | #define WIN32_LEAN_AND_MEAN |
25 | #endif |
26 | |
27 | #include <windows.h> |
28 | #endif //_WIN32 |
29 | |
30 | #ifdef SPDLOG_COMPILED_LIB |
31 | #undef SPDLOG_HEADER_ONLY |
32 | #define SPDLOG_INLINE |
33 | #else |
34 | #define |
35 | #define SPDLOG_INLINE inline |
36 | #endif |
37 | |
38 | #include <spdlog/fmt/fmt.h> |
39 | |
40 | // visual studio upto 2013 does not support noexcept nor constexpr |
41 | #if defined(_MSC_VER) && (_MSC_VER < 1900) |
42 | #define SPDLOG_NOEXCEPT _NOEXCEPT |
43 | #define SPDLOG_CONSTEXPR |
44 | #else |
45 | #define SPDLOG_NOEXCEPT noexcept |
46 | #define SPDLOG_CONSTEXPR constexpr |
47 | #endif |
48 | |
49 | #if defined(__GNUC__) || defined(__clang__) |
50 | #define SPDLOG_DEPRECATED __attribute__((deprecated)) |
51 | #elif defined(_MSC_VER) |
52 | #define SPDLOG_DEPRECATED __declspec(deprecated) |
53 | #else |
54 | #define SPDLOG_DEPRECATED |
55 | #endif |
56 | |
57 | // disable thread local on msvc 2013 |
58 | #ifndef SPDLOG_NO_TLS |
59 | #if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt) |
60 | #define SPDLOG_NO_TLS 1 |
61 | #endif |
62 | #endif |
63 | |
64 | #ifndef SPDLOG_FUNCTION |
65 | #define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__) |
66 | #endif |
67 | |
68 | #ifdef SPDLOG_NO_EXCEPTIONS |
69 | #define SPDLOG_TRY |
70 | #define SPDLOG_THROW(ex) \ |
71 | do \ |
72 | { \ |
73 | printf("spdlog fatal error: %s\n", ex.what()); \ |
74 | std::abort(); \ |
75 | } while (0) |
76 | #define SPDLOG_CATCH_ALL() |
77 | #else |
78 | #define SPDLOG_TRY try |
79 | #define SPDLOG_THROW(ex) throw(ex) |
80 | #define SPDLOG_CATCH_ALL() catch (...) |
81 | #endif |
82 | |
83 | namespace spdlog { |
84 | |
85 | class formatter; |
86 | |
87 | namespace sinks { |
88 | class sink; |
89 | } |
90 | |
91 | #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) |
92 | using filename_t = std::wstring; |
93 | #define SPDLOG_FILENAME_T(s) L##s |
94 | #else |
95 | using filename_t = std::string; |
96 | #define SPDLOG_FILENAME_T(s) s |
97 | #endif |
98 | |
99 | using log_clock = std::chrono::system_clock; |
100 | using sink_ptr = std::shared_ptr<sinks::sink>; |
101 | using sinks_init_list = std::initializer_list<sink_ptr>; |
102 | using err_handler = std::function<void(const std::string &err_msg)>; |
103 | using string_view_t = fmt::basic_string_view<char>; |
104 | using wstring_view_t = fmt::basic_string_view<wchar_t>; |
105 | using memory_buf_t = fmt::basic_memory_buffer<char, 250>; |
106 | |
107 | #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT |
108 | #ifndef _WIN32 |
109 | #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows |
110 | #else |
111 | template<typename T> |
112 | struct is_convertible_to_wstring_view : std::is_convertible<T, wstring_view_t> |
113 | {}; |
114 | #endif // _WIN32 |
115 | #else |
116 | template<typename> |
117 | struct is_convertible_to_wstring_view : std::false_type |
118 | {}; |
119 | #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT |
120 | |
121 | #if defined(SPDLOG_NO_ATOMIC_LEVELS) |
122 | using level_t = details::null_atomic_int; |
123 | #else |
124 | using level_t = std::atomic<int>; |
125 | #endif |
126 | |
127 | #define SPDLOG_LEVEL_TRACE 0 |
128 | #define SPDLOG_LEVEL_DEBUG 1 |
129 | #define SPDLOG_LEVEL_INFO 2 |
130 | #define SPDLOG_LEVEL_WARN 3 |
131 | #define SPDLOG_LEVEL_ERROR 4 |
132 | #define SPDLOG_LEVEL_CRITICAL 5 |
133 | #define SPDLOG_LEVEL_OFF 6 |
134 | |
135 | #if !defined(SPDLOG_ACTIVE_LEVEL) |
136 | #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO |
137 | #endif |
138 | |
139 | // Log level enum |
140 | namespace level { |
141 | enum level_enum |
142 | { |
143 | trace = SPDLOG_LEVEL_TRACE, |
144 | debug = SPDLOG_LEVEL_DEBUG, |
145 | info = SPDLOG_LEVEL_INFO, |
146 | warn = SPDLOG_LEVEL_WARN, |
147 | err = SPDLOG_LEVEL_ERROR, |
148 | critical = SPDLOG_LEVEL_CRITICAL, |
149 | off = SPDLOG_LEVEL_OFF, |
150 | }; |
151 | |
152 | #if !defined(SPDLOG_LEVEL_NAMES) |
153 | #define SPDLOG_LEVEL_NAMES \ |
154 | { \ |
155 | "trace", "debug", "info", "warning", "error", "critical", "off" \ |
156 | } |
157 | #endif |
158 | |
159 | #if !defined(SPDLOG_SHORT_LEVEL_NAMES) |
160 | |
161 | #define SPDLOG_SHORT_LEVEL_NAMES \ |
162 | { \ |
163 | "T", "D", "I", "W", "E", "C", "O" \ |
164 | } |
165 | #endif |
166 | |
167 | string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT; |
168 | const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT; |
169 | spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT; |
170 | |
171 | using level_hasher = std::hash<int>; |
172 | } // namespace level |
173 | |
174 | // |
175 | // Color mode used by sinks with color support. |
176 | // |
177 | enum class color_mode |
178 | { |
179 | always, |
180 | automatic, |
181 | never |
182 | }; |
183 | |
184 | // |
185 | // Pattern time - specific time getting to use for pattern_formatter. |
186 | // local time by default |
187 | // |
188 | enum class pattern_time_type |
189 | { |
190 | local, // log localtime |
191 | utc // log utc |
192 | }; |
193 | |
194 | // |
195 | // Log exception |
196 | // |
197 | class spdlog_ex : public std::exception |
198 | { |
199 | public: |
200 | explicit spdlog_ex(std::string msg); |
201 | spdlog_ex(const std::string &msg, int last_errno); |
202 | const char *what() const SPDLOG_NOEXCEPT override; |
203 | |
204 | private: |
205 | std::string msg_; |
206 | }; |
207 | |
208 | struct source_loc |
209 | { |
210 | SPDLOG_CONSTEXPR source_loc() = default; |
211 | SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in) |
212 | : filename{filename_in} |
213 | , line{line_in} |
214 | , funcname{funcname_in} |
215 | {} |
216 | |
217 | SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT |
218 | { |
219 | return line == 0; |
220 | } |
221 | const char *filename{nullptr}; |
222 | int line{0}; |
223 | const char *funcname{nullptr}; |
224 | }; |
225 | |
226 | namespace details { |
227 | // make_unique support for pre c++14 |
228 | |
229 | #if __cplusplus >= 201402L // C++14 and beyond |
230 | using std::make_unique; |
231 | #else |
232 | template<typename T, typename... Args> |
233 | std::unique_ptr<T> make_unique(Args &&... args) |
234 | { |
235 | static_assert(!std::is_array<T>::value, "arrays not supported" ); |
236 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
237 | } |
238 | #endif |
239 | } // namespace details |
240 | |
241 | } // namespace spdlog |
242 | |
243 | #ifdef SPDLOG_HEADER_ONLY |
244 | #include "common-inl.h" |
245 | #endif |
246 | |