Warning, file /include/spdlog/common.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004 #pragma once
0005
0006 #include <spdlog/tweakme.h>
0007 #include <spdlog/details/null_mutex.h>
0008
0009 #include <atomic>
0010 #include <chrono>
0011 #include <initializer_list>
0012 #include <memory>
0013 #include <exception>
0014 #include <string>
0015 #include <type_traits>
0016 #include <functional>
0017 #include <cstdio>
0018
0019 #ifdef SPDLOG_USE_STD_FORMAT
0020 # include <string_view>
0021 #endif
0022
0023 #ifdef SPDLOG_COMPILED_LIB
0024 # undef SPDLOG_HEADER_ONLY
0025 # if defined(SPDLOG_SHARED_LIB)
0026 # if defined(_WIN32)
0027 # ifdef spdlog_EXPORTS
0028 # define SPDLOG_API __declspec(dllexport)
0029 # else
0030 # define SPDLOG_API __declspec(dllimport)
0031 # endif
0032 # else
0033 # define SPDLOG_API __attribute__((visibility("default")))
0034 # endif
0035 # else
0036 # define SPDLOG_API
0037 # endif
0038 # define SPDLOG_INLINE
0039 #else
0040 # define SPDLOG_API
0041 # define SPDLOG_HEADER_ONLY
0042 # define SPDLOG_INLINE inline
0043 #endif
0044
0045 #include <spdlog/fmt/fmt.h>
0046
0047 #if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION >= 80000
0048 # define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
0049 # define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
0050 # if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
0051 # include <spdlog/fmt/xchar.h>
0052 # endif
0053 #else
0054 # define SPDLOG_FMT_RUNTIME(format_string) format_string
0055 # define SPDLOG_FMT_STRING(format_string) format_string
0056 #endif
0057
0058
0059 #if defined(_MSC_VER) && (_MSC_VER < 1900)
0060 # define SPDLOG_NOEXCEPT _NOEXCEPT
0061 # define SPDLOG_CONSTEXPR
0062 # define SPDLOG_CONSTEXPR_FUNC
0063 #else
0064 # define SPDLOG_NOEXCEPT noexcept
0065 # define SPDLOG_CONSTEXPR constexpr
0066 # if __cplusplus >= 201402L
0067 # define SPDLOG_CONSTEXPR_FUNC constexpr
0068 # else
0069 # define SPDLOG_CONSTEXPR_FUNC
0070 # endif
0071 #endif
0072
0073 #if defined(__GNUC__) || defined(__clang__)
0074 # define SPDLOG_DEPRECATED __attribute__((deprecated))
0075 #elif defined(_MSC_VER)
0076 # define SPDLOG_DEPRECATED __declspec(deprecated)
0077 #else
0078 # define SPDLOG_DEPRECATED
0079 #endif
0080
0081
0082 #ifndef SPDLOG_NO_TLS
0083 # if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
0084 # define SPDLOG_NO_TLS 1
0085 # endif
0086 #endif
0087
0088 #ifndef SPDLOG_FUNCTION
0089 # define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
0090 #endif
0091
0092 #ifdef SPDLOG_NO_EXCEPTIONS
0093 # define SPDLOG_TRY
0094 # define SPDLOG_THROW(ex) \
0095 do \
0096 { \
0097 printf("spdlog fatal error: %s\n", ex.what()); \
0098 std::abort(); \
0099 } while (0)
0100 # define SPDLOG_CATCH_STD
0101 #else
0102 # define SPDLOG_TRY try
0103 # define SPDLOG_THROW(ex) throw(ex)
0104 # define SPDLOG_CATCH_STD \
0105 catch (const std::exception &) {}
0106 #endif
0107
0108 namespace spdlog {
0109
0110 class formatter;
0111
0112 namespace sinks {
0113 class sink;
0114 }
0115
0116 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
0117 using filename_t = std::wstring;
0118
0119 # define SPDLOG_FILENAME_T_INNER(s) L##s
0120 # define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s)
0121 #else
0122 using filename_t = std::string;
0123 # define SPDLOG_FILENAME_T(s) s
0124 #endif
0125
0126 using log_clock = std::chrono::system_clock;
0127 using sink_ptr = std::shared_ptr<sinks::sink>;
0128 using sinks_init_list = std::initializer_list<sink_ptr>;
0129 using err_handler = std::function<void(const std::string &err_msg)>;
0130 #ifdef SPDLOG_USE_STD_FORMAT
0131 namespace fmt_lib = std;
0132
0133 using string_view_t = std::string_view;
0134 using memory_buf_t = std::string;
0135
0136 template<typename... Args>
0137 using format_string_t = std::string_view;
0138
0139 template<class T, class Char = char>
0140 struct is_convertible_to_basic_format_string : std::integral_constant<bool, std::is_convertible<T, std::basic_string_view<Char>>::value>
0141 {};
0142
0143 # if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
0144 using wstring_view_t = std::wstring_view;
0145 using wmemory_buf_t = std::wstring;
0146
0147 template<typename... Args>
0148 using wformat_string_t = std::wstring_view;
0149 # endif
0150 # define SPDLOG_BUF_TO_STRING(x) x
0151 #else
0152 namespace fmt_lib = fmt;
0153
0154 using string_view_t = fmt::basic_string_view<char>;
0155 using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
0156
0157 template<typename... Args>
0158 using format_string_t = fmt::format_string<Args...>;
0159
0160 template<class T>
0161 using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
0162
0163
0164
0165 template<class T, class Char = char>
0166 struct is_convertible_to_basic_format_string
0167 : std::integral_constant<bool,
0168 std::is_convertible<T, fmt::basic_string_view<Char>>::value || std::is_same<remove_cvref_t<T>, fmt::basic_runtime<Char>>::value>
0169 {};
0170
0171 # if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
0172 using wstring_view_t = fmt::basic_string_view<wchar_t>;
0173 using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
0174
0175 template<typename... Args>
0176 using wformat_string_t = fmt::wformat_string<Args...>;
0177 # endif
0178 # define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
0179 #endif
0180
0181 #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
0182 # ifndef _WIN32
0183 # error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
0184 # endif
0185 #endif
0186
0187 template<class T>
0188 struct is_convertible_to_any_format_string : std::integral_constant<bool, is_convertible_to_basic_format_string<T, char>::value ||
0189 is_convertible_to_basic_format_string<T, wchar_t>::value>
0190 {};
0191
0192 #if defined(SPDLOG_NO_ATOMIC_LEVELS)
0193 using level_t = details::null_atomic_int;
0194 #else
0195 using level_t = std::atomic<int>;
0196 #endif
0197
0198 #define SPDLOG_LEVEL_TRACE 0
0199 #define SPDLOG_LEVEL_DEBUG 1
0200 #define SPDLOG_LEVEL_INFO 2
0201 #define SPDLOG_LEVEL_WARN 3
0202 #define SPDLOG_LEVEL_ERROR 4
0203 #define SPDLOG_LEVEL_CRITICAL 5
0204 #define SPDLOG_LEVEL_OFF 6
0205
0206 #if !defined(SPDLOG_ACTIVE_LEVEL)
0207 # define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
0208 #endif
0209
0210
0211 namespace level {
0212 enum level_enum : int
0213 {
0214 trace = SPDLOG_LEVEL_TRACE,
0215 debug = SPDLOG_LEVEL_DEBUG,
0216 info = SPDLOG_LEVEL_INFO,
0217 warn = SPDLOG_LEVEL_WARN,
0218 err = SPDLOG_LEVEL_ERROR,
0219 critical = SPDLOG_LEVEL_CRITICAL,
0220 off = SPDLOG_LEVEL_OFF,
0221 n_levels
0222 };
0223
0224 #define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
0225 #define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
0226 #define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
0227 #define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
0228 #define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
0229 #define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
0230 #define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
0231
0232 #if !defined(SPDLOG_LEVEL_NAMES)
0233 # define SPDLOG_LEVEL_NAMES \
0234 { \
0235 SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, \
0236 SPDLOG_LEVEL_NAME_CRITICAL, SPDLOG_LEVEL_NAME_OFF \
0237 }
0238 #endif
0239
0240 #if !defined(SPDLOG_SHORT_LEVEL_NAMES)
0241
0242 # define SPDLOG_SHORT_LEVEL_NAMES \
0243 { \
0244 "T", "D", "I", "W", "E", "C", "O" \
0245 }
0246 #endif
0247
0248 SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
0249 SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
0250 SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
0251
0252 }
0253
0254
0255
0256
0257 enum class color_mode
0258 {
0259 always,
0260 automatic,
0261 never
0262 };
0263
0264
0265
0266
0267
0268 enum class pattern_time_type
0269 {
0270 local,
0271 utc
0272 };
0273
0274
0275
0276
0277 class SPDLOG_API spdlog_ex : public std::exception
0278 {
0279 public:
0280 explicit spdlog_ex(std::string msg);
0281 spdlog_ex(const std::string &msg, int last_errno);
0282 const char *what() const SPDLOG_NOEXCEPT override;
0283
0284 private:
0285 std::string msg_;
0286 };
0287
0288 [[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno);
0289 [[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg);
0290
0291 struct source_loc
0292 {
0293 SPDLOG_CONSTEXPR source_loc() = default;
0294 SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
0295 : filename{filename_in}
0296 , line{line_in}
0297 , funcname{funcname_in}
0298 {}
0299
0300 SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT
0301 {
0302 return line == 0;
0303 }
0304 const char *filename{nullptr};
0305 int line{0};
0306 const char *funcname{nullptr};
0307 };
0308
0309 struct file_event_handlers
0310 {
0311 file_event_handlers()
0312 : before_open(nullptr)
0313 , after_open(nullptr)
0314 , before_close(nullptr)
0315 , after_close(nullptr)
0316 {}
0317
0318 std::function<void(const filename_t &filename)> before_open;
0319 std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
0320 std::function<void(const filename_t &filename, std::FILE *file_stream)> before_close;
0321 std::function<void(const filename_t &filename)> after_close;
0322 };
0323
0324 namespace details {
0325
0326
0327
0328 #if __cplusplus >= 201402L
0329 using std::enable_if_t;
0330 using std::make_unique;
0331 #else
0332 template<bool B, class T = void>
0333 using enable_if_t = typename std::enable_if<B, T>::type;
0334
0335 template<typename T, typename... Args>
0336 std::unique_ptr<T> make_unique(Args &&... args)
0337 {
0338 static_assert(!std::is_array<T>::value, "arrays not supported");
0339 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
0340 }
0341 #endif
0342
0343
0344 template<typename T, typename U, enable_if_t<!std::is_same<T, U>::value, int> = 0>
0345 constexpr T conditional_static_cast(U value)
0346 {
0347 return static_cast<T>(value);
0348 }
0349
0350 template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
0351 constexpr T conditional_static_cast(U value)
0352 {
0353 return value;
0354 }
0355
0356 }
0357 }
0358
0359 #ifdef SPDLOG_HEADER_ONLY
0360 # include "common-inl.h"
0361 #endif