mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-30 10:29:02 +08:00
Compare commits
15 Commits
fix-3360
...
feature-33
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d8820dcb44 | ||
![]() |
37e8a77862 | ||
![]() |
762c238135 | ||
![]() |
9a86d1f2ba | ||
![]() |
493f5b0581 | ||
![]() |
548b264254 | ||
![]() |
847db3375f | ||
![]() |
bb8694b50f | ||
![]() |
cec28bf839 | ||
![]() |
bd0609d7a0 | ||
![]() |
1f4959c832 | ||
![]() |
48bcf39a66 | ||
![]() |
9c58257480 | ||
![]() |
faa0a7a9c5 | ||
![]() |
10320184df |
@@ -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()
|
||||||
|
|
||||||
@@ -304,12 +304,13 @@ endif ()
|
|||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if(SPDLOG_NO_EXCEPTIONS)
|
if(SPDLOG_NO_EXCEPTIONS)
|
||||||
if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
|
if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
|
||||||
target_compile_definitions(spdlog PUBLIC FMT_EXCEPTIONS=0)
|
target_compile_definitions(spdlog PUBLIC FMT_USE_EXCEPTIONS=0)
|
||||||
endif()
|
endif()
|
||||||
if(NOT MSVC)
|
if(NOT MSVC)
|
||||||
target_compile_options(spdlog PRIVATE -fno-exceptions)
|
target_compile_options(spdlog PRIVATE -fno-exceptions)
|
||||||
else()
|
else()
|
||||||
target_compile_options(spdlog PRIVATE /EHs-c-)
|
target_compile_options(spdlog PRIVATE /EHs-c-)
|
||||||
|
target_compile_definitions(spdlog PRIVATE _HAS_EXCEPTIONS=0)
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
# ---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
|
13
README.md
13
README.md
@@ -11,7 +11,7 @@ Fast C++ logging library
|
|||||||
|
|
||||||
## Install
|
## Install
|
||||||
#### Header-only version
|
#### Header-only version
|
||||||
Copy the include [folder](https://github.com/gabime/spdlog/tree/v1.x/include/spdlog) to your build tree and use a C++11 compiler.
|
Copy the include [folder](include/spdlog) to your build tree and use a C++11 compiler.
|
||||||
|
|
||||||
#### Compiled version (recommended - much faster compile times)
|
#### Compiled version (recommended - much faster compile times)
|
||||||
```console
|
```console
|
||||||
@@ -19,7 +19,7 @@ $ git clone https://github.com/gabime/spdlog.git
|
|||||||
$ cd spdlog && mkdir build && cd build
|
$ cd spdlog && mkdir build && cd build
|
||||||
$ cmake .. && cmake --build .
|
$ cmake .. && cmake --build .
|
||||||
```
|
```
|
||||||
see example [CMakeLists.txt](https://github.com/gabime/spdlog/blob/v1.x/example/CMakeLists.txt) on how to use.
|
see example [CMakeLists.txt](example/CMakeLists.txt) on how to use.
|
||||||
|
|
||||||
## Platforms
|
## Platforms
|
||||||
* Linux, FreeBSD, OpenBSD, Solaris, AIX
|
* Linux, FreeBSD, OpenBSD, Solaris, AIX
|
||||||
@@ -48,7 +48,7 @@ see example [CMakeLists.txt](https://github.com/gabime/spdlog/blob/v1.x/example/
|
|||||||
* Headers only or compiled
|
* Headers only or compiled
|
||||||
* Feature-rich formatting, using the excellent [fmt](https://github.com/fmtlib/fmt) library.
|
* Feature-rich formatting, using the excellent [fmt](https://github.com/fmtlib/fmt) library.
|
||||||
* Asynchronous mode (optional)
|
* Asynchronous mode (optional)
|
||||||
* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
|
* [Custom](https://github.com/gabime/spdlog/wiki/Custom-formatting) formatting.
|
||||||
* Multi/Single threaded loggers.
|
* Multi/Single threaded loggers.
|
||||||
* Various log targets:
|
* Various log targets:
|
||||||
* Rotating log files.
|
* Rotating log files.
|
||||||
@@ -58,7 +58,7 @@ see example [CMakeLists.txt](https://github.com/gabime/spdlog/blob/v1.x/example/
|
|||||||
* Windows event log.
|
* Windows event log.
|
||||||
* Windows debugger (```OutputDebugString(..)```).
|
* Windows debugger (```OutputDebugString(..)```).
|
||||||
* Log to Qt widgets ([example](#log-to-qt-with-nice-colors)).
|
* Log to Qt widgets ([example](#log-to-qt-with-nice-colors)).
|
||||||
* Easily [extendable](https://github.com/gabime/spdlog/wiki/4.-Sinks#implementing-your-own-sink) with custom log targets.
|
* Easily [extendable](https://github.com/gabime/spdlog/wiki/Sinks#implementing-your-own-sink) with custom log targets.
|
||||||
* Log filtering - log levels can be modified at runtime as well as compile time.
|
* Log filtering - log levels can be modified at runtime as well as compile time.
|
||||||
* Support for loading log levels from argv or environment var.
|
* Support for loading log levels from argv or environment var.
|
||||||
* [Backtrace](#backtrace-support) support - store debug messages in a ring buffer and display them later on demand.
|
* [Backtrace](#backtrace-support) support - store debug messages in a ring buffer and display them later on demand.
|
||||||
@@ -467,7 +467,7 @@ void mdc_example()
|
|||||||
---
|
---
|
||||||
## Benchmarks
|
## Benchmarks
|
||||||
|
|
||||||
Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/bench.cpp) done in Ubuntu 64 bit, Intel i7-4770 CPU @ 3.40GHz
|
Below are some [benchmarks](bench/bench.cpp) done in Ubuntu 64 bit, Intel i7-4770 CPU @ 3.40GHz
|
||||||
|
|
||||||
#### Synchronous mode
|
#### Synchronous mode
|
||||||
```
|
```
|
||||||
@@ -519,7 +519,8 @@ Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/ben
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
Documentation can be found in the [wiki](https://github.com/gabime/spdlog/wiki/1.-QuickStart) pages.
|
|
||||||
|
Documentation can be found in the [wiki](https://github.com/gabime/spdlog/wiki) pages.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@@ -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.
|
||||||
|
@@ -267,7 +267,8 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
|
|||||||
|
|
||||||
#if defined(sun) || defined(__sun) || defined(_AIX) || \
|
#if defined(sun) || defined(__sun) || defined(_AIX) || \
|
||||||
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
||||||
(!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
|
(!defined(__APPLE__) && !defined(_BSD_SOURCE) && !defined(_GNU_SOURCE) && \
|
||||||
|
(!defined(_POSIX_VERSION) || (_POSIX_VERSION < 202405L)))
|
||||||
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
||||||
struct helper {
|
struct helper {
|
||||||
static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),
|
static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),
|
||||||
@@ -482,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;
|
||||||
|
@@ -21,7 +21,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// The fmt library version in the form major * 10000 + minor * 100 + patch.
|
// The fmt library version in the form major * 10000 + minor * 100 + patch.
|
||||||
#define FMT_VERSION 110103
|
#define FMT_VERSION 110104
|
||||||
|
|
||||||
// Detect compiler versions.
|
// Detect compiler versions.
|
||||||
#if defined(__clang__) && !defined(__ibmxl__)
|
#if defined(__clang__) && !defined(__ibmxl__)
|
||||||
@@ -294,8 +294,8 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define FMT_APPLY_VARIADIC(expr) \
|
#define FMT_APPLY_VARIADIC(expr) \
|
||||||
using ignore = int[]; \
|
using unused = int[]; \
|
||||||
(void)ignore { 0, (expr, 0)... }
|
(void)unused { 0, (expr, 0)... }
|
||||||
|
|
||||||
// Enable minimal optimizations for more compact code in debug mode.
|
// Enable minimal optimizations for more compact code in debug mode.
|
||||||
FMT_PRAGMA_GCC(push_options)
|
FMT_PRAGMA_GCC(push_options)
|
||||||
@@ -537,7 +537,7 @@ template <typename Char> class basic_string_view {
|
|||||||
FMT_ALWAYS_INLINE
|
FMT_ALWAYS_INLINE
|
||||||
#endif
|
#endif
|
||||||
FMT_CONSTEXPR20 basic_string_view(const Char* s) : data_(s) {
|
FMT_CONSTEXPR20 basic_string_view(const Char* s) : data_(s) {
|
||||||
#if FMT_HAS_BUILTIN(__buitin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION
|
#if FMT_HAS_BUILTIN(__builtin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION
|
||||||
if (std::is_same<Char, char>::value) {
|
if (std::is_same<Char, char>::value) {
|
||||||
size_ = __builtin_strlen(detail::narrow(s));
|
size_ = __builtin_strlen(detail::narrow(s));
|
||||||
return;
|
return;
|
||||||
@@ -740,7 +740,7 @@ class basic_specs {
|
|||||||
};
|
};
|
||||||
|
|
||||||
unsigned data_ = 1 << fill_size_shift;
|
unsigned data_ = 1 << fill_size_shift;
|
||||||
static_assert(sizeof(data_) * CHAR_BIT >= 18, "");
|
static_assert(sizeof(basic_specs::data_) * CHAR_BIT >= 18, "");
|
||||||
|
|
||||||
// Character (code unit) type is erased to prevent template bloat.
|
// Character (code unit) type is erased to prevent template bloat.
|
||||||
char fill_data_[max_fill_size] = {' '};
|
char fill_data_[max_fill_size] = {' '};
|
||||||
@@ -2263,15 +2263,15 @@ template <> struct is_output_iterator<appender, char> : std::true_type {};
|
|||||||
template <typename It, typename T>
|
template <typename It, typename T>
|
||||||
struct is_output_iterator<
|
struct is_output_iterator<
|
||||||
It, T,
|
It, T,
|
||||||
void_t<decltype(*std::declval<decay_t<It>&>()++ = std::declval<T>())>>
|
enable_if_t<std::is_assignable<decltype(*std::declval<decay_t<It>&>()++),
|
||||||
: std::true_type {};
|
T>::value>> : std::true_type {};
|
||||||
|
|
||||||
#ifndef FMT_USE_LOCALE
|
#ifndef FMT_USE_LOCALE
|
||||||
# define FMT_USE_LOCALE (FMT_OPTIMIZE_SIZE <= 1)
|
# define FMT_USE_LOCALE (FMT_OPTIMIZE_SIZE <= 1)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// A type-erased reference to an std::locale to avoid a heavy <locale> include.
|
// A type-erased reference to an std::locale to avoid a heavy <locale> include.
|
||||||
struct locale_ref {
|
class locale_ref {
|
||||||
#if FMT_USE_LOCALE
|
#if FMT_USE_LOCALE
|
||||||
private:
|
private:
|
||||||
const void* locale_; // A type-erased pointer to std::locale.
|
const void* locale_; // A type-erased pointer to std::locale.
|
||||||
@@ -2283,6 +2283,7 @@ struct locale_ref {
|
|||||||
inline explicit operator bool() const noexcept { return locale_ != nullptr; }
|
inline explicit operator bool() const noexcept { return locale_ != nullptr; }
|
||||||
#endif // FMT_USE_LOCALE
|
#endif // FMT_USE_LOCALE
|
||||||
|
|
||||||
|
public:
|
||||||
template <typename Locale> auto get() const -> Locale;
|
template <typename Locale> auto get() const -> Locale;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -2729,9 +2730,9 @@ template <typename... T> struct fstring {
|
|||||||
std::is_same<typename S::char_type, char>::value)>
|
std::is_same<typename S::char_type, char>::value)>
|
||||||
FMT_ALWAYS_INLINE fstring(const S&) : str(S()) {
|
FMT_ALWAYS_INLINE fstring(const S&) : str(S()) {
|
||||||
FMT_CONSTEXPR auto sv = string_view(S());
|
FMT_CONSTEXPR auto sv = string_view(S());
|
||||||
FMT_CONSTEXPR int ignore =
|
FMT_CONSTEXPR int unused =
|
||||||
(parse_format_string(sv, checker(sv, arg_pack())), 0);
|
(parse_format_string(sv, checker(sv, arg_pack())), 0);
|
||||||
detail::ignore_unused(ignore);
|
detail::ignore_unused(unused);
|
||||||
}
|
}
|
||||||
fstring(runtime_format_string<> fmt) : str(fmt.str) {}
|
fstring(runtime_format_string<> fmt) : str(fmt.str) {}
|
||||||
|
|
||||||
|
@@ -19,11 +19,11 @@ FMT_BEGIN_NAMESPACE
|
|||||||
// A compile-time string which is compiled into fast formatting code.
|
// A compile-time string which is compiled into fast formatting code.
|
||||||
FMT_EXPORT class compiled_string {};
|
FMT_EXPORT class compiled_string {};
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template <typename S>
|
template <typename S>
|
||||||
struct is_compiled_string : std::is_base_of<compiled_string, S> {};
|
struct is_compiled_string : std::is_base_of<compiled_string, S> {};
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a string literal `s` into a format string that will be parsed at
|
* Converts a string literal `s` into a format string that will be parsed at
|
||||||
* compile time and converted into efficient formatting code. Requires C++17
|
* compile time and converted into efficient formatting code. Requires C++17
|
||||||
@@ -41,16 +41,6 @@ struct is_compiled_string : std::is_base_of<compiled_string, S> {};
|
|||||||
# define FMT_COMPILE(s) FMT_STRING(s)
|
# define FMT_COMPILE(s) FMT_STRING(s)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
|
||||||
template <typename Char, size_t N, fmt::detail::fixed_string<Char, N> Str>
|
|
||||||
struct udl_compiled_string : compiled_string {
|
|
||||||
using char_type = Char;
|
|
||||||
constexpr explicit operator basic_string_view<char_type>() const {
|
|
||||||
return {Str.data, N - 1};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template <typename T, typename... Tail>
|
template <typename T, typename... Tail>
|
||||||
auto first(const T& value, const Tail&...) -> const T& {
|
auto first(const T& value, const Tail&...) -> const T& {
|
||||||
return value;
|
return value;
|
||||||
@@ -425,7 +415,7 @@ constexpr auto compile_format_string(S fmt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args, typename S,
|
template <typename... Args, typename S,
|
||||||
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
FMT_ENABLE_IF(is_compiled_string<S>::value)>
|
||||||
constexpr auto compile(S fmt) {
|
constexpr auto compile(S fmt) {
|
||||||
constexpr auto str = basic_string_view<typename S::char_type>(fmt);
|
constexpr auto str = basic_string_view<typename S::char_type>(fmt);
|
||||||
if constexpr (str.size() == 0) {
|
if constexpr (str.size() == 0) {
|
||||||
@@ -461,7 +451,7 @@ constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
FMT_ENABLE_IF(is_compiled_string<S>::value)>
|
||||||
FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
|
FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
|
||||||
Args&&... args) {
|
Args&&... args) {
|
||||||
if constexpr (std::is_same<typename S::char_type, char>::value) {
|
if constexpr (std::is_same<typename S::char_type, char>::value) {
|
||||||
@@ -488,7 +478,7 @@ FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputIt, typename S, typename... Args,
|
template <typename OutputIt, typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
FMT_ENABLE_IF(is_compiled_string<S>::value)>
|
||||||
FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
|
FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
|
||||||
constexpr auto compiled = detail::compile<Args...>(S());
|
constexpr auto compiled = detail::compile<Args...>(S());
|
||||||
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
|
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
|
||||||
@@ -503,7 +493,7 @@ FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <typename OutputIt, typename S, typename... Args,
|
template <typename OutputIt, typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
FMT_ENABLE_IF(is_compiled_string<S>::value)>
|
||||||
auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args)
|
auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args)
|
||||||
-> format_to_n_result<OutputIt> {
|
-> format_to_n_result<OutputIt> {
|
||||||
using traits = detail::fixed_buffer_traits;
|
using traits = detail::fixed_buffer_traits;
|
||||||
@@ -513,7 +503,7 @@ auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
FMT_ENABLE_IF(is_compiled_string<S>::value)>
|
||||||
FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args)
|
FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args)
|
||||||
-> size_t {
|
-> size_t {
|
||||||
auto buf = detail::counting_buffer<>();
|
auto buf = detail::counting_buffer<>();
|
||||||
@@ -522,7 +512,7 @@ FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
FMT_ENABLE_IF(is_compiled_string<S>::value)>
|
||||||
void print(std::FILE* f, const S& fmt, const Args&... args) {
|
void print(std::FILE* f, const S& fmt, const Args&... args) {
|
||||||
auto buf = memory_buffer();
|
auto buf = memory_buffer();
|
||||||
fmt::format_to(appender(buf), fmt, args...);
|
fmt::format_to(appender(buf), fmt, args...);
|
||||||
@@ -530,7 +520,7 @@ void print(std::FILE* f, const S& fmt, const Args&... args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
|
FMT_ENABLE_IF(is_compiled_string<S>::value)>
|
||||||
void print(const S& fmt, const Args&... args) {
|
void print(const S& fmt, const Args&... args) {
|
||||||
print(stdout, fmt, args...);
|
print(stdout, fmt, args...);
|
||||||
}
|
}
|
||||||
@@ -538,9 +528,7 @@ void print(const S& fmt, const Args&... args) {
|
|||||||
#if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
#if FMT_USE_NONTYPE_TEMPLATE_ARGS
|
||||||
inline namespace literals {
|
inline namespace literals {
|
||||||
template <detail::fixed_string Str> constexpr auto operator""_cf() {
|
template <detail::fixed_string Str> constexpr auto operator""_cf() {
|
||||||
using char_t = remove_cvref_t<decltype(Str.data[0])>;
|
return FMT_COMPILE(Str.data);
|
||||||
return detail::udl_compiled_string<char_t, sizeof(Str.data) / sizeof(char_t),
|
|
||||||
Str>();
|
|
||||||
}
|
}
|
||||||
} // namespace literals
|
} // namespace literals
|
||||||
#endif
|
#endif
|
||||||
|
@@ -2332,7 +2332,7 @@ template <typename Char, typename OutputIt, typename DecimalFP,
|
|||||||
typename Grouping = digit_grouping<Char>>
|
typename Grouping = digit_grouping<Char>>
|
||||||
FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
|
FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
|
||||||
const format_specs& specs, sign s,
|
const format_specs& specs, sign s,
|
||||||
locale_ref loc) -> OutputIt {
|
int exp_upper, locale_ref loc) -> OutputIt {
|
||||||
auto significand = f.significand;
|
auto significand = f.significand;
|
||||||
int significand_size = get_significand_size(f);
|
int significand_size = get_significand_size(f);
|
||||||
const Char zero = static_cast<Char>('0');
|
const Char zero = static_cast<Char>('0');
|
||||||
@@ -2348,7 +2348,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
|
|||||||
if (specs.type() == presentation_type::fixed) return false;
|
if (specs.type() == presentation_type::fixed) return false;
|
||||||
// Use the fixed notation if the exponent is in [exp_lower, exp_upper),
|
// Use the fixed notation if the exponent is in [exp_lower, exp_upper),
|
||||||
// e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation.
|
// e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation.
|
||||||
const int exp_lower = -4, exp_upper = 16;
|
const int exp_lower = -4;
|
||||||
return output_exp < exp_lower ||
|
return output_exp < exp_lower ||
|
||||||
output_exp >= (specs.precision > 0 ? specs.precision : exp_upper);
|
output_exp >= (specs.precision > 0 ? specs.precision : exp_upper);
|
||||||
};
|
};
|
||||||
@@ -2451,12 +2451,13 @@ template <typename Char> class fallback_digit_grouping {
|
|||||||
template <typename Char, typename OutputIt, typename DecimalFP>
|
template <typename Char, typename OutputIt, typename DecimalFP>
|
||||||
FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f,
|
FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f,
|
||||||
const format_specs& specs, sign s,
|
const format_specs& specs, sign s,
|
||||||
locale_ref loc) -> OutputIt {
|
int exp_upper, locale_ref loc) -> OutputIt {
|
||||||
if (is_constant_evaluated()) {
|
if (is_constant_evaluated()) {
|
||||||
return do_write_float<Char, OutputIt, DecimalFP,
|
return do_write_float<Char, OutputIt, DecimalFP,
|
||||||
fallback_digit_grouping<Char>>(out, f, specs, s, loc);
|
fallback_digit_grouping<Char>>(out, f, specs, s,
|
||||||
|
exp_upper, loc);
|
||||||
} else {
|
} else {
|
||||||
return do_write_float<Char>(out, f, specs, s, loc);
|
return do_write_float<Char>(out, f, specs, s, exp_upper, loc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3288,6 +3289,14 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision,
|
|||||||
return exp;
|
return exp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Numbers with exponents greater or equal to the returned value will use
|
||||||
|
// the exponential notation.
|
||||||
|
template <typename T> constexpr auto exp_upper() -> int {
|
||||||
|
return std::numeric_limits<T>::digits10 != 0
|
||||||
|
? min_of(16, std::numeric_limits<T>::digits10 + 1)
|
||||||
|
: 16;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Char, typename OutputIt, typename T>
|
template <typename Char, typename OutputIt, typename T>
|
||||||
FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
|
FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
|
||||||
locale_ref loc) -> OutputIt {
|
locale_ref loc) -> OutputIt {
|
||||||
@@ -3303,6 +3312,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
|
|||||||
if (specs.width != 0) --specs.width;
|
if (specs.width != 0) --specs.width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr int exp_upper = detail::exp_upper<T>();
|
||||||
int precision = specs.precision;
|
int precision = specs.precision;
|
||||||
if (precision < 0) {
|
if (precision < 0) {
|
||||||
if (specs.type() != presentation_type::none) {
|
if (specs.type() != presentation_type::none) {
|
||||||
@@ -3311,7 +3321,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
|
|||||||
// Use Dragonbox for the shortest format.
|
// Use Dragonbox for the shortest format.
|
||||||
using floaty = conditional_t<sizeof(T) >= sizeof(double), double, float>;
|
using floaty = conditional_t<sizeof(T) >= sizeof(double), double, float>;
|
||||||
auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
|
auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
|
||||||
return write_float<Char>(out, dec, specs, s, loc);
|
return write_float<Char>(out, dec, specs, s, exp_upper, loc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3339,7 +3349,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
|
|||||||
|
|
||||||
specs.precision = precision;
|
specs.precision = precision;
|
||||||
auto f = big_decimal_fp{buffer.data(), static_cast<int>(buffer.size()), exp};
|
auto f = big_decimal_fp{buffer.data(), static_cast<int>(buffer.size()), exp};
|
||||||
return write_float<Char>(out, f, specs, s, loc);
|
return write_float<Char>(out, f, specs, s, exp_upper, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char, typename OutputIt, typename T,
|
template <typename Char, typename OutputIt, typename T,
|
||||||
@@ -3366,7 +3376,7 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
|
|||||||
return write_nonfinite<Char>(out, std::isnan(value), specs, s);
|
return write_nonfinite<Char>(out, std::isnan(value), specs, s);
|
||||||
|
|
||||||
auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
|
auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
|
||||||
return write_float<Char>(out, dec, specs, s, {});
|
return write_float<Char>(out, dec, specs, s, exp_upper<T>(), {});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char, typename OutputIt, typename T,
|
template <typename Char, typename OutputIt, typename T,
|
||||||
|
@@ -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_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -40,22 +40,21 @@ template <typename Mutex>
|
|||||||
class dup_filter_sink : public dist_sink<Mutex> {
|
class dup_filter_sink : public dist_sink<Mutex> {
|
||||||
public:
|
public:
|
||||||
template <class Rep, class Period>
|
template <class Rep, class Period>
|
||||||
explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration,
|
explicit dup_filter_sink(std::chrono::duration<Rep, Period> max_skip_duration)
|
||||||
level::level_enum notification_level = level::info)
|
: max_skip_duration_{max_skip_duration} {}
|
||||||
: max_skip_duration_{max_skip_duration},
|
|
||||||
log_level_{notification_level} {}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::chrono::microseconds max_skip_duration_;
|
std::chrono::microseconds max_skip_duration_;
|
||||||
log_clock::time_point last_msg_time_;
|
log_clock::time_point last_msg_time_;
|
||||||
std::string last_msg_payload_;
|
std::string last_msg_payload_;
|
||||||
size_t skip_counter_ = 0;
|
size_t skip_counter_ = 0;
|
||||||
level::level_enum log_level_;
|
level::level_enum skipped_msg_log_level_ = spdlog::level::level_enum::off;
|
||||||
|
|
||||||
void sink_it_(const details::log_msg &msg) override {
|
void sink_it_(const details::log_msg &msg) override {
|
||||||
bool filtered = filter_(msg);
|
bool filtered = filter_(msg);
|
||||||
if (!filtered) {
|
if (!filtered) {
|
||||||
skip_counter_ += 1;
|
skip_counter_ += 1;
|
||||||
|
skipped_msg_log_level_ = msg.level;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +64,7 @@ protected:
|
|||||||
auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..",
|
auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..",
|
||||||
static_cast<unsigned>(skip_counter_));
|
static_cast<unsigned>(skip_counter_));
|
||||||
if (msg_size > 0 && static_cast<size_t>(msg_size) < sizeof(buf)) {
|
if (msg_size > 0 && static_cast<size_t>(msg_size) < sizeof(buf)) {
|
||||||
details::log_msg skipped_msg{msg.source, msg.logger_name, log_level_,
|
details::log_msg skipped_msg{msg.source, msg.logger_name, skipped_msg_log_level_,
|
||||||
string_view_t{buf, static_cast<size_t>(msg_size)}};
|
string_view_t{buf, static_cast<size_t>(msg_size)}};
|
||||||
dist_sink<Mutex>::sink_it_(skipped_msg);
|
dist_sink<Mutex>::sink_it_(skipped_msg);
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,6 @@
|
|||||||
#include <spdlog/fmt/fmt.h>
|
#include <spdlog/fmt/fmt.h>
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <chrono>
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -38,8 +37,8 @@ SPDLOG_INLINE rotating_file_sink<Mutex>::rotating_file_sink(
|
|||||||
throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
|
throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (max_files > 200000) {
|
if (max_files > MaxFiles) {
|
||||||
throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000");
|
throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed MaxFiles");
|
||||||
}
|
}
|
||||||
file_helper_.open(calc_filename(base_filename_, 0));
|
file_helper_.open(calc_filename(base_filename_, 0));
|
||||||
current_size_ = file_helper_.size(); // expensive. called only once
|
current_size_ = file_helper_.size(); // expensive. called only once
|
||||||
@@ -54,11 +53,12 @@ SPDLOG_INLINE rotating_file_sink<Mutex>::rotating_file_sink(
|
|||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename,
|
SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename,
|
||||||
std::size_t index) {
|
std::size_t index) {
|
||||||
if (index == 0u) {
|
if (index == 0U) {
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
filename_t basename, ext;
|
filename_t basename;
|
||||||
|
filename_t ext;
|
||||||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
||||||
return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}.{}{}")), basename, index, ext);
|
return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}.{}{}")), basename, index, ext);
|
||||||
}
|
}
|
||||||
@@ -74,6 +74,35 @@ SPDLOG_INLINE void rotating_file_sink<Mutex>::rotate_now() {
|
|||||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
rotate_();
|
rotate_();
|
||||||
}
|
}
|
||||||
|
template <typename Mutex>
|
||||||
|
SPDLOG_INLINE void rotating_file_sink<Mutex>::set_max_size(std::size_t max_size) {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
if (max_size == 0) {
|
||||||
|
throw_spdlog_ex("rotating sink set_max_size: max_size arg cannot be zero");
|
||||||
|
}
|
||||||
|
max_size_ = max_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Mutex>
|
||||||
|
SPDLOG_INLINE std::size_t rotating_file_sink<Mutex>::get_max_size() {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
return max_size_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Mutex>
|
||||||
|
SPDLOG_INLINE void rotating_file_sink<Mutex>::set_max_files(std::size_t max_files) {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
if (max_files > MaxFiles) {
|
||||||
|
throw_spdlog_ex("rotating sink set_max_files: max_files arg cannot exceed 200000");
|
||||||
|
}
|
||||||
|
max_files_ = max_files;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Mutex>
|
||||||
|
std::size_t rotating_file_sink<Mutex>::get_max_files() {
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
return max_files_;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
SPDLOG_INLINE void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
SPDLOG_INLINE void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
||||||
|
@@ -8,7 +8,6 @@
|
|||||||
#include <spdlog/details/synchronous_factory.h>
|
#include <spdlog/details/synchronous_factory.h>
|
||||||
#include <spdlog/sinks/base_sink.h>
|
#include <spdlog/sinks/base_sink.h>
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -21,6 +20,7 @@ namespace sinks {
|
|||||||
template <typename Mutex>
|
template <typename Mutex>
|
||||||
class rotating_file_sink final : public base_sink<Mutex> {
|
class rotating_file_sink final : public base_sink<Mutex> {
|
||||||
public:
|
public:
|
||||||
|
static constexpr size_t MaxFiles = 200000;
|
||||||
rotating_file_sink(filename_t base_filename,
|
rotating_file_sink(filename_t base_filename,
|
||||||
std::size_t max_size,
|
std::size_t max_size,
|
||||||
std::size_t max_files,
|
std::size_t max_files,
|
||||||
@@ -29,6 +29,10 @@ public:
|
|||||||
static filename_t calc_filename(const filename_t &filename, std::size_t index);
|
static filename_t calc_filename(const filename_t &filename, std::size_t index);
|
||||||
filename_t filename();
|
filename_t filename();
|
||||||
void rotate_now();
|
void rotate_now();
|
||||||
|
void set_max_size(std::size_t max_size);
|
||||||
|
std::size_t get_max_size();
|
||||||
|
void set_max_files(std::size_t max_files);
|
||||||
|
std::size_t get_max_files();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override;
|
void sink_it_(const details::log_msg &msg) override;
|
||||||
@@ -61,9 +65,8 @@ using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
|
|||||||
//
|
//
|
||||||
// factory functions
|
// factory functions
|
||||||
//
|
//
|
||||||
|
|
||||||
template <typename Factory = spdlog::synchronous_factory>
|
template <typename Factory = spdlog::synchronous_factory>
|
||||||
inline std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name,
|
std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name,
|
||||||
const filename_t &filename,
|
const filename_t &filename,
|
||||||
size_t max_file_size,
|
size_t max_file_size,
|
||||||
size_t max_files,
|
size_t max_files,
|
||||||
@@ -74,7 +77,7 @@ inline std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Factory = spdlog::synchronous_factory>
|
template <typename Factory = spdlog::synchronous_factory>
|
||||||
inline std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name,
|
std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name,
|
||||||
const filename_t &filename,
|
const filename_t &filename,
|
||||||
size_t max_file_size,
|
size_t max_file_size,
|
||||||
size_t max_files,
|
size_t max_files,
|
||||||
|
@@ -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 }
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#define SPDLOG_VER_MAJOR 1
|
#define SPDLOG_VER_MAJOR 1
|
||||||
#define SPDLOG_VER_MINOR 15
|
#define SPDLOG_VER_MINOR 15
|
||||||
#define SPDLOG_VER_PATCH 1
|
#define SPDLOG_VER_PATCH 2
|
||||||
|
|
||||||
#define SPDLOG_TO_VERSION(major, minor, patch) (major * 10000 + minor * 100 + patch)
|
#define SPDLOG_TO_VERSION(major, minor, patch) (major * 10000 + minor * 100 + patch)
|
||||||
#define SPDLOG_VERSION SPDLOG_TO_VERSION(SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH)
|
#define SPDLOG_VERSION SPDLOG_TO_VERSION(SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH)
|
||||||
|
@@ -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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -94,14 +94,11 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]") {
|
|||||||
// next logger can rename the first output file.
|
// next logger can rename the first output file.
|
||||||
spdlog::drop(logger->name());
|
spdlog::drop(logger->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
||||||
for (int i = 0; i < 10; ++i) {
|
for (int i = 0; i < 10; ++i) {
|
||||||
logger->info("Test message {}", i);
|
logger->info("Test message {}", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger->flush();
|
logger->flush();
|
||||||
|
|
||||||
require_message_count(ROTATING_LOG, 10);
|
require_message_count(ROTATING_LOG, 10);
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
@@ -141,3 +138,50 @@ TEST_CASE("rotating_file_logger4", "[rotating_logger]") {
|
|||||||
REQUIRE(get_filesize(ROTATING_LOG) > 0);
|
REQUIRE(get_filesize(ROTATING_LOG) > 0);
|
||||||
REQUIRE(get_filesize(ROTATING_LOG ".1") > 0);
|
REQUIRE(get_filesize(ROTATING_LOG ".1") > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test changing the max size of the rotating file sink
|
||||||
|
TEST_CASE("rotating_file_logger5", "[rotating_logger]") {
|
||||||
|
prepare_logdir();
|
||||||
|
size_t max_size = 5 * 1024;
|
||||||
|
size_t max_files = 2;
|
||||||
|
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
||||||
|
auto sink =
|
||||||
|
std::make_shared<spdlog::sinks::rotating_file_sink_st>(basename, max_size, max_files);
|
||||||
|
auto logger = std::make_shared<spdlog::logger>("rotating_sink_logger", sink);
|
||||||
|
logger->set_pattern("%v");
|
||||||
|
|
||||||
|
REQUIRE(sink->get_max_size() == max_size);
|
||||||
|
REQUIRE(sink->get_max_files() == max_files);
|
||||||
|
max_size = 7 * 1024;
|
||||||
|
max_files = 3;
|
||||||
|
|
||||||
|
sink->set_max_size(max_size);
|
||||||
|
sink->set_max_files(max_files);
|
||||||
|
REQUIRE(sink->get_max_size() == max_size);
|
||||||
|
REQUIRE(sink->get_max_files() == max_files);
|
||||||
|
|
||||||
|
const auto message = std::string(200, 'x');
|
||||||
|
assert(message.size() < max_size);
|
||||||
|
const auto n_messages = max_files * max_size / message.size();
|
||||||
|
for (size_t i = 0; i < n_messages; ++i) {
|
||||||
|
logger->info(message);
|
||||||
|
}
|
||||||
|
logger.reset(); // force flush and close the file
|
||||||
|
|
||||||
|
// validate that the files were rotated correctly with the new max size and max files
|
||||||
|
for (size_t i = 0; i <= max_files; i++) {
|
||||||
|
// calc filenames
|
||||||
|
// e.g. rotating_log, rotating_log.0 rotating_log.1, rotating_log.2, etc.
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << ROTATING_LOG;
|
||||||
|
if (i > 0) {
|
||||||
|
oss << '.' << i;
|
||||||
|
}
|
||||||
|
const auto filename = oss.str();
|
||||||
|
const auto filesize = get_filesize(filename);
|
||||||
|
REQUIRE(filesize <= max_size);
|
||||||
|
if (i > 0) {
|
||||||
|
REQUIRE(filesize >= max_size - message.size() - 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -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();
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
#include "test_sink.h"
|
#include "test_sink.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
using spdlog::memory_buf_t;
|
using spdlog::memory_buf_t;
|
||||||
using spdlog::details::to_string_view;
|
using spdlog::details::to_string_view;
|
||||||
|
|
||||||
@@ -19,6 +21,23 @@ static std::string log_to_str(const std::string &msg, const Args &...args) {
|
|||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// log to str and return it with time
|
||||||
|
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) {
|
||||||
|
std::ostringstream oss;
|
||||||
|
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
|
||||||
|
spdlog::logger oss_logger("pattern_tester", oss_sink);
|
||||||
|
oss_logger.set_level(spdlog::level::info);
|
||||||
|
|
||||||
|
oss_logger.set_formatter(
|
||||||
|
std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(args...)));
|
||||||
|
|
||||||
|
oss_logger.log(log_time, {}, spdlog::level::info, msg);
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("custom eol", "[pattern_formatter]") {
|
TEST_CASE("custom eol", "[pattern_formatter]") {
|
||||||
std::string msg = "Hello custom eol test";
|
std::string msg = "Hello custom eol test";
|
||||||
std::string eol = ";)";
|
std::string eol = ";)";
|
||||||
@@ -58,6 +77,15 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
|
|||||||
oss.str());
|
oss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("GMT offset ", "[pattern_formatter]") {
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
const auto now = std::chrono::system_clock::now();
|
||||||
|
const auto yesterday = now - 24h;
|
||||||
|
|
||||||
|
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc,
|
||||||
|
"\n") == "+00:00\n");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("color range test1", "[pattern_formatter]") {
|
TEST_CASE("color range test1", "[pattern_formatter]") {
|
||||||
auto formatter = std::make_shared<spdlog::pattern_formatter>(
|
auto formatter = std::make_shared<spdlog::pattern_formatter>(
|
||||||
"%^%v%$", spdlog::pattern_time_type::local, "\n");
|
"%^%v%$", spdlog::pattern_time_type::local, "\n");
|
||||||
|
@@ -50,9 +50,8 @@ void require_message_count(const std::string &filename, const std::size_t messag
|
|||||||
std::size_t get_filesize(const std::string &filename) {
|
std::size_t get_filesize(const std::string &filename) {
|
||||||
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
|
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
|
||||||
if (!ifs) {
|
if (!ifs) {
|
||||||
throw std::runtime_error("Failed open file ");
|
throw std::runtime_error("Failed open file " + filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
return static_cast<std::size_t>(ifs.tellg());
|
return static_cast<std::size_t>(ifs.tellg());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user