mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-28 17:19:34 +08:00
clang format
This commit is contained in:
@@ -33,7 +33,6 @@ elseif (NOT CMAKE_CXX_STANDARD)
|
|||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
if(CMAKE_SYSTEM_NAME MATCHES "CYGWIN" OR CMAKE_SYSTEM_NAME MATCHES "MSYS" OR CMAKE_SYSTEM_NAME MATCHES "MINGW")
|
if(CMAKE_SYSTEM_NAME MATCHES "CYGWIN" OR CMAKE_SYSTEM_NAME MATCHES "MSYS" OR CMAKE_SYSTEM_NAME MATCHES "MINGW")
|
||||||
@@ -291,11 +290,12 @@ if (MSVC)
|
|||||||
target_compile_options(spdlog PRIVATE "/Zc:__cplusplus")
|
target_compile_options(spdlog PRIVATE "/Zc:__cplusplus")
|
||||||
target_compile_options(spdlog_header_only INTERFACE "/Zc:__cplusplus")
|
target_compile_options(spdlog_header_only INTERFACE "/Zc:__cplusplus")
|
||||||
if(SPDLOG_MSVC_UTF8)
|
if(SPDLOG_MSVC_UTF8)
|
||||||
# fmtlib requires the /utf-8 flag when building with msvc.
|
# fmtlib requires the /utf-8 flag when building with msvc. see https://github.com/fmtlib/fmt/pull/4159 on the
|
||||||
# see https://github.com/fmtlib/fmt/pull/4159 on the purpose of the additional
|
# purpose of the additional
|
||||||
# "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>"
|
# "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>"
|
||||||
target_compile_options(spdlog PUBLIC $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)
|
target_compile_options(spdlog PUBLIC $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)
|
||||||
target_compile_options(spdlog_header_only INTERFACE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)
|
target_compile_options(spdlog_header_only
|
||||||
|
INTERFACE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/utf-8>)
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
@@ -382,14 +382,14 @@ void replace_default_logger_example() {
|
|||||||
spdlog::set_default_logger(old_logger);
|
spdlog::set_default_logger(old_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mapped Diagnostic Context (MDC) is a map that stores key-value pairs (string values) in thread local storage.
|
// Mapped Diagnostic Context (MDC) is a map that stores key-value pairs (string values) in thread
|
||||||
// Each thread maintains its own MDC, which loggers use to append diagnostic information to log outputs.
|
// local storage. Each thread maintains its own MDC, which loggers use to append diagnostic
|
||||||
// Note: it is not supported in asynchronous mode due to its reliance on thread-local storage.
|
// information to log outputs. Note: it is not supported in asynchronous mode due to its reliance on
|
||||||
|
// thread-local storage.
|
||||||
|
|
||||||
#ifndef SPDLOG_NO_TLS
|
#ifndef SPDLOG_NO_TLS
|
||||||
#include "spdlog/mdc.h"
|
#include "spdlog/mdc.h"
|
||||||
void mdc_example()
|
void mdc_example() {
|
||||||
{
|
|
||||||
spdlog::mdc::put("key1", "value1");
|
spdlog::mdc::put("key1", "value1");
|
||||||
spdlog::mdc::put("key2", "value2");
|
spdlog::mdc::put("key2", "value2");
|
||||||
// if not using the default format, you can use the %& formatter to print mdc data as well
|
// if not using the default format, you can use the %& formatter to print mdc data as well
|
||||||
|
@@ -89,8 +89,7 @@ inline void init_thread_pool(size_t q_size,
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void init_thread_pool(size_t q_size, size_t thread_count) {
|
inline void init_thread_pool(size_t q_size, size_t thread_count) {
|
||||||
init_thread_pool(
|
init_thread_pool(q_size, thread_count, [] {}, [] {});
|
||||||
q_size, thread_count, [] {}, [] {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the global thread pool.
|
// get the global thread pool.
|
||||||
|
@@ -483,13 +483,12 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// find the size to allocate for the result buffer
|
// find the size to allocate for the result buffer
|
||||||
int result_size =
|
int result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0);
|
||||||
::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0);
|
|
||||||
|
|
||||||
if (result_size > 0) {
|
if (result_size > 0) {
|
||||||
target.resize(result_size);
|
target.resize(result_size);
|
||||||
result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(),
|
result_size =
|
||||||
result_size);
|
::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), result_size);
|
||||||
if (result_size > 0) {
|
if (result_size > 0) {
|
||||||
assert(result_size == target.size());
|
assert(result_size == target.size());
|
||||||
return;
|
return;
|
||||||
|
@@ -38,8 +38,7 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items,
|
|||||||
: thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {}
|
: thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {}
|
||||||
|
|
||||||
SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
|
SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
|
||||||
: thread_pool(
|
: thread_pool(q_max_items, threads_n, [] {}, [] {}) {}
|
||||||
q_max_items, threads_n, [] {}, [] {}) {}
|
|
||||||
|
|
||||||
// message all threads to terminate gracefully join them
|
// message all threads to terminate gracefully join them
|
||||||
SPDLOG_INLINE thread_pool::~thread_pool() {
|
SPDLOG_INLINE thread_pool::~thread_pool() {
|
||||||
|
@@ -142,8 +142,8 @@ struct formatter<spdlog::details::dump_info<T>, char> {
|
|||||||
|
|
||||||
// format the given bytes range as hex
|
// format the given bytes range as hex
|
||||||
template <typename FormatContext, typename Container>
|
template <typename FormatContext, typename Container>
|
||||||
auto format(const spdlog::details::dump_info<Container> &the_range, FormatContext &ctx) const
|
auto format(const spdlog::details::dump_info<Container> &the_range,
|
||||||
-> decltype(ctx.out()) {
|
FormatContext &ctx) const -> decltype(ctx.out()) {
|
||||||
SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF";
|
SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF";
|
||||||
SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef";
|
SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef";
|
||||||
const char *hex_chars = use_uppercase ? hex_upper : hex_lower;
|
const char *hex_chars = use_uppercase ? hex_upper : hex_lower;
|
||||||
|
@@ -12,12 +12,14 @@
|
|||||||
|
|
||||||
#include <spdlog/common.h>
|
#include <spdlog/common.h>
|
||||||
|
|
||||||
// MDC is a simple map of key->string values stored in thread local storage whose content will be printed by the loggers.
|
// MDC is a simple map of key->string values stored in thread local storage whose content will be
|
||||||
// Note: Not supported in async mode (thread local storage - so the async thread pool have different copy).
|
// printed by the loggers. Note: Not supported in async mode (thread local storage - so the async
|
||||||
|
// thread pool have different copy).
|
||||||
//
|
//
|
||||||
// Usage example:
|
// Usage example:
|
||||||
// spdlog::mdc::put("mdc_key_1", "mdc_value_1");
|
// spdlog::mdc::put("mdc_key_1", "mdc_value_1");
|
||||||
// spdlog::info("Hello, {}", "World!"); // => [2024-04-26 02:08:05.040] [info] [mdc_key_1:mdc_value_1] Hello, World!
|
// spdlog::info("Hello, {}", "World!"); // => [2024-04-26 02:08:05.040] [info]
|
||||||
|
// [mdc_key_1:mdc_value_1] Hello, World!
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
class SPDLOG_API mdc {
|
class SPDLOG_API mdc {
|
||||||
|
@@ -111,7 +111,8 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode_(color_mode mode
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename ConsoleMutex>
|
template <typename ConsoleMutex>
|
||||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code) const {
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(
|
||||||
|
const string_view_t &color_code) const {
|
||||||
details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_);
|
details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -109,8 +109,8 @@
|
|||||||
//
|
//
|
||||||
// #include <string_view>
|
// #include <string_view>
|
||||||
// using namespace std::string_view_literals;
|
// using namespace std::string_view_literals;
|
||||||
// #define SPDLOG_LEVEL_NAMES { "MY TRACE"sv, "MY DEBUG"sv, "MY INFO"sv, "MY WARNING"sv, "MY ERROR"sv, "MY
|
// #define SPDLOG_LEVEL_NAMES { "MY TRACE"sv, "MY DEBUG"sv, "MY INFO"sv, "MY WARNING"sv, "MY
|
||||||
// CRITICAL"sv, "OFF"sv }
|
// ERROR"sv, "MY CRITICAL"sv, "OFF"sv }
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@@ -13,10 +13,8 @@
|
|||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template FMT_API auto dragonbox::to_decimal(float x) noexcept
|
template FMT_API auto dragonbox::to_decimal(float x) noexcept -> dragonbox::decimal_fp<float>;
|
||||||
-> dragonbox::decimal_fp<float>;
|
template FMT_API auto dragonbox::to_decimal(double x) noexcept -> dragonbox::decimal_fp<double>;
|
||||||
template FMT_API auto dragonbox::to_decimal(double x) noexcept
|
|
||||||
-> dragonbox::decimal_fp<double>;
|
|
||||||
|
|
||||||
#if FMT_USE_LOCALE
|
#if FMT_USE_LOCALE
|
||||||
// DEPRECATED! locale_ref in the detail namespace
|
// DEPRECATED! locale_ref in the detail namespace
|
||||||
@@ -26,21 +24,21 @@ template FMT_API auto locale_ref::get<std::locale>() const -> std::locale;
|
|||||||
|
|
||||||
// Explicit instantiations for char.
|
// Explicit instantiations for char.
|
||||||
|
|
||||||
template FMT_API auto thousands_sep_impl(locale_ref)
|
template FMT_API auto thousands_sep_impl(locale_ref) -> thousands_sep_result<char>;
|
||||||
-> thousands_sep_result<char>;
|
|
||||||
template FMT_API auto decimal_point_impl(locale_ref) -> char;
|
template FMT_API auto decimal_point_impl(locale_ref) -> char;
|
||||||
|
|
||||||
// DEPRECATED!
|
// DEPRECATED!
|
||||||
template FMT_API void buffer<char>::append(const char*, const char*);
|
template FMT_API void buffer<char>::append(const char*, const char*);
|
||||||
|
|
||||||
// DEPRECATED!
|
// DEPRECATED!
|
||||||
template FMT_API void vformat_to(buffer<char>&, string_view,
|
template FMT_API void vformat_to(buffer<char>&,
|
||||||
typename vformat_args<>::type, locale_ref);
|
string_view,
|
||||||
|
typename vformat_args<>::type,
|
||||||
|
locale_ref);
|
||||||
|
|
||||||
// Explicit instantiations for wchar_t.
|
// Explicit instantiations for wchar_t.
|
||||||
|
|
||||||
template FMT_API auto thousands_sep_impl(locale_ref)
|
template FMT_API auto thousands_sep_impl(locale_ref) -> thousands_sep_result<wchar_t>;
|
||||||
-> thousands_sep_result<wchar_t>;
|
|
||||||
template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
|
template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
|
||||||
|
|
||||||
template FMT_API void buffer<wchar_t>::append(const wchar_t*, const wchar_t*);
|
template FMT_API void buffer<wchar_t>::append(const wchar_t*, const wchar_t*);
|
||||||
@@ -48,5 +46,4 @@ template FMT_API void buffer<wchar_t>::append(const wchar_t*, const wchar_t*);
|
|||||||
} // namespace detail
|
} // namespace detail
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
|
||||||
#endif // !SPDLOG_FMT_EXTERNAL
|
#endif // !SPDLOG_FMT_EXTERNAL
|
||||||
|
@@ -19,8 +19,8 @@ if(Catch2_FOUND)
|
|||||||
else()
|
else()
|
||||||
message(STATUS "Bundled version of Catch will be downloaded and used.")
|
message(STATUS "Bundled version of Catch will be downloaded and used.")
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
FetchContent_Declare(Catch2
|
FetchContent_Declare(
|
||||||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||||
GIT_TAG 53d0d913a422d356b23dd927547febdf69ee9081 # v3.5.0
|
GIT_TAG 53d0d913a422d356b23dd927547febdf69ee9081 # v3.5.0
|
||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(Catch2)
|
FetchContent_MakeAvailable(Catch2)
|
||||||
|
@@ -16,7 +16,8 @@ TEST_CASE("custom_callback_logger", "[custom_callback_logger]") {
|
|||||||
spdlog::memory_buf_t formatted;
|
spdlog::memory_buf_t formatted;
|
||||||
formatter.format(msg, formatted);
|
formatter.format(msg, formatted);
|
||||||
auto eol_len = strlen(spdlog::details::os::default_eol);
|
auto eol_len = strlen(spdlog::details::os::default_eol);
|
||||||
using diff_t = typename std::iterator_traits<decltype(formatted.end())>::difference_type;
|
using diff_t =
|
||||||
|
typename std::iterator_traits<decltype(formatted.end())>::difference_type;
|
||||||
lines.emplace_back(formatted.begin(), formatted.end() - static_cast<diff_t>(eol_len));
|
lines.emplace_back(formatted.begin(), formatted.end() - static_cast<diff_t>(eol_len));
|
||||||
});
|
});
|
||||||
std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st);
|
std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st);
|
||||||
|
@@ -46,10 +46,8 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
|||||||
|
|
||||||
struct custom_daily_file_name_calculator {
|
struct custom_daily_file_name_calculator {
|
||||||
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm) {
|
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm) {
|
||||||
|
return spdlog::fmt_lib::format(SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename,
|
||||||
return spdlog::fmt_lib::format(SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"),
|
now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday);
|
||||||
basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1,
|
|
||||||
now_tm.tm_mday);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -185,14 +185,16 @@ TEST_CASE("utf8 to utf16 conversion using windows api", "[windows utf]") {
|
|||||||
spdlog::details::os::utf8_to_wstrbuf("\xc3\x28", buffer); // Invalid UTF-8 sequence.
|
spdlog::details::os::utf8_to_wstrbuf("\xc3\x28", buffer); // Invalid UTF-8 sequence.
|
||||||
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\xfffd("));
|
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\xfffd("));
|
||||||
|
|
||||||
spdlog::details::os::utf8_to_wstrbuf("\xe3\x81\xad\xe3\x81\x93", buffer); // "Neko" in hiragana.
|
spdlog::details::os::utf8_to_wstrbuf("\xe3\x81\xad\xe3\x81\x93",
|
||||||
|
buffer); // "Neko" in hiragana.
|
||||||
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\x306d\x3053"));
|
REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\x306d\x3053"));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct auto_closer {
|
struct auto_closer {
|
||||||
FILE* fp = nullptr;
|
FILE* fp = nullptr;
|
||||||
explicit auto_closer(FILE* f) : fp(f) {}
|
explicit auto_closer(FILE* f)
|
||||||
|
: fp(f) {}
|
||||||
auto_closer(const auto_closer&) = delete;
|
auto_closer(const auto_closer&) = delete;
|
||||||
auto_closer& operator=(const auto_closer&) = delete;
|
auto_closer& operator=(const auto_closer&) = delete;
|
||||||
~auto_closer() {
|
~auto_closer() {
|
||||||
@@ -201,8 +203,8 @@ struct auto_closer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
TEST_CASE("os::fwrite_bytes", "[os]") {
|
TEST_CASE("os::fwrite_bytes", "[os]") {
|
||||||
using spdlog::details::os::fwrite_bytes;
|
|
||||||
using spdlog::details::os::create_dir;
|
using spdlog::details::os::create_dir;
|
||||||
|
using spdlog::details::os::fwrite_bytes;
|
||||||
const char* filename = "log_tests/test_fwrite_bytes.txt";
|
const char* filename = "log_tests/test_fwrite_bytes.txt";
|
||||||
const char* msg = "hello";
|
const char* msg = "hello";
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
|
@@ -23,7 +23,9 @@ static std::string log_to_str(const std::string &msg, const Args &...args) {
|
|||||||
|
|
||||||
// log to str and return it with time
|
// log to str and return it with time
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
static std::string log_to_str_with_time(spdlog::log_clock::time_point log_time, const std::string &msg, const Args &...args) {
|
static std::string log_to_str_with_time(spdlog::log_clock::time_point log_time,
|
||||||
|
const std::string &msg,
|
||||||
|
const Args &...args) {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||||
spdlog::logger oss_logger("pattern_tester", oss_sink);
|
spdlog::logger oss_logger("pattern_tester", oss_sink);
|
||||||
@@ -80,8 +82,8 @@ TEST_CASE("GMT offset ", "[pattern_formatter]") {
|
|||||||
const auto now = std::chrono::system_clock::now();
|
const auto now = std::chrono::system_clock::now();
|
||||||
const auto yesterday = now - 24h;
|
const auto yesterday = now - 24h;
|
||||||
|
|
||||||
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc, "\n") ==
|
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc,
|
||||||
"+00:00\n");
|
"\n") == "+00:00\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("color range test1", "[pattern_formatter]") {
|
TEST_CASE("color range test1", "[pattern_formatter]") {
|
||||||
|
Reference in New Issue
Block a user