From c716aa2ef2e4dffd44c894fa8248dc6a57328ea9 Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 16 Sep 2021 17:05:40 +0300 Subject: [PATCH] removed set_pattern from logger --- example/example.cpp | 4 +++- include/spdlog/logger.h | 1 - include/spdlog/pattern_formatter-inl.h | 4 ++-- include/spdlog/pattern_formatter.h | 5 +++++ include/spdlog/spdlog.h | 4 +--- tests/test_pattern_formatter.cpp | 2 +- tests/test_stdout_api.cpp | 1 - 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index b9cd9d16..120eeca3 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -24,6 +24,7 @@ void udp_example(); void custom_flags_example(); #include "spdlog/spdlog.h" +#include "spdlog/default_formatter.h" #include "spdlog/pattern_formatter.h" #include "spdlog/cfg/env.h" // support for loading levels from the environment variable #include "spdlog/fmt/ostr.h" // support for user defined types @@ -50,7 +51,8 @@ int main(int, char *[]) // Customize msg format for all loggers spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v"); spdlog::info("This an info message with custom format"); - spdlog::set_pattern("%+"); // back to default format + using spdlog::details::make_unique; // for pre c++14 + spdlog::set_formatter(make_unique()); // back to default format spdlog::set_level(spdlog::level::info); // Backtrace support diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index cbae9564..e030c925 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -290,7 +290,6 @@ public: // set formatting for the sinks in this logger. // each sink will get a separate instance of the formatter object. void set_formatter(std::unique_ptr f); - void set_pattern(std::string pattern); // backtrace support. // efficiently store all debug/trace messages in a circular buffer until needed for debugging. diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 425a48a6..2e7bde16 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1294,8 +1294,8 @@ SPDLOG_INLINE void set_pattern(std::string pattern) set_formatter(details::make_unique(std::move(pattern))); } -SPDLOG_INLINE void logger::set_pattern(std::string pattern) +SPDLOG_INLINE void set_pattern(logger& logger, std::string pattern) { - this->set_formatter(details::make_unique(std::move(pattern))); + logger.set_formatter(details::make_unique(std::move(pattern))); } } // namespace spdlog diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index 8658e875..b39784c4 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -118,6 +118,11 @@ private: void compile_pattern_(const std::string &pattern); }; +// Set global log pattern. +SPDLOG_API void set_pattern(std::string pattern); + +// set pattern to logger +SPDLOG_API void set_pattern(logger& logger, std::string pattern); } // namespace spdlog diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 4c769499..f0b05765 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -53,11 +53,9 @@ 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")); +// Note: to use spdlog::set_pattern(format_string) include the "spdlog/pattern_formatter.h" file. SPDLOG_API void set_formatter(std::unique_ptr formatter); -// Set global log pattern. -SPDLOG_API void set_pattern(std::string pattern); - // enable global backtrace support SPDLOG_API void enable_backtrace(size_t n_messages); diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index c4651672..6fd8123d 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -278,7 +278,7 @@ TEST_CASE("clone-default-formatter", "[pattern_formatter]") TEST_CASE("clone-default-formatter2", "[pattern_formatter]") { - auto formatter_1 = std::make_shared("%+"); + auto formatter_1 = std::make_shared(); auto formatter_2 = formatter_1->clone(); std::string logger_name = "test"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message"); diff --git a/tests/test_stdout_api.cpp b/tests/test_stdout_api.cpp index 02a67d44..b50de162 100644 --- a/tests/test_stdout_api.cpp +++ b/tests/test_stdout_api.cpp @@ -84,7 +84,6 @@ TEST_CASE("stderr_color_mt", "[stderr]") TEST_CASE("wchar_api", "[stdout]") { auto l = spdlog::stdout_logger_st("wchar_logger"); - l->set_pattern("%+"); l->set_level(spdlog::level::trace); l->trace(L"Test wchar_api"); l->trace(L"Test wchar_api {}", L"param");