spdlog/include/spdlog/common.h

140 lines
3.9 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>
2024-12-07 01:21:42 +08:00
#include <chrono>
2023-09-25 07:35:55 +08:00
#include <exception>
#include <functional>
#include <initializer_list>
#include <memory>
#include <string>
#include <string_view>
2024-12-07 20:47:03 +08:00
#include <cstdint>
2023-09-16 06:31:40 +08:00
#include "./source_loc.h"
#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
#define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
#define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string)
2021-06-27 00:43:37 +08:00
#ifndef SPDLOG_FUNCTION
2023-09-25 07:35:55 +08:00
#define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
#endif
2019-04-06 18:45:33 +08:00
namespace spdlog {
class formatter;
namespace sinks {
class sink;
}
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)>;
2024-11-29 19:52:17 +08:00
using string_view_t = std::basic_string_view<char>;
using wstring_view_t = std::basic_string_view<wchar_t>;
namespace fmt_lib = fmt;
using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
2023-09-25 07:35:55 +08:00
template <typename... Args>
using format_string_t = fmt::format_string<Args...>;
#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
// Log level enum
2024-12-07 20:47:03 +08:00
enum class level : std::uint8_t {
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,
2024-12-07 20:47:03 +08:00
n_levels = SPDLOG_LEVEL_OFF + 1
2018-02-25 05:35:09 +08:00
};
2016-08-23 01:54:18 +08:00
2023-09-25 01:43:14 +08:00
using atomic_level_t = std::atomic<level>;
2023-09-23 05:27:32 +08:00
2024-01-13 15:37:32 +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);
constexpr std::array<std::string_view, levels_count> level_string_views{"trace", "debug", "info", "warning",
2024-11-29 19:40:40 +08:00
"error", "critical", "off"};
constexpr std::array<std::string_view, levels_count> short_level_names{"T", "D", "I", "W", "E", "C", "O"};
[[nodiscard]] constexpr std::string_view to_string_view(spdlog::level lvl) noexcept {
2023-09-23 23:21:27 +08:00
return level_string_views.at(level_to_number(lvl));
}
[[nodiscard]] constexpr std::string_view 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
2023-09-25 21:40:05 +08:00
} // namespace spdlog