Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-09 08:47:32

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 #include <spdlog/details/null_mutex.h>
0007 #include <spdlog/tweakme.h>
0008 
0009 #include <atomic>
0010 #include <chrono>
0011 #include <cstdio>
0012 #include <exception>
0013 #include <functional>
0014 #include <initializer_list>
0015 #include <memory>
0016 #include <string>
0017 #include <type_traits>
0018 
0019 #ifdef SPDLOG_USE_STD_FORMAT
0020     #include <version>
0021     #if __cpp_lib_format >= 202207L
0022         #include <format>
0023     #else
0024         #include <string_view>
0025     #endif
0026 #endif
0027 
0028 #ifdef SPDLOG_COMPILED_LIB
0029     #undef SPDLOG_HEADER_ONLY
0030     #if defined(SPDLOG_SHARED_LIB)
0031         #if defined(_WIN32)
0032             #ifdef spdlog_EXPORTS
0033                 #define SPDLOG_API __declspec(dllexport)
0034             #else  // !spdlog_EXPORTS
0035                 #define SPDLOG_API __declspec(dllimport)
0036             #endif
0037         #else  // !defined(_WIN32)
0038             #define SPDLOG_API __attribute__((visibility("default")))
0039         #endif
0040     #else  // !defined(SPDLOG_SHARED_LIB)
0041         #define SPDLOG_API
0042     #endif
0043     #define SPDLOG_INLINE
0044 #else  // !defined(SPDLOG_COMPILED_LIB)
0045     #define SPDLOG_API
0046     #define SPDLOG_HEADER_ONLY
0047     #define SPDLOG_INLINE inline
0048 #endif  // #ifdef SPDLOG_COMPILED_LIB
0049 
0050 #include <spdlog/fmt/fmt.h>
0051 
0052 #if !defined(SPDLOG_USE_STD_FORMAT) && \
0053     FMT_VERSION >= 80000  // backward compatibility with fmt versions older than 8
0054     #define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
0055     #define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
0056     #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
0057         #include <spdlog/fmt/xchar.h>
0058     #endif
0059 #else
0060     #define SPDLOG_FMT_RUNTIME(format_string) format_string
0061     #define SPDLOG_FMT_STRING(format_string) format_string
0062 #endif
0063 
0064 // visual studio up to 2013 does not support noexcept nor constexpr
0065 #if defined(_MSC_VER) && (_MSC_VER < 1900)
0066     #define SPDLOG_NOEXCEPT _NOEXCEPT
0067     #define SPDLOG_CONSTEXPR
0068 #else
0069     #define SPDLOG_NOEXCEPT noexcept
0070     #define SPDLOG_CONSTEXPR constexpr
0071 #endif
0072 
0073 // If building with std::format, can just use constexpr, otherwise if building with fmt
0074 // SPDLOG_CONSTEXPR_FUNC needs to be set the same as FMT_CONSTEXPR to avoid situations where
0075 // a constexpr function in spdlog could end up calling a non-constexpr function in fmt
0076 // depending on the compiler
0077 // If fmt determines it can't use constexpr, we should inline the function instead
0078 #ifdef SPDLOG_USE_STD_FORMAT
0079     #define SPDLOG_CONSTEXPR_FUNC constexpr
0080 #else  // Being built with fmt
0081     #if FMT_USE_CONSTEXPR
0082         #define SPDLOG_CONSTEXPR_FUNC FMT_CONSTEXPR
0083     #else
0084         #define SPDLOG_CONSTEXPR_FUNC inline
0085     #endif
0086 #endif
0087 
0088 #if defined(__GNUC__) || defined(__clang__)
0089     #define SPDLOG_DEPRECATED __attribute__((deprecated))
0090 #elif defined(_MSC_VER)
0091     #define SPDLOG_DEPRECATED __declspec(deprecated)
0092 #else
0093     #define SPDLOG_DEPRECATED
0094 #endif
0095 
0096 // disable thread local on msvc 2013
0097 #ifndef SPDLOG_NO_TLS
0098     #if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
0099         #define SPDLOG_NO_TLS 1
0100     #endif
0101 #endif
0102 
0103 #ifndef SPDLOG_FUNCTION
0104     #define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
0105 #endif
0106 
0107 #ifdef SPDLOG_NO_EXCEPTIONS
0108     #define SPDLOG_TRY
0109     #define SPDLOG_THROW(ex)                               \
0110         do {                                               \
0111             printf("spdlog fatal error: %s\n", ex.what()); \
0112             std::abort();                                  \
0113         } while (0)
0114     #define SPDLOG_CATCH_STD
0115 #else
0116     #define SPDLOG_TRY try
0117     #define SPDLOG_THROW(ex) throw(ex)
0118     #define SPDLOG_CATCH_STD             \
0119         catch (const std::exception &) { \
0120         }
0121 #endif
0122 
0123 namespace spdlog {
0124 
0125 class formatter;
0126 
0127 namespace sinks {
0128 class sink;
0129 }
0130 
0131 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
0132 using filename_t = std::wstring;
0133     // allow macro expansion to occur in SPDLOG_FILENAME_T
0134     #define SPDLOG_FILENAME_T_INNER(s) L##s
0135     #define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s)
0136 #else
0137 using filename_t = std::string;
0138     #define SPDLOG_FILENAME_T(s) s
0139 #endif
0140 
0141 using log_clock = std::chrono::system_clock;
0142 using sink_ptr = std::shared_ptr<sinks::sink>;
0143 using sinks_init_list = std::initializer_list<sink_ptr>;
0144 using err_handler = std::function<void(const std::string &err_msg)>;
0145 #ifdef SPDLOG_USE_STD_FORMAT
0146 namespace fmt_lib = std;
0147 
0148 using string_view_t = std::string_view;
0149 using memory_buf_t = std::string;
0150 
0151 template <typename... Args>
0152     #if __cpp_lib_format >= 202207L
0153 using format_string_t = std::format_string<Args...>;
0154     #else
0155 using format_string_t = std::string_view;
0156     #endif
0157 
0158 template <class T, class Char = char>
0159 struct is_convertible_to_basic_format_string
0160     : std::integral_constant<bool, std::is_convertible<T, std::basic_string_view<Char>>::value> {};
0161 
0162     #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
0163 using wstring_view_t = std::wstring_view;
0164 using wmemory_buf_t = std::wstring;
0165 
0166 template <typename... Args>
0167         #if __cpp_lib_format >= 202207L
0168 using wformat_string_t = std::wformat_string<Args...>;
0169         #else
0170 using wformat_string_t = std::wstring_view;
0171         #endif
0172     #endif
0173     #define SPDLOG_BUF_TO_STRING(x) x
0174 #else  // use fmt lib instead of std::format
0175 namespace fmt_lib = fmt;
0176 
0177 using string_view_t = fmt::basic_string_view<char>;
0178 using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
0179 
0180 template <typename... Args>
0181 using format_string_t = fmt::format_string<Args...>;
0182 
0183 template <class T>
0184 using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
0185 
0186 template <typename Char>
0187     #if FMT_VERSION >= 90101
0188 using fmt_runtime_string = fmt::runtime_format_string<Char>;
0189     #else
0190 using fmt_runtime_string = fmt::basic_runtime<Char>;
0191     #endif
0192 
0193 // clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the
0194 // condition from basic_format_string here, in addition, fmt::basic_runtime<Char> is only
0195 // convertible to basic_format_string<Char> but not basic_string_view<Char>
0196 template <class T, class Char = char>
0197 struct is_convertible_to_basic_format_string
0198     : std::integral_constant<bool,
0199                              std::is_convertible<T, fmt::basic_string_view<Char>>::value ||
0200                                  std::is_same<remove_cvref_t<T>, fmt_runtime_string<Char>>::value> {
0201 };
0202 
0203     #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
0204 using wstring_view_t = fmt::basic_string_view<wchar_t>;
0205 using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
0206 
0207 template <typename... Args>
0208 using wformat_string_t = fmt::wformat_string<Args...>;
0209     #endif
0210     #define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
0211 #endif
0212 
0213 #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
0214     #ifndef _WIN32
0215         #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
0216     #endif  // _WIN32
0217 #endif      // SPDLOG_WCHAR_TO_UTF8_SUPPORT
0218 
0219 template <class T>
0220 struct is_convertible_to_any_format_string
0221     : std::integral_constant<bool,
0222                              is_convertible_to_basic_format_string<T, char>::value ||
0223                                  is_convertible_to_basic_format_string<T, wchar_t>::value> {};
0224 
0225 #if defined(SPDLOG_NO_ATOMIC_LEVELS)
0226 using level_t = details::null_atomic_int;
0227 #else
0228 using level_t = std::atomic<int>;
0229 #endif
0230 
0231 #define SPDLOG_LEVEL_TRACE 0
0232 #define SPDLOG_LEVEL_DEBUG 1
0233 #define SPDLOG_LEVEL_INFO 2
0234 #define SPDLOG_LEVEL_WARN 3
0235 #define SPDLOG_LEVEL_ERROR 4
0236 #define SPDLOG_LEVEL_CRITICAL 5
0237 #define SPDLOG_LEVEL_OFF 6
0238 
0239 #if !defined(SPDLOG_ACTIVE_LEVEL)
0240     #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
0241 #endif
0242 
0243 // Log level enum
0244 namespace level {
0245 enum level_enum : int {
0246     trace = SPDLOG_LEVEL_TRACE,
0247     debug = SPDLOG_LEVEL_DEBUG,
0248     info = SPDLOG_LEVEL_INFO,
0249     warn = SPDLOG_LEVEL_WARN,
0250     err = SPDLOG_LEVEL_ERROR,
0251     critical = SPDLOG_LEVEL_CRITICAL,
0252     off = SPDLOG_LEVEL_OFF,
0253     n_levels
0254 };
0255 
0256 #define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
0257 #define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
0258 #define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
0259 #define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
0260 #define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
0261 #define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
0262 #define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
0263 
0264 #if !defined(SPDLOG_LEVEL_NAMES)
0265     #define SPDLOG_LEVEL_NAMES                                                                  \
0266         {                                                                                       \
0267             SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO,           \
0268                 SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL, \
0269                 SPDLOG_LEVEL_NAME_OFF                                                           \
0270         }
0271 #endif
0272 
0273 #if !defined(SPDLOG_SHORT_LEVEL_NAMES)
0274 
0275     #define SPDLOG_SHORT_LEVEL_NAMES \
0276         { "T", "D", "I", "W", "E", "C", "O" }
0277 #endif
0278 
0279 SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
0280 SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
0281 SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
0282 
0283 }  // namespace level
0284 
0285 //
0286 // Color mode used by sinks with color support.
0287 //
0288 enum class color_mode { always, automatic, never };
0289 
0290 //
0291 // Pattern time - specific time getting to use for pattern_formatter.
0292 // local time by default
0293 //
0294 enum class pattern_time_type {
0295     local,  // log localtime
0296     utc     // log utc
0297 };
0298 
0299 //
0300 // Log exception
0301 //
0302 class SPDLOG_API spdlog_ex : public std::exception {
0303 public:
0304     explicit spdlog_ex(std::string msg);
0305     spdlog_ex(const std::string &msg, int last_errno);
0306     const char *what() const SPDLOG_NOEXCEPT override;
0307 
0308 private:
0309     std::string msg_;
0310 };
0311 
0312 [[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno);
0313 [[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg);
0314 
0315 struct source_loc {
0316     SPDLOG_CONSTEXPR source_loc() = default;
0317     SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in)
0318         : filename{filename_in},
0319           line{line_in},
0320           funcname{funcname_in} {}
0321 
0322     SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT { return line <= 0; }
0323     const char *filename{nullptr};
0324     int line{0};
0325     const char *funcname{nullptr};
0326 };
0327 
0328 struct file_event_handlers {
0329     file_event_handlers()
0330         : before_open(nullptr),
0331           after_open(nullptr),
0332           before_close(nullptr),
0333           after_close(nullptr) {}
0334 
0335     std::function<void(const filename_t &filename)> before_open;
0336     std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
0337     std::function<void(const filename_t &filename, std::FILE *file_stream)> before_close;
0338     std::function<void(const filename_t &filename)> after_close;
0339 };
0340 
0341 namespace details {
0342 
0343 // to_string_view
0344 
0345 SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(const memory_buf_t &buf)
0346     SPDLOG_NOEXCEPT {
0347     return spdlog::string_view_t{buf.data(), buf.size()};
0348 }
0349 
0350 SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(spdlog::string_view_t str)
0351     SPDLOG_NOEXCEPT {
0352     return str;
0353 }
0354 
0355 #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
0356 SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf)
0357     SPDLOG_NOEXCEPT {
0358     return spdlog::wstring_view_t{buf.data(), buf.size()};
0359 }
0360 
0361 SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str)
0362     SPDLOG_NOEXCEPT {
0363     return str;
0364 }
0365 #endif
0366 
0367 #ifndef SPDLOG_USE_STD_FORMAT
0368 template <typename T, typename... Args>
0369 inline fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt) {
0370     return fmt;
0371 }
0372 #elif __cpp_lib_format >= 202207L
0373 template <typename T, typename... Args>
0374 SPDLOG_CONSTEXPR_FUNC std::basic_string_view<T> to_string_view(
0375     std::basic_format_string<T, Args...> fmt) SPDLOG_NOEXCEPT {
0376     return fmt.get();
0377 }
0378 #endif
0379 
0380 // make_unique support for pre c++14
0381 #if __cplusplus >= 201402L  // C++14 and beyond
0382 using std::enable_if_t;
0383 using std::make_unique;
0384 #else
0385 template <bool B, class T = void>
0386 using enable_if_t = typename std::enable_if<B, T>::type;
0387 
0388 template <typename T, typename... Args>
0389 std::unique_ptr<T> make_unique(Args &&...args) {
0390     static_assert(!std::is_array<T>::value, "arrays not supported");
0391     return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
0392 }
0393 #endif
0394 
0395 // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
0396 template <typename T, typename U, enable_if_t<!std::is_same<T, U>::value, int> = 0>
0397 constexpr T conditional_static_cast(U value) {
0398     return static_cast<T>(value);
0399 }
0400 
0401 template <typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
0402 constexpr T conditional_static_cast(U value) {
0403     return value;
0404 }
0405 
0406 }  // namespace details
0407 }  // namespace spdlog
0408 
0409 #ifdef SPDLOG_HEADER_ONLY
0410     #include "common-inl.h"
0411 #endif