spdlog/include/spdlog/common.h

307 lines
8.7 KiB
C
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2016-08-23 01:54:18 +08:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
2023-09-29 08:21:28 +08:00
2023-09-25 07:35:55 +08:00
#include <array>
2016-08-23 01:54:18 +08:00
#include <atomic>
#include <chrono>
2023-09-25 07:35:55 +08:00
#include <cstdint>
#include <cstdio>
#include <exception>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
2019-04-06 06:44:03 +08:00
#include <type_traits>
2023-09-16 06:31:40 +08:00
2023-09-29 05:20:26 +08:00
#include "details/null_mutex.h"
#include "source_loc.h"
2023-09-29 05:20:26 +08:00
#include "tweakme.h"
2023-09-16 06:31:40 +08:00
#if __has_include(<version>)
2023-09-25 07:35:55 +08:00
#include <version>
2023-09-16 06:31:40 +08:00
#endif
#ifdef SPDLOG_USE_STD_FORMAT
2023-09-25 07:35:55 +08:00
#if __cpp_lib_format >= 202207L
#include <format>
#else
#include <string_view>
#endif
#endif
#if defined(SPDLOG_SHARED_LIB)
2023-09-25 07:35:55 +08:00
#if defined(_WIN32)
#ifdef spdlog_EXPORTS
#define SPDLOG_API __declspec(dllexport)
2023-09-25 21:40:05 +08:00
#else // !spdlog_EXPORTS
2023-09-25 07:35:55 +08:00
#define SPDLOG_API __declspec(dllimport)
#endif
2023-09-25 21:40:05 +08:00
#else // !defined(_WIN32)
2023-09-25 07:35:55 +08:00
#define SPDLOG_API __attribute__((visibility("default")))
#endif
2023-09-25 21:40:05 +08:00
#else // !defined(SPDLOG_SHARED_LIB)
2023-09-25 07:35:55 +08:00
#define SPDLOG_API
#endif
2023-09-29 04:45:45 +08:00
#include "fmt/fmt.h"
2019-05-12 05:32:57 +08:00
#if !defined(SPDLOG_USE_STD_FORMAT) && \
2023-09-25 21:40:05 +08:00
FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8
2023-09-25 07:35:55 +08:00
#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
#if defined(SPDLOG_WCHAR_FILENAMES)
2023-09-29 04:45:45 +08:00
#include "fmt/xchar.h"
2023-09-25 07:35:55 +08:00
#endif
#else
2023-09-25 07:35:55 +08:00
#define SPDLOG_FMT_RUNTIME(format_string) format_string
2021-06-27 00:43:37 +08:00
#endif
#ifndef SPDLOG_FUNCTION
2023-09-25 07:35:55 +08:00
#define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
#endif
2019-08-19 00:46:28 +08:00
#ifdef SPDLOG_NO_EXCEPTIONS
2023-09-25 07:35:55 +08:00
#define SPDLOG_TRY
#define SPDLOG_THROW(ex) \
do { \
printf("spdlog fatal error: %s\n", ex.what()); \
std::abort(); \
2021-06-26 22:36:57 +08:00
} while (0)
2023-09-25 07:35:55 +08:00
#define SPDLOG_CATCH_STD
2019-08-19 00:46:28 +08:00
#else
2023-09-25 07:35:55 +08:00
#define SPDLOG_TRY try
#define SPDLOG_THROW(ex) throw(ex)
#define SPDLOG_CATCH_STD \
catch (const std::exception &) { \
}
2019-08-19 00:46:28 +08:00
#endif
2019-04-06 18:45:33 +08:00
namespace spdlog {
class formatter;
namespace sinks {
class sink;
}
2019-04-06 04:05:46 +08:00
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
2019-04-06 06:37:27 +08:00
using filename_t = std::wstring;
// allow macro expansion to occur in SPDLOG_FILENAME_T
2023-09-25 07:35:55 +08:00
#define SPDLOG_FILENAME_T_INNER(s) L##s
#define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s)
2019-04-06 04:05:46 +08:00
#else
2019-04-06 06:37:27 +08:00
using filename_t = std::string;
2023-09-25 07:35:55 +08:00
#define SPDLOG_FILENAME_T(s) s
2019-04-06 04:05:46 +08:00
#endif
2016-08-23 01:54:18 +08:00
using log_clock = std::chrono::system_clock;
using sink_ptr = std::shared_ptr<sinks::sink>;
using sinks_init_list = std::initializer_list<sink_ptr>;
2019-04-27 07:33:33 +08:00
using err_handler = std::function<void(const std::string &err_msg)>;
2023-09-17 00:50:23 +08:00
#ifdef SPDLOG_USE_STD_FORMAT
namespace fmt_lib = std;
using string_view_t = std::string_view;
using memory_buf_t = std::string;
2023-09-16 08:42:13 +08:00
using wstring_view_t = std::wstring_view;
using wmemory_buf_t = std::wstring;
2023-09-25 07:35:55 +08:00
template <typename... Args>
#if __cpp_lib_format >= 202207L
using format_string_t = std::format_string<Args...>;
2023-09-25 07:35:55 +08:00
#else
using format_string_t = std::string_view;
2023-09-25 07:35:55 +08:00
#endif
2023-09-25 07:35:55 +08:00
#define SPDLOG_BUF_TO_STRING(x) x
2023-09-25 21:40:05 +08:00
#else // use fmt lib instead of std::format
namespace fmt_lib = fmt;
2019-09-20 05:18:36 +08:00
using string_view_t = fmt::basic_string_view<char>;
using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
2023-09-25 07:35:55 +08:00
template <typename... Args>
using format_string_t = fmt::format_string<Args...>;
using wstring_view_t = fmt::basic_string_view<wchar_t>;
2021-11-17 05:42:06 +08:00
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
2023-09-25 07:35:55 +08:00
#define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
2023-09-25 21:40:05 +08:00
#endif // SPDLOG_USE_STD_FORMAT
#define SPDLOG_LEVEL_TRACE 0
#define SPDLOG_LEVEL_DEBUG 1
#define SPDLOG_LEVEL_INFO 2
#define SPDLOG_LEVEL_WARN 3
#define SPDLOG_LEVEL_ERROR 4
#define SPDLOG_LEVEL_CRITICAL 5
#define SPDLOG_LEVEL_OFF 6
#if !defined(SPDLOG_ACTIVE_LEVEL)
2023-09-25 07:35:55 +08:00
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
#endif
// Is convertable to string_view_t ?
2023-09-25 07:35:55 +08:00
template <typename T>
using is_convertible_to_sv = std::enable_if_t<std::is_convertible_v<T, string_view_t>>;
// Log level enum
2023-09-25 07:35:55 +08:00
enum class level {
trace = SPDLOG_LEVEL_TRACE,
debug = SPDLOG_LEVEL_DEBUG,
info = SPDLOG_LEVEL_INFO,
warn = SPDLOG_LEVEL_WARN,
err = SPDLOG_LEVEL_ERROR,
critical = SPDLOG_LEVEL_CRITICAL,
off = SPDLOG_LEVEL_OFF,
n_levels
2018-02-25 05:35:09 +08:00
};
2016-08-23 01:54:18 +08:00
2023-09-23 05:30:24 +08:00
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
2023-09-25 01:43:14 +08:00
using atomic_level_t = details::null_atomic<level>;
2023-09-23 05:27:32 +08:00
#else
2023-09-25 01:43:14 +08:00
using atomic_level_t = std::atomic<level>;
2023-09-23 05:27:32 +08:00
#endif
#if !defined(SPDLOG_LEVEL_NAMES)
2023-09-25 07:35:55 +08:00
#define SPDLOG_LEVEL_NAMES \
{ "trace", "debug", "info", "warning", "error", "critical", "off" }
#endif
#if !defined(SPDLOG_SHORT_LEVEL_NAMES)
2023-09-25 07:35:55 +08:00
#define SPDLOG_SHORT_LEVEL_NAMES \
{ "T", "D", "I", "W", "E", "C", "O" }
#endif
2016-08-23 01:54:18 +08:00
[[nodiscard]] constexpr size_t level_to_number(level lvl) noexcept {
return static_cast<size_t>(lvl);
}
2023-09-23 23:21:27 +08:00
constexpr auto levels_count = level_to_number(level::n_levels);
2023-09-23 05:27:32 +08:00
constexpr std::array<string_view_t, levels_count> level_string_views SPDLOG_LEVEL_NAMES;
2023-09-26 06:43:48 +08:00
constexpr std::array<string_view_t, levels_count> short_level_names SPDLOG_SHORT_LEVEL_NAMES;
2023-09-25 07:35:55 +08:00
[[nodiscard]] constexpr string_view_t to_string_view(spdlog::level lvl) noexcept {
2023-09-23 23:21:27 +08:00
return level_string_views.at(level_to_number(lvl));
}
2023-09-26 06:53:55 +08:00
[[nodiscard]] constexpr const string_view_t to_short_string_view(spdlog::level lvl) noexcept {
2023-09-23 23:21:27 +08:00
return short_level_names.at(level_to_number(lvl));
}
2023-10-01 22:56:42 +08:00
[[nodiscard]] SPDLOG_API spdlog::level level_from_str(const std::string &name) noexcept;
2023-09-23 05:27:32 +08:00
//
// Color mode used by sinks with color support.
//
2023-09-25 07:35:55 +08:00
enum class color_mode { always, automatic, never };
//
// Pattern time - specific time getting to use for pattern_formatter.
// local time by default
//
2023-09-25 07:35:55 +08:00
enum class pattern_time_type {
2023-09-25 21:40:05 +08:00
local, // log localtime
utc // log utc
};
2016-08-23 01:54:18 +08:00
//
// Log exception
//
2023-09-25 07:35:55 +08:00
class SPDLOG_API spdlog_ex : public std::exception {
2016-08-23 01:54:18 +08:00
public:
2019-05-12 01:15:03 +08:00
explicit spdlog_ex(std::string msg);
2019-05-12 01:15:03 +08:00
spdlog_ex(const std::string &msg, int last_errno);
2023-09-23 06:53:03 +08:00
[[nodiscard]] const char *what() const noexcept override;
2018-06-14 01:16:31 +08:00
2016-08-23 01:54:18 +08:00
private:
2018-08-16 00:01:44 +08:00
std::string msg_;
2016-08-23 01:54:18 +08:00
};
2021-03-17 06:25:26 +08:00
[[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno);
[[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg);
2018-11-23 00:47:50 +08:00
// trick to capture format string and caller's source location with variadic template.
// see logger::info() etc. to understand how it's used.
2023-09-25 07:35:55 +08:00
struct loc_with_fmt {
source_loc loc;
string_view_t fmt_string;
2023-09-25 07:35:55 +08:00
template <typename S, typename = is_convertible_to_sv<S>>
2023-09-16 02:10:47 +08:00
constexpr loc_with_fmt(S fmt_str, source_loc loc = source_loc::current()) noexcept
2023-09-25 21:19:10 +08:00
: loc(loc),
fmt_string(fmt_str) {}
2023-09-17 06:19:23 +08:00
#ifndef SPDLOG_USE_STD_FORMAT
constexpr loc_with_fmt(fmt::runtime_format_string<char> fmt_str,
source_loc loc = source_loc::current()) noexcept
2023-09-25 21:19:10 +08:00
: loc(loc),
fmt_string(fmt_str.str) {}
2023-09-17 06:19:23 +08:00
#endif
};
2023-09-25 07:35:55 +08:00
struct file_event_handlers {
file_event_handlers()
2023-09-25 21:19:10 +08:00
: before_open(nullptr),
after_open(nullptr),
before_close(nullptr),
after_close(nullptr) {}
std::function<void(const filename_t &filename)> before_open;
2021-11-11 00:08:24 +08:00
std::function<void(const filename_t &filename, std::FILE *file_stream)> after_open;
std::function<void(const filename_t &filename, std::FILE *file_stream)> before_close;
std::function<void(const filename_t &filename)> after_close;
};
2021-11-11 00:08:24 +08:00
namespace details {
2021-12-20 01:37:21 +08:00
// to_string_view
2023-09-25 07:35:55 +08:00
[[nodiscard]] constexpr spdlog::string_view_t to_string_view(const memory_buf_t &buf) noexcept {
return spdlog::string_view_t{buf.data(), buf.size()};
}
[[nodiscard]] constexpr spdlog::string_view_t to_string_view(spdlog::string_view_t str) noexcept {
return str;
}
2023-07-28 22:15:10 +08:00
#if defined(SPDLOG_WCHAR_FILENAMES)
2023-09-25 07:35:55 +08:00
[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) noexcept {
return spdlog::wstring_view_t{buf.data(), buf.size()};
}
[[nodiscard]] constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept {
return str;
}
#endif
2023-09-17 00:50:23 +08:00
// convert format_string<...> to string_view depending on format lib versions
#if defined(SPDLOG_USE_STD_FORMAT)
2023-09-25 21:40:05 +08:00
#if __cpp_lib_format >= 202207L // std::format and __cpp_lib_format >= 202207L
2023-09-25 07:35:55 +08:00
template <typename T, typename... Args>
2023-09-25 21:40:05 +08:00
[[nodiscard]] constexpr std::basic_string_view<T> to_string_view(
std::basic_format_string<T, Args...> fmt) noexcept {
2023-09-17 00:50:23 +08:00
return fmt.get();
}
2023-09-25 21:40:05 +08:00
#else // std::format and __cpp_lib_format < 202207L
2023-09-25 07:35:55 +08:00
template <typename T, typename... Args>
2023-09-25 21:40:05 +08:00
[[nodiscard]] constexpr std::basic_string_view<T> to_string_view(
std::basic_format_string<T, Args...> fmt) noexcept {
2023-09-17 00:50:23 +08:00
return fmt;
}
2023-09-25 07:35:55 +08:00
#endif
2023-09-25 21:40:05 +08:00
#else // {fmt} version
2023-09-25 07:35:55 +08:00
template <typename T, typename... Args>
2023-09-25 21:40:05 +08:00
[[nodiscard]] constexpr fmt::basic_string_view<T> to_string_view(
fmt::basic_format_string<T, Args...> fmt) noexcept {
2023-09-16 22:23:04 +08:00
return fmt;
}
#endif
2023-09-25 21:40:05 +08:00
} // namespace details
} // namespace spdlog