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