common.h 12 KB

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