From 035a8ff0df2d242433e525513fb23fcbadfa0430 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 17 Sep 2021 17:16:55 +0300 Subject: [PATCH] Added logger::set_formatter template overload --- example/example.cpp | 6 +++--- include/spdlog/logger.h | 7 +++++++ tests/test_pattern_formatter.cpp | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 1b8597cb..a97c2749 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -51,9 +51,9 @@ int main(int, char *[]) spdlog::debug("This message should be displayed.."); // Customize msg format - spdlog::default_logger()->set_formatter(make_unique("[%H:%M:%S %z] [%^%L%$] [thread %t] %v")); + spdlog::default_logger()->set_formatter("[%H:%M:%S %z] [%^%L%$] [thread %t] %v"); spdlog::info("This an info message with custom format"); - spdlog::default_logger()->set_formatter(make_unique()); // back to default format + spdlog::default_logger()->set_formatter(); // back to default format spdlog::default_logger()->set_level(spdlog::level::info); try @@ -285,7 +285,7 @@ public: std::unique_ptr clone() const override { - return spdlog::details::make_unique(); + return make_unique(); } }; diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index c7ddeec8..acfb3764 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -279,6 +279,13 @@ public: // each sink will get a separate instance of the formatter object. void set_formatter(std::unique_ptr f); + + template + void set_formatter(Args &&...args) + { + set_formatter(details::make_unique(std::forward(args)...)); + } + // flush functions void flush(); void flush_on(level::level_enum log_level); diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 22b904e1..b47fd7b3 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -12,7 +12,7 @@ static std::string log_to_str(const std::string &msg, const Args &... args) spdlog::logger oss_logger("pattern_tester", oss_sink); oss_logger.set_level(spdlog::level::info); - oss_logger.set_formatter(std::unique_ptr(new spdlog::pattern_formatter(args...))); + oss_logger.set_formatter(args...); oss_logger.info(msg); return oss.str();