common.h 13 KB

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