mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
@@ -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) {
|
||||
init_thread_pool(
|
||||
q_size, thread_count, [] {}, [] {});
|
||||
init_thread_pool(q_size, thread_count, [] {}, [] {});
|
||||
}
|
||||
|
||||
// get the global thread pool.
|
||||
|
@@ -33,7 +33,7 @@ SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name,
|
||||
// send the log message to the thread pool
|
||||
SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){
|
||||
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
|
||||
pool_ptr->post_log(shared_from_this(), msg, overflow_policy_);
|
||||
pool_ptr -> post_log(shared_from_this(), msg, overflow_policy_);
|
||||
}
|
||||
else {
|
||||
throw_spdlog_ex("async log: thread pool doesn't exist anymore");
|
||||
@@ -45,7 +45,7 @@ SPDLOG_LOGGER_CATCH(msg.source)
|
||||
// send flush request to the thread pool
|
||||
SPDLOG_INLINE void spdlog::async_logger::flush_(){
|
||||
SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){
|
||||
pool_ptr->post_flush(shared_from_this(), overflow_policy_);
|
||||
pool_ptr -> post_flush(shared_from_this(), overflow_policy_);
|
||||
}
|
||||
else {
|
||||
throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
|
||||
|
@@ -364,7 +364,7 @@ SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(SPDLOG_USE_STD_FORMAT) && __cpp_lib_format >= 202207L
|
||||
#if defined(SPDLOG_USE_STD_FORMAT) && __cpp_lib_format >= 202207L
|
||||
template <typename T, typename... Args>
|
||||
SPDLOG_CONSTEXPR_FUNC std::basic_string_view<T> to_string_view(
|
||||
std::basic_format_string<T, Args...> fmt) SPDLOG_NOEXCEPT {
|
||||
|
@@ -265,10 +265,10 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
|
||||
return offset;
|
||||
#else
|
||||
|
||||
#if defined(sun) || defined(__sun) || defined(_AIX) || \
|
||||
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
||||
#if defined(sun) || defined(__sun) || defined(_AIX) || \
|
||||
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
|
||||
(!defined(__APPLE__) && !defined(_BSD_SOURCE) && !defined(_GNU_SOURCE) && \
|
||||
(!defined(_POSIX_VERSION) || (_POSIX_VERSION < 202405L)))
|
||||
(!defined(_POSIX_VERSION) || (_POSIX_VERSION < 202405L)))
|
||||
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
||||
struct helper {
|
||||
static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),
|
||||
@@ -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
|
||||
int result_size =
|
||||
::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0);
|
||||
int result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0);
|
||||
|
||||
if (result_size > 0) {
|
||||
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) {
|
||||
assert(result_size == target.size());
|
||||
return;
|
||||
@@ -593,13 +592,13 @@ SPDLOG_INLINE bool fsync(FILE *fp) {
|
||||
// Do non-locking fwrite if possible by the os or use the regular locking fwrite
|
||||
// Return true on success.
|
||||
SPDLOG_INLINE bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) {
|
||||
#if defined(_WIN32) && defined(SPDLOG_FWRITE_UNLOCKED)
|
||||
#if defined(_WIN32) && defined(SPDLOG_FWRITE_UNLOCKED)
|
||||
return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes;
|
||||
#elif defined(SPDLOG_FWRITE_UNLOCKED)
|
||||
#elif defined(SPDLOG_FWRITE_UNLOCKED)
|
||||
return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes;
|
||||
#else
|
||||
#else
|
||||
return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace os
|
||||
|
@@ -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, [] {}) {}
|
||||
|
||||
SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n)
|
||||
: thread_pool(
|
||||
q_max_items, threads_n, [] {}, [] {}) {}
|
||||
: thread_pool(q_max_items, threads_n, [] {}, [] {}) {}
|
||||
|
||||
// message all threads to terminate gracefully join them
|
||||
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
|
||||
template <typename FormatContext, typename Container>
|
||||
auto format(const spdlog::details::dump_info<Container> &the_range, FormatContext &ctx) const
|
||||
-> decltype(ctx.out()) {
|
||||
auto format(const spdlog::details::dump_info<Container> &the_range,
|
||||
FormatContext &ctx) const -> decltype(ctx.out()) {
|
||||
SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF";
|
||||
SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef";
|
||||
const char *hex_chars = use_uppercase ? hex_upper : hex_lower;
|
||||
|
@@ -12,12 +12,14 @@
|
||||
|
||||
#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.
|
||||
// Note: Not supported in async mode (thread local storage - so the async thread pool have different copy).
|
||||
// MDC is a simple map of key->string values stored in thread local storage whose content will be
|
||||
// printed by the loggers. Note: Not supported in async mode (thread local storage - so the async
|
||||
// thread pool have different copy).
|
||||
//
|
||||
// Usage example:
|
||||
// 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 {
|
||||
class SPDLOG_API mdc {
|
||||
|
@@ -111,7 +111,8 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode_(color_mode mode
|
||||
}
|
||||
|
||||
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_);
|
||||
}
|
||||
|
||||
|
@@ -27,7 +27,7 @@ public:
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override { callback_(msg); }
|
||||
void flush_() override{}
|
||||
void flush_() override {}
|
||||
|
||||
private:
|
||||
custom_log_callback callback_;
|
||||
|
@@ -14,7 +14,6 @@
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <mutex>
|
||||
#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");
|
||||
}
|
||||
|
||||
if (max_files > 200000) {
|
||||
throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000");
|
||||
if (max_files > MaxFiles) {
|
||||
throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed MaxFiles");
|
||||
}
|
||||
file_helper_.open(calc_filename(base_filename_, 0));
|
||||
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>
|
||||
SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::calc_filename(const filename_t &filename,
|
||||
std::size_t index) {
|
||||
if (index == 0u) {
|
||||
if (index == 0U) {
|
||||
return filename;
|
||||
}
|
||||
|
||||
filename_t basename, ext;
|
||||
filename_t basename;
|
||||
filename_t ext;
|
||||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
||||
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_);
|
||||
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>
|
||||
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/sinks/base_sink.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
@@ -21,6 +20,7 @@ namespace sinks {
|
||||
template <typename Mutex>
|
||||
class rotating_file_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
static constexpr size_t MaxFiles = 200000;
|
||||
rotating_file_sink(filename_t base_filename,
|
||||
std::size_t max_size,
|
||||
std::size_t max_files,
|
||||
@@ -29,6 +29,10 @@ public:
|
||||
static filename_t calc_filename(const filename_t &filename, std::size_t index);
|
||||
filename_t filename();
|
||||
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:
|
||||
void sink_it_(const details::log_msg &msg) override;
|
||||
@@ -42,7 +46,7 @@ private:
|
||||
// log.3.txt -> delete
|
||||
void rotate_();
|
||||
|
||||
// delete the target if exists, and rename the src file to target
|
||||
// delete the target if exists, and rename the src file to target
|
||||
// return true on success, false otherwise.
|
||||
bool rename_file_(const filename_t &src_filename, const filename_t &target_filename);
|
||||
|
||||
@@ -61,25 +65,24 @@ using rotating_file_sink_st = rotating_file_sink<details::null_mutex>;
|
||||
//
|
||||
// factory functions
|
||||
//
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
size_t max_file_size,
|
||||
size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
std::shared_ptr<logger> rotating_logger_mt(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
size_t max_file_size,
|
||||
size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::rotating_file_sink_mt>(
|
||||
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
size_t max_file_size,
|
||||
size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
std::shared_ptr<logger> rotating_logger_st(const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
size_t max_file_size,
|
||||
size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::rotating_file_sink_st>(
|
||||
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
|
||||
}
|
||||
|
@@ -137,10 +137,10 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t
|
||||
#if defined(SPDLOG_UTF8_TO_WCHAR_CONSOLE)
|
||||
wmemory_buf_t wformatted;
|
||||
details::os::utf8_to_wstrbuf(string_view_t(formatted.data() + start, end - start),
|
||||
wformatted);
|
||||
wformatted);
|
||||
auto size = static_cast<DWORD>(wformatted.size());
|
||||
auto ignored = ::WriteConsoleW(static_cast<HANDLE>(out_handle_), wformatted.data(), size,
|
||||
nullptr, nullptr);
|
||||
nullptr, nullptr);
|
||||
#else
|
||||
auto size = static_cast<DWORD>(end - start);
|
||||
auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start,
|
||||
|
@@ -109,8 +109,8 @@
|
||||
//
|
||||
// #include <string_view>
|
||||
// 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
|
||||
// CRITICAL"sv, "OFF"sv }
|
||||
// #define SPDLOG_LEVEL_NAMES { "MY TRACE"sv, "MY DEBUG"sv, "MY INFO"sv, "MY WARNING"sv, "MY
|
||||
// ERROR"sv, "MY CRITICAL"sv, "OFF"sv }
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
Reference in New Issue
Block a user