You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.h 13 kB

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