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

246 line
6.6KB

  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. #ifdef _WIN32
  15. #ifndef NOMINMAX
  16. #define NOMINMAX // prevent windows redefining min/max
  17. #endif
  18. #ifndef WIN32_LEAN_AND_MEAN
  19. #define WIN32_LEAN_AND_MEAN
  20. #endif
  21. #include <windows.h>
  22. #endif //_WIN32
  23. #ifdef SPDLOG_COMPILED_LIB
  24. #undef SPDLOG_HEADER_ONLY
  25. #define SPDLOG_INLINE
  26. #else
  27. #define SPDLOG_HEADER_ONLY
  28. #define SPDLOG_INLINE inline
  29. #endif
  30. #include "spdlog/fmt/fmt.h"
  31. // visual studio upto 2013 does not support noexcept nor constexpr
  32. #if defined(_MSC_VER) && (_MSC_VER < 1900)
  33. #define SPDLOG_NOEXCEPT _NOEXCEPT
  34. #define SPDLOG_CONSTEXPR
  35. #else
  36. #define SPDLOG_NOEXCEPT noexcept
  37. #define SPDLOG_CONSTEXPR constexpr
  38. #endif
  39. #if defined(__GNUC__) || defined(__clang__)
  40. #define SPDLOG_DEPRECATED __attribute__((deprecated))
  41. #elif defined(_MSC_VER)
  42. #define SPDLOG_DEPRECATED __declspec(deprecated)
  43. #else
  44. #define SPDLOG_DEPRECATED
  45. #endif
  46. // disable thread local on msvc 2013
  47. #ifndef SPDLOG_NO_TLS
  48. #if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
  49. #define SPDLOG_NO_TLS 1
  50. #endif
  51. #endif
  52. #ifndef SPDLOG_FUNCTION
  53. #define SPDLOG_FUNCTION __FUNCTION__
  54. #endif
  55. #ifdef SPDLOG_NO_EXCEPTIONS
  56. #define SPDLOG_TRY
  57. #define SPDLOG_THROW(ex) \
  58. do \
  59. { \
  60. printf("spdlog fatal error: %s\n", ex.what()); \
  61. std::abort(); \
  62. } while (0)
  63. #define SPDLOG_CATCH_ALL()
  64. #else
  65. #define SPDLOG_TRY try
  66. #define SPDLOG_THROW(ex) throw(ex)
  67. #define SPDLOG_CATCH_ALL() catch (...)
  68. #endif
  69. namespace spdlog {
  70. class formatter;
  71. namespace sinks {
  72. class sink;
  73. }
  74. #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
  75. using filename_t = std::wstring;
  76. #define SPDLOG_FILENAME_T(s) L##s
  77. #else
  78. using filename_t = std::string;
  79. #define SPDLOG_FILENAME_T(s) s
  80. #endif
  81. using log_clock = std::chrono::system_clock;
  82. using sink_ptr = std::shared_ptr<sinks::sink>;
  83. using sinks_init_list = std::initializer_list<sink_ptr>;
  84. using err_handler = std::function<void(const std::string &err_msg)>;
  85. using string_view_t = fmt::basic_string_view<char>;
  86. using wstring_view_t = fmt::basic_string_view<wchar_t>;
  87. using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
  88. #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
  89. #ifndef _WIN32
  90. #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
  91. #else
  92. template<typename T>
  93. struct is_convertible_to_wstring_view : std::is_convertible<T, wstring_view_t>
  94. {};
  95. #endif // _WIN32
  96. #else
  97. template<typename>
  98. struct is_convertible_to_wstring_view : std::false_type
  99. {};
  100. #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
  101. #if defined(SPDLOG_NO_ATOMIC_LEVELS)
  102. using level_t = details::null_atomic_int;
  103. #else
  104. using level_t = std::atomic<int>;
  105. #endif
  106. #define SPDLOG_LEVEL_TRACE 0
  107. #define SPDLOG_LEVEL_DEBUG 1
  108. #define SPDLOG_LEVEL_INFO 2
  109. #define SPDLOG_LEVEL_WARN 3
  110. #define SPDLOG_LEVEL_ERROR 4
  111. #define SPDLOG_LEVEL_CRITICAL 5
  112. #define SPDLOG_LEVEL_OFF 6
  113. #if !defined(SPDLOG_ACTIVE_LEVEL)
  114. #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
  115. #endif
  116. // Log level enum
  117. namespace level {
  118. enum level_enum
  119. {
  120. trace = SPDLOG_LEVEL_TRACE,
  121. debug = SPDLOG_LEVEL_DEBUG,
  122. info = SPDLOG_LEVEL_INFO,
  123. warn = SPDLOG_LEVEL_WARN,
  124. err = SPDLOG_LEVEL_ERROR,
  125. critical = SPDLOG_LEVEL_CRITICAL,
  126. off = SPDLOG_LEVEL_OFF,
  127. };
  128. #if !defined(SPDLOG_LEVEL_NAMES)
  129. #define SPDLOG_LEVEL_NAMES \
  130. { \
  131. "trace", "debug", "info", "warning", "error", "critical", "off" \
  132. }
  133. #endif
  134. #if !defined(SPDLOG_SHORT_LEVEL_NAMES)
  135. #define SPDLOG_SHORT_LEVEL_NAMES \
  136. { \
  137. "T", "D", "I", "W", "E", "C", "O" \
  138. }
  139. #endif
  140. string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
  141. const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
  142. spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
  143. using level_hasher = std::hash<int>;
  144. } // namespace level
  145. //
  146. // Color mode used by sinks with color support.
  147. //
  148. enum class color_mode
  149. {
  150. always,
  151. automatic,
  152. never
  153. };
  154. //
  155. // Pattern time - specific time getting to use for pattern_formatter.
  156. // local time by default
  157. //
  158. enum class pattern_time_type
  159. {
  160. local, // log localtime
  161. utc // log utc
  162. };
  163. //
  164. // Log exception
  165. //
  166. class spdlog_ex : public std::exception
  167. {
  168. public:
  169. explicit spdlog_ex(std::string msg);
  170. spdlog_ex(const std::string &msg, int last_errno);
  171. const char *what() const SPDLOG_NOEXCEPT override;
  172. private:
  173. std::string msg_;
  174. };
  175. struct source_loc
  176. {
  177. SPDLOG_CONSTEXPR source_loc() = default;
  178. SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
  179. : filename{filename_in}
  180. , line{line_in}
  181. , funcname{funcname_in}
  182. {}
  183. SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT
  184. {
  185. return line == 0;
  186. }
  187. const char *filename{nullptr};
  188. int line{0};
  189. const char *funcname{nullptr};
  190. };
  191. namespace details {
  192. // make_unique support for pre c++14
  193. #if __cplusplus >= 201402L // C++14 and beyond
  194. using std::make_unique;
  195. #else
  196. template<typename T, typename... Args>
  197. std::unique_ptr<T> make_unique(Args &&... args)
  198. {
  199. static_assert(!std::is_array<T>::value, "arrays not supported");
  200. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  201. }
  202. #endif
  203. } // namespace details
  204. } // namespace spdlog
  205. #ifdef SPDLOG_HEADER_ONLY
  206. #include "common-inl.h"
  207. #endif