spdlog/include/spdlog/common.h

364 lines
11 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
#include <spdlog/tweakme.h>
#include <spdlog/details/null_mutex.h>
2016-08-23 01:54:18 +08:00
#include <atomic>
#include <chrono>
#include <initializer_list>
#include <memory>
2019-07-16 20:50:42 +08:00
#include <exception>
#include <string>
2019-04-06 06:44:03 +08:00
#include <type_traits>
2019-04-27 07:33:33 +08:00
#include <functional>
2021-11-11 00:08:24 +08:00
#include <cstdio>
#include <cstdint>
#include <version>
2023-09-16 02:10:47 +08:00
#if __cpp_lib_source_location >= 201907
# include <source_location>
# define SPDLOG_STD_SOURCE_LOCATION
#elif __has_include(<experimental/source_location>)
# include <experimental/source_location>
# define SPDLOG_EXPERIMENTAL_SOURCE_LOCATION
#endif
2016-08-23 01:54:18 +08:00
#ifdef SPDLOG_USE_STD_FORMAT
# if __cpp_lib_format >= 202207L
# include <format>
# else
# include <string_view>
# endif
#endif
#ifdef SPDLOG_COMPILED_LIB
2021-06-26 22:36:57 +08:00
# undef SPDLOG_HEADER_ONLY
2021-11-25 07:16:28 +08:00
# if defined(SPDLOG_SHARED_LIB)
# if defined(_WIN32)
# ifdef spdlog_EXPORTS
# define SPDLOG_API __declspec(dllexport)
# else // !spdlog_EXPORTS
# define SPDLOG_API __declspec(dllimport)
# endif
# else // !defined(_WIN32)
2021-12-20 01:37:21 +08:00
# define SPDLOG_API __attribute__((visibility("default")))
2021-06-26 22:36:57 +08:00
# endif
2021-11-25 07:16:28 +08:00
# else // !defined(SPDLOG_SHARED_LIB)
2021-06-26 22:36:57 +08:00
# define SPDLOG_API
# endif
# define SPDLOG_INLINE
2020-04-28 23:42:59 +08:00
#else // !defined(SPDLOG_COMPILED_LIB)
2021-06-26 22:36:57 +08:00
# define SPDLOG_API
# define SPDLOG_HEADER_ONLY
# define SPDLOG_INLINE inline
2020-04-28 23:37:57 +08:00
#endif // #ifdef SPDLOG_COMPILED_LIB
2019-04-05 21:44:17 +08:00
#include <spdlog/fmt/fmt.h>
2019-05-12 05:32:57 +08:00
#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8
# define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
# define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
#else
# define SPDLOG_FMT_RUNTIME(format_string) format_string
# define SPDLOG_FMT_STRING(format_string) format_string
2021-06-27 00:43:37 +08:00
#endif
#ifndef SPDLOG_FUNCTION
2021-06-26 22:36:57 +08:00
# define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
#endif
2019-08-19 00:46:28 +08:00
#ifdef SPDLOG_NO_EXCEPTIONS
2021-06-26 22:36:57 +08:00
# define SPDLOG_TRY
# define SPDLOG_THROW(ex) \
do \
{ \
printf("spdlog fatal error: %s\n", ex.what()); \
std::abort(); \
} while (0)
# define SPDLOG_CATCH_STD
2019-08-19 00:46:28 +08:00
#else
2021-06-26 22:36:57 +08:00
# define SPDLOG_TRY try
# define SPDLOG_THROW(ex) throw(ex)
# define SPDLOG_CATCH_STD \
2023-06-07 18:23:44 +08:00
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
2021-06-26 22:36:57 +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;
2021-06-26 22:36:57 +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)>;
#ifdef SPDLOG_USE_STD_FORMAT
namespace fmt_lib = std;
using string_view_t = std::string_view;
using memory_buf_t = std::string;
template<typename... Args>
# if __cpp_lib_format >= 202207L
using format_string_t = std::format_string<Args...>;
# else
using format_string_t = std::string_view;
# endif
# define SPDLOG_BUF_TO_STRING(x) x
2021-11-17 05:42:06 +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>;
template<typename... Args>
using format_string_t = fmt::format_string<Args...>;
2023-07-28 22:15:10 +08:00
# if defined(SPDLOG_WCHAR_FILENAMES)
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>;
template<typename... Args>
using wformat_string_t = fmt::wformat_string<Args...>;
# endif
# define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x)
#endif
2016-08-23 01:54:18 +08:00
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
using level_t = details::null_atomic_int;
#else
2016-10-09 06:55:47 +08:00
using level_t = std::atomic<int>;
2016-08-23 01:54:18 +08:00
#endif
#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)
2021-06-26 22:36:57 +08:00
# define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
#endif
// Is convertable to string_view_t ?
2023-09-16 02:10:47 +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
namespace level {
enum level_enum : int
2016-08-23 01:54:18 +08:00
{
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
2021-07-29 15:09:52 +08:00
#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
#if !defined(SPDLOG_LEVEL_NAMES)
2021-06-26 22:36:57 +08:00
# define SPDLOG_LEVEL_NAMES \
{ \
SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, \
SPDLOG_LEVEL_NAME_CRITICAL, SPDLOG_LEVEL_NAME_OFF \
}
#endif
#if !defined(SPDLOG_SHORT_LEVEL_NAMES)
2021-06-26 22:36:57 +08:00
# define SPDLOG_SHORT_LEVEL_NAMES \
{ \
"T", "D", "I", "W", "E", "C", "O" \
}
#endif
2016-08-23 01:54:18 +08:00
SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) noexcept;
SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) noexcept;
SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) noexcept;
} // namespace level
2016-08-23 01:54:18 +08:00
//
// Color mode used by sinks with color support.
//
enum class color_mode
{
always,
automatic,
never
};
//
// Pattern time - specific time getting to use for pattern_formatter.
// local time by default
//
enum class pattern_time_type
{
2017-06-01 08:42:10 +08:00
local, // log localtime
utc // log utc
};
2016-08-23 01:54:18 +08:00
//
// Log exception
//
2020-03-10 03:02:16 +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);
spdlog_ex(const std::string &msg, int last_errno);
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
struct source_loc
{
constexpr source_loc() = default;
constexpr source_loc(const char *filename_in, std::uint_least32_t line_in, const char *funcname_in)
: filename{filename_in}
2019-04-06 06:44:03 +08:00
, line{line_in}
, funcname{funcname_in}
{}
2018-11-25 06:44:51 +08:00
#ifdef SPDLOG_STD_SOURCE_LOCATION
static constexpr source_loc current(const std::source_location source_location = std::source_location::current())
{
return source_loc{source_location.file_name(), source_location.line(), source_location.function_name()};
}
#elif defined(SPDLOG_EXPERIMENTAL_SOURCE_LOCATION)
2023-09-16 02:10:47 +08:00
static constexpr source_loc current(
const std::experimental::source_location source_location = std::experimental::source_location::current())
{
return source_loc{source_location.file_name(), source_location.line(), source_location.function_name()};
}
#else // no source location support
static constexpr source_loc current()
{
2023-09-16 02:10:47 +08:00
return source_loc{};
}
#endif
[[nodiscard]] constexpr bool empty() const noexcept
2018-11-25 06:44:51 +08:00
{
return line == 0;
}
2019-04-06 06:44:03 +08:00
const char *filename{nullptr};
std::uint_least32_t line{0};
2019-04-06 06:44:03 +08:00
const char *funcname{nullptr};
2018-11-23 00:47:50 +08:00
};
2023-09-16 02:07:09 +08:00
// to capture caller's current source location and format string at compile time.
struct loc_with_fmt
{
source_loc loc;
string_view_t fmt_string;
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
: loc(loc)
, fmt_string(fmt_str)
{}
#ifndef SPDLOG_USE_STD_FORMAT
constexpr loc_with_fmt(fmt::runtime_format_string<char> fmt_str, source_loc loc = source_loc::current()) noexcept
: loc(loc)
, fmt_string(fmt_str.str)
{}
#endif
};
struct file_event_handlers
2021-11-11 00:08:24 +08:00
{
file_event_handlers()
: 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
constexpr spdlog::string_view_t to_string_view(const memory_buf_t &buf) noexcept
{
return spdlog::string_view_t{buf.data(), buf.size()};
}
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)
constexpr spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) noexcept
{
return spdlog::wstring_view_t{buf.data(), buf.size()};
}
constexpr spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) noexcept
{
return str;
}
#endif
#ifndef SPDLOG_USE_STD_FORMAT
template<typename T, typename... Args>
inline fmt::basic_string_view<T> to_string_view(fmt::basic_format_string<T, Args...> fmt)
{
return fmt;
}
#elif __cpp_lib_format >= 202207L
template<typename T, typename... Args>
constexpr std::basic_string_view<T> to_string_view(std::basic_format_string<T, Args...> fmt) noexcept
{
return fmt.get();
}
#endif
} // namespace details
} // namespace spdlog
2019-05-12 01:15:03 +08:00
#ifdef SPDLOG_HEADER_ONLY
2021-06-26 22:36:57 +08:00
# include "common-inl.h"
#endif