From 83ac0792271ae6973683208b7a3c859e05085a0e Mon Sep 17 00:00:00 2001 From: Your Full Name Date: Tue, 7 Sep 2021 16:27:57 +0300 Subject: [PATCH] default_formatter class --- CMakeLists.txt | 2 +- bench/async_bench.cpp | 4 +- bench/bench.cpp | 6 +- bench/formatter-bench.cpp | 7 +- example/example.cpp | 17 ++-- include/spdlog/default_formatter.h | 113 ++++++++++++++++++++++ include/spdlog/details/registry-inl.h | 2 +- include/spdlog/logger-inl.h | 6 -- include/spdlog/logger.h | 2 - include/spdlog/pattern_formatter-inl.h | 105 -------------------- include/spdlog/pattern_formatter.h | 105 -------------------- include/spdlog/sinks/ansicolor_sink-inl.h | 9 +- include/spdlog/sinks/ansicolor_sink.h | 1 - include/spdlog/sinks/base_sink-inl.h | 14 +-- include/spdlog/sinks/base_sink.h | 2 - include/spdlog/sinks/dist_sink.h | 5 - include/spdlog/sinks/sink.h | 1 - include/spdlog/sinks/stdout_sinks-inl.h | 11 +-- include/spdlog/sinks/stdout_sinks.h | 2 - include/spdlog/sinks/wincolor_sink-inl.h | 6 -- include/spdlog/sinks/wincolor_sink.h | 1 - include/spdlog/spdlog-inl.h | 5 - include/spdlog/spdlog.h | 5 +- tests/includes.h | 5 +- tests/test_backtrace.cpp | 4 +- tests/test_dup_filter.cpp | 2 +- tests/test_errors.cpp | 2 +- tests/test_file_logging.cpp | 4 +- tests/test_macros.cpp | 2 +- tests/test_misc.cpp | 10 +- tests/test_pattern_formatter.cpp | 10 +- tests/test_stdout_api.cpp | 16 +-- tests/test_stopwatch.cpp | 2 +- tests/test_time_point.cpp | 2 +- 34 files changed, 174 insertions(+), 316 deletions(-) create mode 100644 include/spdlog/default_formatter.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 58e76f52..5b9a0bee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,7 +128,7 @@ message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) # --------------------------------------------------------------------------------------- # Static/Shared library (shared not supported in windows yet) # --------------------------------------------------------------------------------------- -set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp src/color_sinks.cpp src/file_sinks.cpp src/async.cpp src/cfg.cpp) +set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp src/color_sinks.cpp src/file_sinks.cpp src/async.cpp src/cfg.cpp include/spdlog/default_formatter.h) if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO) list(APPEND SPDLOG_SRCS src/fmt.cpp) diff --git a/bench/async_bench.cpp b/bench/async_bench.cpp index 99d8ecb2..df4dacfe 100644 --- a/bench/async_bench.cpp +++ b/bench/async_bench.cpp @@ -7,6 +7,7 @@ // bench.cpp : spdlog benchmarks // #include "spdlog/spdlog.h" +#include "spdlog/pattern_formatter.h" #include "spdlog/async.h" #include "spdlog/sinks/basic_file_sink.h" @@ -69,6 +70,7 @@ void verify_file(const char *filename, int expected_count) int main(int argc, char *argv[]) { + using spdlog::details::make_unique; // for pre c++14 int howmany = 1000000; int queue_size = std::min(howmany + 2, 8192); @@ -77,7 +79,7 @@ int main(int argc, char *argv[]) try { - spdlog::set_pattern("[%^%l%$] %v"); + spdlog::set_formatter(make_unique("[%^%l%$] %v")); if (argc == 1) { spdlog::info("Usage: {} ", argv[0]); diff --git a/bench/bench.cpp b/bench/bench.cpp index 5a4fc39b..88ce46b2 100644 --- a/bench/bench.cpp +++ b/bench/bench.cpp @@ -7,6 +7,7 @@ // bench.cpp : spdlog benchmarks // #include "spdlog/spdlog.h" +#include "spdlog/pattern_formatter.h" #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/null_sink.h" @@ -110,8 +111,9 @@ void bench_single_threaded(int iters) int main(int argc, char *argv[]) { + using spdlog::details::make_unique; // for pre c++14 spdlog::set_automatic_registration(false); - spdlog::default_logger()->set_pattern("[%^%l%$] %v"); + spdlog::default_logger()->set_formatter(make_unique("[%^%l%$] %v")); int iters = 250000; size_t threads = 4; try @@ -243,4 +245,4 @@ void bench_c_string(int howmany, std::shared_ptr log) spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany / delta_d)); } -*/ \ No newline at end of file +*/ diff --git a/bench/formatter-bench.cpp b/bench/formatter-bench.cpp index 8302acf9..dc9c7872 100644 --- a/bench/formatter-bench.cpp +++ b/bench/formatter-bench.cpp @@ -6,6 +6,7 @@ #include "benchmark/benchmark.h" #include "spdlog/spdlog.h" +#include "spdlog/default_formatter.h" #include "spdlog/pattern_formatter.h" void bench_formatter(benchmark::State &state, std::string pattern) @@ -15,7 +16,8 @@ void bench_formatter(benchmark::State &state, std::string pattern) { formatter = spdlog::details::make_unique(); } - else { + else + { formatter = spdlog::details::make_unique(pattern); } @@ -67,7 +69,8 @@ void bench_formatters() int main(int argc, char *argv[]) { - spdlog::set_pattern("[%^%l%$] %v"); + using spdlog::details::make_unique; // for pre c++14 + spdlog::set_formatter(make_unique("[%^%l%$] %v")); if (argc != 2) { spdlog::error("Usage: {} (or \"all\" to bench all)", argv[0]); diff --git a/example/example.cpp b/example/example.cpp index 3fbde21a..1c441076 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -26,6 +26,10 @@ void custom_flags_example(); #include "spdlog/spdlog.h" #include "spdlog/cfg/env.h" // support for loading levels from the environment variable #include "spdlog/fmt/ostr.h" // support for user defined types +#include "spdlog/pattern_formatter.h" +#include "spdlog/default_formatter.h" + +using spdlog::details::make_unique; // for pre c++14 int main(int, char *[]) { @@ -47,9 +51,10 @@ int main(int, char *[]) spdlog::debug("This message should be displayed.."); // Customize msg format for all loggers - spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v"); + auto formatter = make_unique("[multi_sink_example] [%^%l%$] %v"); + spdlog::set_formatter(std::move(formatter)); spdlog::info("This an info message with custom format"); - spdlog::set_pattern("%+"); // back to default format + spdlog::set_formatter(make_unique()); // back to default format spdlog::set_level(spdlog::level::info); // Backtrace support @@ -217,13 +222,14 @@ void udp_example() my_logger->info("hello world"); } +#include // A logger with multiple sinks (stdout and file) - each with a different format and log level. void multi_sink_example() { auto console_sink = std::make_shared(); console_sink->set_level(spdlog::level::warn); - console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v"); - + auto formatter = make_unique("[multi_sink_example] [%^%l%$] %v"); + console_sink->set_formatter(std::move(formatter)); auto file_sink = std::make_shared("logs/multisink.txt", true); file_sink->set_level(spdlog::level::trace); @@ -299,8 +305,7 @@ public: void custom_flags_example() { - using spdlog::details::make_unique; // for pre c++14 - auto formatter = make_unique(); + auto formatter = make_unique(""); formatter->add_flag('*').set_pattern("[%n] [%*] [%^%l%$] %v"); spdlog::set_formatter(std::move(formatter)); } diff --git a/include/spdlog/default_formatter.h b/include/spdlog/default_formatter.h new file mode 100644 index 00000000..8c05beea --- /dev/null +++ b/include/spdlog/default_formatter.h @@ -0,0 +1,113 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include + +// Default spdlog formatter +// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v +namespace spdlog { +class default_formatter final : public formatter +{ +public: + default_formatter() = default; + ~default_formatter() = default; + + std::unique_ptr clone() const override + { + return details::make_unique(); + } + + void format(const details::log_msg &msg, memory_buf_t &dest) override + { + // 30ns + using std::chrono::duration_cast; + using std::chrono::milliseconds; + using std::chrono::seconds; + // cache the date/time part for the next second. + auto duration = msg.time.time_since_epoch(); + auto secs = duration_cast(duration); + //[2021-08-23 00:57:12.310] [info] Welcome to spdlog version 1.9.2 ! + if (cache_timestamp_ != secs || cached_datetime_.size() == 0) + { + auto tm_time = details::os::localtime(log_clock::to_time_t(msg.time)); + cached_datetime_.clear(); + cached_datetime_.push_back('['); + details::fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_); + cached_datetime_.push_back('-'); + + details::fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_); + cached_datetime_.push_back('-'); + + details::fmt_helper::pad2(tm_time.tm_mday, cached_datetime_); + cached_datetime_.push_back(' '); + + details::fmt_helper::pad2(tm_time.tm_hour, cached_datetime_); + cached_datetime_.push_back(':'); + + details::fmt_helper::pad2(tm_time.tm_min, cached_datetime_); + cached_datetime_.push_back(':'); + + details::fmt_helper::pad2(tm_time.tm_sec, cached_datetime_); + cached_datetime_.push_back('.'); + + cache_timestamp_ = secs; + } + // 32ns + + dest.append(cached_datetime_.begin(), cached_datetime_.end()); + // 36ns + auto millis = details::fmt_helper::time_fraction(msg.time); + // 40ns + + details::fmt_helper::pad3(static_cast(millis.count()), dest); + + // 45ns + + dest.push_back(']'); + dest.push_back(' '); + // 49ns + + // append logger name if exists + if (msg.logger_name.size() > 0) + { + dest.push_back('['); + details::fmt_helper::append_string_view(msg.logger_name, dest); + dest.push_back(']'); + dest.push_back(' '); + } + // 56ns + + dest.push_back('['); + // wrap the level name with color + msg.color_range_start = dest.size(); + // fmt_helper::append_string_view(level::to_c_str(msg.level), dest); + details::fmt_helper::append_string_view(level::to_string_view(msg.level), dest); + msg.color_range_end = dest.size(); + dest.push_back(']'); + dest.push_back(' '); + + // add source location if present + + if (!msg.source.empty()) + { + dest.push_back('['); + // const char *filename = details::short_filename_formatter::basename(msg.source.filename); + // details::fmt_helper::append_string_view(filename, dest); + dest.push_back(':'); + details::fmt_helper::append_int(msg.source.line, dest); + dest.push_back(']'); + dest.push_back(' '); + } + details::fmt_helper::append_string_view(msg.payload, dest); + details::fmt_helper::append_string_view(details::os::default_eol, dest); + } + +private: + std::chrono::seconds cache_timestamp_{0}; + memory_buf_t cached_datetime_; +}; +} // namespace spdlog diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 1994ed76..f62d5cdc 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER // support for the default stdout color logger diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 411f2cb5..d7f94bd2 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -98,12 +98,6 @@ SPDLOG_INLINE void logger::set_formatter(std::unique_ptr f) } } -SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type) -{ - auto new_formatter = details::make_unique(std::move(pattern), time_type); - set_formatter(std::move(new_formatter)); -} - // create new backtrace sink and move to it all our child sinks SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages) { diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index eea0afc2..e030c925 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -291,8 +291,6 @@ public: // each sink will get a separate instance of the formatter object. void set_formatter(std::unique_ptr f); - void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); - // backtrace support. // efficiently store all debug/trace messages in a circular buffer until needed for debugging. void enable_backtrace(size_t n_messages); diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index ec727032..118f7626 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -924,93 +924,6 @@ private: log_clock::time_point last_message_time_; }; -// Full info formatter -// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v -class full_formatter final : public flag_formatter -{ -public: - explicit full_formatter(padding_info padinfo) - : flag_formatter(padinfo) - {} - - void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override - { - using std::chrono::duration_cast; - using std::chrono::milliseconds; - using std::chrono::seconds; - - // cache the date/time part for the next second. - auto duration = msg.time.time_since_epoch(); - auto secs = duration_cast(duration); - - if (cache_timestamp_ != secs || cached_datetime_.size() == 0) - { - cached_datetime_.clear(); - cached_datetime_.push_back('['); - fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_); - cached_datetime_.push_back('-'); - - fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_); - cached_datetime_.push_back('-'); - - fmt_helper::pad2(tm_time.tm_mday, cached_datetime_); - cached_datetime_.push_back(' '); - - fmt_helper::pad2(tm_time.tm_hour, cached_datetime_); - cached_datetime_.push_back(':'); - - fmt_helper::pad2(tm_time.tm_min, cached_datetime_); - cached_datetime_.push_back(':'); - - fmt_helper::pad2(tm_time.tm_sec, cached_datetime_); - cached_datetime_.push_back('.'); - - cache_timestamp_ = secs; - } - dest.append(cached_datetime_.begin(), cached_datetime_.end()); - - auto millis = fmt_helper::time_fraction(msg.time); - fmt_helper::pad3(static_cast(millis.count()), dest); - dest.push_back(']'); - dest.push_back(' '); - - // append logger name if exists - if (msg.logger_name.size() > 0) - { - dest.push_back('['); - fmt_helper::append_string_view(msg.logger_name, dest); - dest.push_back(']'); - dest.push_back(' '); - } - - dest.push_back('['); - // wrap the level name with color - msg.color_range_start = dest.size(); - // fmt_helper::append_string_view(level::to_c_str(msg.level), dest); - fmt_helper::append_string_view(level::to_string_view(msg.level), dest); - msg.color_range_end = dest.size(); - dest.push_back(']'); - dest.push_back(' '); - - // add source location if present - if (!msg.source.empty()) - { - dest.push_back('['); - const char *filename = details::short_filename_formatter::basename(msg.source.filename); - fmt_helper::append_string_view(filename, dest); - dest.push_back(':'); - fmt_helper::append_int(msg.source.line, dest); - dest.push_back(']'); - dest.push_back(' '); - } - // fmt_helper::append_string_view(msg.msg(), dest); - fmt_helper::append_string_view(msg.payload, dest); - } - -private: - std::chrono::seconds cache_timestamp_{0}; - memory_buf_t cached_datetime_; -}; } // namespace details @@ -1026,17 +939,6 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter( compile_pattern_(pattern_); } -// use by default full formatter for if pattern is not given -SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eol) - : pattern_("%+") - , eol_(std::move(eol)) - , pattern_time_type_(time_type) - , last_log_secs_(0) -{ - std::memset(&cached_tm_, 0, sizeof(cached_tm_)); - formatters_.push_back(details::make_unique(details::padding_info{})); -} - SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const { custom_flags cloned_custom_formatters; @@ -1095,10 +997,6 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i // process built-in flags switch (flag) { - case ('+'): // default formatter - formatters_.push_back(details::make_unique(padding)); - break; - case 'n': // logger name formatters_.push_back(details::make_unique>(padding)); break; @@ -1276,8 +1174,6 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i formatters_.push_back((std::move(unknown_flag))); } // fix issue #1617 (prev char was '!' and should have been treated as funcname flag instead of truncating flag) - // spdlog::set_pattern("[%10!] %v") => "[ main] some message" - // spdlog::set_pattern("[%3!!] %v") => "[mai] some message" else { padding.truncate_ = false; @@ -1285,7 +1181,6 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i unknown_flag->add_ch(flag); formatters_.push_back((std::move(unknown_flag))); } - break; } } diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index ec060d0a..8658e875 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -83,9 +83,6 @@ public: explicit pattern_formatter(std::string pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol, custom_flags custom_user_flags = custom_flags()); - // use default pattern is not given - explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol); - pattern_formatter(const pattern_formatter &other) = delete; pattern_formatter &operator=(const pattern_formatter &other) = delete; @@ -121,108 +118,6 @@ private: void compile_pattern_(const std::string &pattern); }; -// Full info formatter -// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v -class default_formatter final : public formatter -{ -public: - default_formatter() = default; - ~default_formatter() = default; - - std::unique_ptr clone() const override - { - return details::make_unique(); - } - - void format(const details::log_msg &msg, memory_buf_t &dest) override - { - // 30ns - using std::chrono::duration_cast; - using std::chrono::milliseconds; - using std::chrono::seconds; - // cache the date/time part for the next second. - auto duration = msg.time.time_since_epoch(); - auto secs = duration_cast(duration); - //[2021-08-23 00:57:12.310] [info] Welcome to spdlog version 1.9.2 ! - if (cache_timestamp_ != secs || cached_datetime_.size() == 0) - { - auto tm_time = details::os::localtime(log_clock::to_time_t(msg.time)); - cached_datetime_.clear(); - cached_datetime_.push_back('['); - details::fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_); - cached_datetime_.push_back('-'); - - details::fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_); - cached_datetime_.push_back('-'); - - details::fmt_helper::pad2(tm_time.tm_mday, cached_datetime_); - cached_datetime_.push_back(' '); - - details::fmt_helper::pad2(tm_time.tm_hour, cached_datetime_); - cached_datetime_.push_back(':'); - - details::fmt_helper::pad2(tm_time.tm_min, cached_datetime_); - cached_datetime_.push_back(':'); - - details::fmt_helper::pad2(tm_time.tm_sec, cached_datetime_); - cached_datetime_.push_back('.'); - - cache_timestamp_ = secs; - } - // 32ns - - dest.append(cached_datetime_.begin(), cached_datetime_.end()); - // 36ns - auto millis = details::fmt_helper::time_fraction(msg.time); - // 40ns - - details::fmt_helper::pad3(static_cast(millis.count()), dest); - - // 45ns - - dest.push_back(']'); - dest.push_back(' '); - // 49ns - - // append logger name if exists - if (msg.logger_name.size() > 0) - { - dest.push_back('['); - details::fmt_helper::append_string_view(msg.logger_name, dest); - dest.push_back(']'); - dest.push_back(' '); - } - // 56ns - - dest.push_back('['); - // wrap the level name with color - msg.color_range_start = dest.size(); - // fmt_helper::append_string_view(level::to_c_str(msg.level), dest); - details::fmt_helper::append_string_view(level::to_string_view(msg.level), dest); - msg.color_range_end = dest.size(); - dest.push_back(']'); - dest.push_back(' '); - - // add source location if present - - if (!msg.source.empty()) - { - dest.push_back('['); - // const char *filename = details::short_filename_formatter::basename(msg.source.filename); - // details::fmt_helper::append_string_view(filename, dest); - dest.push_back(':'); - details::fmt_helper::append_int(msg.source.line, dest); - dest.push_back(']'); - dest.push_back(' '); - } - details::fmt_helper::append_string_view(msg.payload, dest); - details::fmt_helper::append_string_view(details::os::default_eol, dest); - } - -private: - std::chrono::seconds cache_timestamp_{0}; - memory_buf_t cached_datetime_; -}; } // namespace spdlog diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 3b19abd1..1e7a9c3c 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -7,7 +7,7 @@ # include #endif -#include +#include #include namespace spdlog { @@ -72,13 +72,6 @@ SPDLOG_INLINE void ansicolor_sink::flush() fflush(target_file_); } -template -SPDLOG_INLINE void ansicolor_sink::set_pattern(const std::string &pattern) -{ - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} - template SPDLOG_INLINE void ansicolor_sink::set_formatter(std::unique_ptr sink_formatter) { diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 39d966bc..4bb08b7d 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -41,7 +41,6 @@ public: void log(const details::log_msg &msg) override; void flush() override; - void set_pattern(const std::string &pattern) final; void set_formatter(std::unique_ptr sink_formatter) override; // Formatting codes diff --git a/include/spdlog/sinks/base_sink-inl.h b/include/spdlog/sinks/base_sink-inl.h index 63380c20..8c5bce40 100644 --- a/include/spdlog/sinks/base_sink-inl.h +++ b/include/spdlog/sinks/base_sink-inl.h @@ -8,7 +8,7 @@ #endif #include -#include +#include #include @@ -36,13 +36,6 @@ void SPDLOG_INLINE spdlog::sinks::base_sink::flush() flush_(); } -template -void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern(const std::string &pattern) -{ - std::lock_guard lock(mutex_); - set_pattern_(pattern); -} - template void SPDLOG_INLINE spdlog::sinks::base_sink::set_formatter(std::unique_ptr sink_formatter) { @@ -50,11 +43,6 @@ void SPDLOG_INLINE spdlog::sinks::base_sink::set_formatter(std::unique_pt set_formatter_(std::move(sink_formatter)); } -template -void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern_(const std::string &pattern) -{ - set_formatter_(details::make_unique(pattern)); -} template void SPDLOG_INLINE spdlog::sinks::base_sink::set_formatter_(std::unique_ptr sink_formatter) diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index a0624e94..fd3beee1 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -31,7 +31,6 @@ public: void log(const details::log_msg &msg) final; void flush() final; - void set_pattern(const std::string &pattern) final; void set_formatter(std::unique_ptr sink_formatter) final; protected: @@ -41,7 +40,6 @@ protected: virtual void sink_it_(const details::log_msg &msg) = 0; virtual void flush_() = 0; - virtual void set_pattern_(const std::string &pattern); virtual void set_formatter_(std::unique_ptr sink_formatter); }; } // namespace sinks diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index 8fccb4ee..9652f8d7 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -74,11 +74,6 @@ protected: } } - void set_pattern_(const std::string &pattern) override - { - set_formatter_(details::make_unique(pattern)); - } - void set_formatter_(std::unique_ptr sink_formatter) override { base_sink::formatter_ = std::move(sink_formatter); diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index 0a28cccc..fe0cd641 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -15,7 +15,6 @@ public: virtual ~sink() = default; virtual void log(const details::log_msg &msg) = 0; virtual void flush() = 0; - virtual void set_pattern(const std::string &pattern) = 0; virtual void set_formatter(std::unique_ptr sink_formatter) = 0; void set_level(level::level_enum log_level); diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h index c14066b9..da49b5d1 100644 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ b/include/spdlog/sinks/stdout_sinks-inl.h @@ -8,7 +8,7 @@ #endif #include -#include +#include #include #ifdef _WIN32 @@ -32,7 +32,7 @@ template SPDLOG_INLINE stdout_sink_base::stdout_sink_base(FILE *file) : mutex_(ConsoleMutex::mutex()) , file_(file) - , formatter_(details::make_unique()) + , formatter_(details::make_unique()) { #ifdef _WIN32 // get windows handle from the FILE* object @@ -84,13 +84,6 @@ SPDLOG_INLINE void stdout_sink_base::flush() fflush(file_); } -template -SPDLOG_INLINE void stdout_sink_base::set_pattern(const std::string &pattern) -{ - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} - template SPDLOG_INLINE void stdout_sink_base::set_formatter(std::unique_ptr sink_formatter) { diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index 6fdc0de3..e281bae7 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -32,8 +32,6 @@ public: void log(const details::log_msg &msg) override; void flush() override; - void set_pattern(const std::string &pattern) override; - void set_formatter(std::unique_ptr sink_formatter) override; protected: diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index be3d80a2..95644d31 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -84,12 +84,6 @@ void SPDLOG_INLINE wincolor_sink::flush() // windows console always flushed? } -template -void SPDLOG_INLINE wincolor_sink::set_pattern(const std::string &pattern) -{ - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} template void SPDLOG_INLINE wincolor_sink::set_formatter(std::unique_ptr sink_formatter) diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 9b030fc1..81d5148d 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -34,7 +34,6 @@ public: void set_color(level::level_enum level, std::uint16_t color); void log(const details::log_msg &msg) final override; void flush() final override; - void set_pattern(const std::string &pattern) override final; void set_formatter(std::unique_ptr sink_formatter) override final; void set_color_mode(color_mode mode); diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h index 2b875fae..5cba3a59 100644 --- a/include/spdlog/spdlog-inl.h +++ b/include/spdlog/spdlog-inl.h @@ -27,11 +27,6 @@ SPDLOG_INLINE void set_formatter(std::unique_ptr formatter) details::registry::instance().set_formatter(std::move(formatter)); } -SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type) -{ - set_formatter(std::unique_ptr(new pattern_formatter(std::move(pattern), time_type))); -} - SPDLOG_INLINE void enable_backtrace(size_t n_messages) { details::registry::instance().enable_backtrace(n_messages); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 812896da..818d73e5 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -52,12 +52,9 @@ SPDLOG_API void initialize_logger(std::shared_ptr logger); SPDLOG_API std::shared_ptr get(const std::string &name); // Set global formatter. Each sink in each logger will get a clone of this object +// example: spdlog::set_formatter(std::make_unique("%Y-%m-%d %H:%M:%S.%e %l : %v")); SPDLOG_API void set_formatter(std::unique_ptr formatter); -// Set global format string. -// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v"); -SPDLOG_API void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); - // enable global backtrace support SPDLOG_API void enable_backtrace(size_t n_messages); diff --git a/tests/includes.h b/tests/includes.h index 7285873a..e89984d5 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -23,4 +23,7 @@ #include "spdlog/sinks/ostream_sink.h" #include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" -#include "spdlog/pattern_formatter.h" \ No newline at end of file +#include "spdlog/pattern_formatter.h" +#include "spdlog/default_formatter.h" + +using spdlog::details::make_unique; // for pre c++14 diff --git a/tests/test_backtrace.cpp b/tests/test_backtrace.cpp index 9504b82b..c7099df3 100644 --- a/tests/test_backtrace.cpp +++ b/tests/test_backtrace.cpp @@ -10,7 +10,7 @@ TEST_CASE("bactrace1", "[bactrace]") size_t backtrace_size = 5; spdlog::logger logger("test-backtrace", test_sink); - logger.set_pattern("%v"); + logger.set_formatter(make_unique("%v")); logger.enable_backtrace(backtrace_size); logger.info("info message"); @@ -41,7 +41,7 @@ TEST_CASE("bactrace-async", "[bactrace]") spdlog::init_thread_pool(120, 1); auto logger = std::make_shared("test-bactrace-async", test_sink, spdlog::thread_pool()); - logger->set_pattern("%v"); + logger->set_formatter(make_unique("%v")); logger->enable_backtrace(backtrace_size); logger->info("info message"); diff --git a/tests/test_dup_filter.cpp b/tests/test_dup_filter.cpp index 8ae2ee60..3b27d810 100644 --- a/tests/test_dup_filter.cpp +++ b/tests/test_dup_filter.cpp @@ -77,7 +77,7 @@ TEST_CASE("dup_filter_test5", "[dup_filter_sink]") dup_filter_sink_mt dup_sink{std::chrono::seconds{5}}; auto test_sink = std::make_shared(); - test_sink->set_pattern("%v"); + test_sink->set_formatter(make_unique("%v")); dup_sink.add_sink(test_sink); dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "message1"}); diff --git a/tests/test_errors.cpp b/tests/test_errors.cpp index bcc21d73..e48f035d 100644 --- a/tests/test_errors.cpp +++ b/tests/test_errors.cpp @@ -28,7 +28,7 @@ TEST_CASE("default_error_handler", "[errors]]") spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG); auto logger = spdlog::create("test-error", filename, true); - logger->set_pattern("%v"); + logger->set_formatter(make_unique("%v")); logger->info(fmt::runtime("Test message {} {}"), 1); logger->info("Test message {}", 2); logger->flush(); diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index eab524dc..b93c7da3 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -12,7 +12,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG); auto logger = spdlog::create("logger", filename); - logger->set_pattern("%v"); + logger->set_formatter(make_unique("%v")); logger->info("Test message {}", 1); logger->info("Test message {}", 2); @@ -29,7 +29,7 @@ TEST_CASE("flush_on", "[flush_on]]") spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG); auto logger = spdlog::create("logger", filename); - logger->set_pattern("%v"); + logger->set_formatter(make_unique("%v")); logger->set_level(spdlog::level::trace); logger->flush_on(spdlog::level::info); logger->trace("Should not be flushed"); diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index dd21ed19..64aca8b0 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -17,7 +17,7 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME); auto logger = spdlog::create("logger", filename); - logger->set_pattern("%v"); + logger->set_formatter(make_unique("%v")); logger->set_level(spdlog::level::trace); SPDLOG_LOGGER_TRACE(logger, "Test message 1"); diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index 1d67e879..b0951e42 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -11,7 +11,7 @@ std::string log_info(const T &what, spdlog::level::level_enum logger_level = spd spdlog::logger oss_logger("oss", oss_sink); oss_logger.set_level(logger_level); - oss_logger.set_pattern("%v"); + oss_logger.set_formatter(make_unique("%v")); oss_logger.info(what); return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol)); @@ -97,7 +97,7 @@ TEST_CASE("clone-logger", "[clone]") using spdlog::sinks::test_sink_mt; auto test_sink = std::make_shared(); auto logger = std::make_shared("orig", test_sink); - logger->set_pattern("%v"); + logger->set_formatter(make_unique("%v")); auto cloned = logger->clone("clone"); REQUIRE(cloned->name() == "clone"); @@ -120,7 +120,7 @@ TEST_CASE("clone async", "[clone]") spdlog::init_thread_pool(4, 1); auto test_sink = std::make_shared(); auto logger = std::make_shared("orig", test_sink, spdlog::thread_pool()); - logger->set_pattern("%v"); + logger->set_formatter(make_unique("%v")); auto cloned = logger->clone("clone"); REQUIRE(cloned->name() == "clone"); @@ -236,7 +236,7 @@ TEST_CASE("default logger API", "[default logger]") auto oss_sink = std::make_shared(oss); spdlog::set_default_logger(std::make_shared("oss", oss_sink)); - spdlog::set_pattern("*** %v"); + spdlog::set_formatter(make_unique("*** %v")); spdlog::default_logger()->set_level(spdlog::level::trace); spdlog::trace("hello trace"); @@ -267,5 +267,5 @@ TEST_CASE("default logger API", "[default logger]") spdlog::debug("should not be logged"); REQUIRE(oss.str().empty()); spdlog::drop_all(); - spdlog::set_pattern("%v"); + spdlog::set_formatter(make_unique("%v")); } diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 656f93a0..c4651672 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -263,7 +263,7 @@ TEST_CASE("padding_funcname", "[pattern_formatter]") TEST_CASE("clone-default-formatter", "[pattern_formatter]") { - auto formatter_1 = std::make_shared(); + auto formatter_1 = std::make_shared("%v"); auto formatter_2 = formatter_1->clone(); std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message"); @@ -351,7 +351,7 @@ public: // test clone with custom flag formatters TEST_CASE("clone-custom_formatter", "[pattern_formatter]") { - auto formatter_1 = std::make_shared(); + auto formatter_1 = std::make_shared(""); formatter_1->add_flag('t', "custom_output").set_pattern("[%n] [%t] %v"); auto formatter_2 = formatter_1->clone(); std::string logger_name = "logger-name"; @@ -423,7 +423,7 @@ TEST_CASE("full filename formatter", "[pattern_formatter]") TEST_CASE("custom flags", "[pattern_formatter]") { - auto formatter = std::make_shared(); + auto formatter = std::make_shared(""); formatter->add_flag('t', "custom1").add_flag('u', "custom2").set_pattern("[%n] [%t] [%u] %v"); memory_buf_t formatted; @@ -436,7 +436,7 @@ TEST_CASE("custom flags", "[pattern_formatter]") TEST_CASE("custom flags-padding", "[pattern_formatter]") { - auto formatter = std::make_shared(); + auto formatter = std::make_shared(""); formatter->add_flag('t', "custom1").add_flag('u', "custom2").set_pattern("[%n] [%t] [%5u] %v"); memory_buf_t formatted; @@ -449,7 +449,7 @@ TEST_CASE("custom flags-padding", "[pattern_formatter]") TEST_CASE("custom flags-exception", "[pattern_formatter]") { - auto formatter = std::make_shared(); + auto formatter = std::make_shared(""); formatter->add_flag('t', "throw_me").add_flag('u', "custom2").set_pattern("[%n] [%t] [%u] %v"); memory_buf_t formatted; diff --git a/tests/test_stdout_api.cpp b/tests/test_stdout_api.cpp index d55223ff..02a67d44 100644 --- a/tests/test_stdout_api.cpp +++ b/tests/test_stdout_api.cpp @@ -7,7 +7,7 @@ TEST_CASE("stdout_st", "[stdout]") { auto l = spdlog::stdout_logger_st("test"); - l->set_pattern("%+"); + l->set_formatter(make_unique("%v")); l->set_level(spdlog::level::trace); l->trace("Test stdout_st"); spdlog::drop_all(); @@ -16,7 +16,7 @@ TEST_CASE("stdout_st", "[stdout]") TEST_CASE("stdout_mt", "[stdout]") { auto l = spdlog::stdout_logger_mt("test"); - l->set_pattern("%+"); + l->set_formatter(make_unique()); l->set_level(spdlog::level::debug); l->debug("Test stdout_mt"); spdlog::drop_all(); @@ -25,7 +25,7 @@ TEST_CASE("stdout_mt", "[stdout]") TEST_CASE("stderr_st", "[stderr]") { auto l = spdlog::stderr_logger_st("test"); - l->set_pattern("%+"); + l->set_formatter(make_unique()); l->info("Test stderr_st"); spdlog::drop_all(); } @@ -33,7 +33,7 @@ TEST_CASE("stderr_st", "[stderr]") TEST_CASE("stderr_mt", "[stderr]") { auto l = spdlog::stderr_logger_mt("test"); - l->set_pattern("%+"); + l->set_formatter(make_unique()); l->info("Test stderr_mt"); l->warn("Test stderr_mt"); l->error("Test stderr_mt"); @@ -45,7 +45,7 @@ TEST_CASE("stderr_mt", "[stderr]") TEST_CASE("stdout_color_st", "[stdout]") { auto l = spdlog::stdout_color_st("test"); - l->set_pattern("%+"); + l->set_formatter(make_unique()); l->info("Test stdout_color_st"); spdlog::drop_all(); } @@ -53,7 +53,7 @@ TEST_CASE("stdout_color_st", "[stdout]") TEST_CASE("stdout_color_mt", "[stdout]") { auto l = spdlog::stdout_color_mt("test"); - l->set_pattern("%+"); + l->set_formatter(make_unique()); l->set_level(spdlog::level::trace); l->trace("Test stdout_color_mt"); spdlog::drop_all(); @@ -62,7 +62,7 @@ TEST_CASE("stdout_color_mt", "[stdout]") TEST_CASE("stderr_color_st", "[stderr]") { auto l = spdlog::stderr_color_st("test"); - l->set_pattern("%+"); + l->set_formatter(make_unique()); l->set_level(spdlog::level::debug); l->debug("Test stderr_color_st"); spdlog::drop_all(); @@ -71,7 +71,7 @@ TEST_CASE("stderr_color_st", "[stderr]") TEST_CASE("stderr_color_mt", "[stderr]") { auto l = spdlog::stderr_color_mt("test"); - l->set_pattern("%+"); + l->set_formatter(make_unique()); l->info("Test stderr_color_mt"); l->warn("Test stderr_color_mt"); l->error("Test stderr_color_mt"); diff --git a/tests/test_stopwatch.cpp b/tests/test_stopwatch.cpp index 71c4aab7..c33ff201 100644 --- a/tests/test_stopwatch.cpp +++ b/tests/test_stopwatch.cpp @@ -25,7 +25,7 @@ TEST_CASE("stopwatch2", "[stopwatch]") spdlog::stopwatch sw; spdlog::logger logger("test-stopwatch", test_sink); - logger.set_pattern("%v"); + logger.set_formatter(make_unique("%v")); std::this_thread::sleep_for(wait_duration); logger.info("{}", sw); auto val = std::stod(test_sink->lines()[0]); diff --git a/tests/test_time_point.cpp b/tests/test_time_point.cpp index bacff69a..38eeebcb 100644 --- a/tests/test_time_point.cpp +++ b/tests/test_time_point.cpp @@ -9,7 +9,7 @@ TEST_CASE("time_point1", "[time_point log_msg]") spdlog::source_loc source{}; std::chrono::system_clock::time_point tp{std::chrono::system_clock::now()}; - test_sink->set_pattern("%T.%F"); // interested in the time_point + test_sink->set_formatter(make_unique("%T.%F")); // all the following should have the same time test_sink->set_delay(std::chrono::milliseconds(10));