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. #endif
  101. namespace spdlog {
  102. class formatter;
  103. namespace sinks {
  104. class sink;
  105. }
  106. #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
  107. using filename_t = std::wstring;
  108. // allow macro expansion to occur in SPDLOG_FILENAME_T
  109. # define SPDLOG_FILENAME_T_INNER(s) L##s
  110. # define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s)
  111. #else
  112. using filename_t = std::string;
  113. # define SPDLOG_FILENAME_T(s) s
  114. #endif
  115. using log_clock = std::chrono::system_clock;
  116. using sink_ptr = std::shared_ptr<sinks::sink>;
  117. using sinks_init_list = std::initializer_list<sink_ptr>;
  118. using err_handler = std::function<void(const std::string &err_msg)>;
  119. #ifdef SPDLOG_USE_STD_FORMAT
  120. namespace fmt_lib = std;
  121. using string_view_t = std::string_view;
  122. using memory_buf_t = std::string;
  123. template<typename... Args>
  124. # if __cpp_lib_format >= 202207L
  125. using format_string_t = std::format_string<Args...>;
  126. # else
  127. using format_string_t = std::string_view;
  128. # endif
  129. template<class T, class Char = char>
  130. struct is_convertible_to_basic_format_string : std::integral_constant<bool, std::is_convertible<T, std::basic_string_view<Char>>::value>
  131. {};
  132. # if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
  133. using wstring_view_t = std::wstring_view;
  134. using wmemory_buf_t = std::wstring;
  135. template<typename... Args>
  136. # if __cpp_lib_format >= 202207L
  137. using wformat_string_t = std::wformat_string<Args...>;
  138. # else
  139. using wformat_string_t = std::wstring_view;
  140. # endif
  141. # endif
  142. # define SPDLOG_BUF_TO_STRING(x) x
  143. #else // use fmt lib instead of std::format
  144. namespace fmt_lib = fmt;
  145. using string_view_t = fmt::basic_string_view<char>;
  146. using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
  147. template<typename... Args>
  148. using format_string_t = fmt::format_string<Args...>;
  149. template<class T>
  150. using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  151. template<typename Char>
  152. # if FMT_VERSION >= 90101
  153. using fmt_runtime_string = fmt::runtime_format_string<Char>;
  154. # else
  155. using fmt_runtime_string = fmt::basic_runtime<Char>;
  156. # endif
  157. // clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the condition from basic_format_string here,
  158. // in addition, fmt::basic_runtime<Char> is only convertible to basic_format_string<Char> but not basic_string_view<Char>
  159. template<class T, class Char = char>
  160. struct is_convertible_to_basic_format_string
  161. : std::integral_constant<bool,
  162. std::is_convertible<T, fmt::basic_string_view<Char>>::value || std::is_same<remove_cvref_t<T>, fmt_runtime_string<Char>>::value>
  163. {};
  164. # if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
  165. using wstring_view_t = fmt::basic_string_view<wchar_t>;
  166. using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
  167. template<typename... Args>
  168. using wformat_string_t = fmt::wformat_string<Args...>;
  169. # endif
  170. # define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
  171. #endif
  172. #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
  173. # ifndef _WIN32
  174. # error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
  175. # endif // _WIN32
  176. #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
  177. template<class T>
  178. struct is_convertible_to_any_format_string : std::integral_constant<bool, is_convertible_to_basic_format_string<T, char>::value ||
  179. is_convertible_to_basic_format_string<T, wchar_t>::value>
  180. {};
  181. #if defined(SPDLOG_NO_ATOMIC_LEVELS)
  182. using level_t = details::null_atomic_int;
  183. #else
  184. using level_t = std::atomic<int>;
  185. #endif
  186. #define SPDLOG_LEVEL_TRACE 0
  187. #define SPDLOG_LEVEL_DEBUG 1
  188. #define SPDLOG_LEVEL_INFO 2
  189. #define SPDLOG_LEVEL_WARN 3
  190. #define SPDLOG_LEVEL_ERROR 4
  191. #define SPDLOG_LEVEL_CRITICAL 5
  192. #define SPDLOG_LEVEL_OFF 6
  193. #if !defined(SPDLOG_ACTIVE_LEVEL)
  194. # define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
  195. #endif
  196. // Log level enum
  197. namespace level {
  198. enum level_enum : int
  199. {
  200. trace = SPDLOG_LEVEL_TRACE,
  201. debug = SPDLOG_LEVEL_DEBUG,
  202. info = SPDLOG_LEVEL_INFO,
  203. warn = SPDLOG_LEVEL_WARN,
  204. err = SPDLOG_LEVEL_ERROR,
  205. critical = SPDLOG_LEVEL_CRITICAL,
  206. off = SPDLOG_LEVEL_OFF,
  207. n_levels
  208. };
  209. #define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
  210. #define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
  211. #define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
  212. #define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
  213. #define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
  214. #define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
  215. #define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
  216. #if !defined(SPDLOG_LEVEL_NAMES)
  217. # define SPDLOG_LEVEL_NAMES \
  218. { \
  219. SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, \
  220. SPDLOG_LEVEL_NAME_CRITICAL, SPDLOG_LEVEL_NAME_OFF \
  221. }
  222. #endif
  223. #if !defined(SPDLOG_SHORT_LEVEL_NAMES)
  224. # define SPDLOG_SHORT_LEVEL_NAMES \
  225. { \
  226. "T", "D", "I", "W", "E", "C", "O" \
  227. }
  228. #endif
  229. SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
  230. SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
  231. SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
  232. } // namespace level
  233. //
  234. // Color mode used by sinks with color support.
  235. //
  236. enum class color_mode
  237. {
  238. always,
  239. automatic,
  240. never
  241. };
  242. //
  243. // Pattern time - specific time getting to use for pattern_formatter.
  244. // local time by default
  245. //
  246. enum class pattern_time_type
  247. {
  248. local, // log localtime
  249. utc // log utc
  250. };
  251. //
  252. // Log exception
  253. //
  254. class SPDLOG_API spdlog_ex : public std::exception
  255. {
  256. public:
  257. explicit spdlog_ex(std::string msg);
  258. spdlog_ex(const std::string &msg, int last_errno);
  259. const char *what() const SPDLOG_NOEXCEPT override;
  260. private:
  261. std::string msg_;
  262. };
  263. [[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno);
  264. [[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg);
  265. struct source_loc
  266. {
  267. SPDLOG_CONSTEXPR source_loc() = default;
  268. SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
  269. : filename{filename_in}
  270. , line{line_in}
  271. , funcname{funcname_in}
  272. {}
  273. SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT
  274. {
  275. return line == 0;
  276. }
  277. const char *filename{nullptr};
  278. int line{0};
  279. const char *funcname{nullptr};
  280. };
  281. struct file_event_handlers
  282. {
  283. file_event_handlers()
  284. : before_open(nullptr)
  285. , after_open(nullptr)
  286. , before_close(nullptr)
  287. , after_close(nullptr)
  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. // to_string_view
  296. SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEXCEPT
  297. {
  298. return spdlog::string_view_t{buf.data(), buf.size()};
  299. }
  300. SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(spdlog::string_view_t str) SPDLOG_NOEXCEPT
  301. {
  302. return str;
  303. }
  304. #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
  305. SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) SPDLOG_NOEXCEPT
  306. {
  307. return spdlog::wstring_view_t{buf.data(), buf.size()};
  308. }
  309. SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) SPDLOG_NOEXCEPT
  310. {
  311. return str;
  312. }
  313. #endif
  314. #ifndef SPDLOG_USE_STD_FORMAT
  315. template<typename T, typename... Args>
  316. inline fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt)
  317. {
  318. return fmt;
  319. }
  320. #elif __cpp_lib_format >= 202207L
  321. template<typename T, typename... Args>
  322. SPDLOG_CONSTEXPR_FUNC std::basic_string_view<T> to_string_view(std::basic_format_string<T, Args...> fmt) SPDLOG_NOEXCEPT
  323. {
  324. return fmt.get();
  325. }
  326. #endif
  327. // make_unique support for pre c++14
  328. #if __cplusplus >= 201402L // C++14 and beyond
  329. using std::enable_if_t;
  330. using std::make_unique;
  331. #else
  332. template<bool B, class T = void>
  333. using enable_if_t = typename std::enable_if<B, T>::type;
  334. template<typename T, typename... Args>
  335. std::unique_ptr<T> make_unique(Args &&...args)
  336. {
  337. static_assert(!std::is_array<T>::value, "arrays not supported");
  338. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  339. }
  340. #endif
  341. // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
  342. template<typename T, typename U, enable_if_t<!std::is_same<T, U>::value, int> = 0>
  343. constexpr T conditional_static_cast(U value)
  344. {
  345. return static_cast<T>(value);
  346. }
  347. template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
  348. constexpr T conditional_static_cast(U value)
  349. {
  350. return value;
  351. }
  352. } // namespace details
  353. } // namespace spdlog
  354. #ifdef SPDLOG_HEADER_ONLY
  355. # include "common-inl.h"
  356. #endif