common.h 12 KB

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