From ed8d099607624c5864616f96a8c30037d8b7f769 Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 29 Aug 2019 01:05:23 +0300 Subject: [PATCH] Fixed #1197 --- include/spdlog/async_logger-inl.h | 7 +++++ include/spdlog/async_logger.h | 2 ++ include/spdlog/fmt/bin_to_hex.h | 1 - include/spdlog/logger-inl.h | 8 +++++ include/spdlog/logger.h | 3 ++ tests/test_misc.cpp | 51 ++++++++++++++++++++++++++++++- 6 files changed, 70 insertions(+), 2 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 93dbab7c..cfeda2de 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -83,3 +83,10 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_() SPDLOG_LOGGER_CATCH() } } + +SPDLOG_INLINE std::shared_ptr spdlog::async_logger::clone(std::string new_name) +{ + auto cloned = std::make_shared(*this); + cloned->name_ = std::move(new_name); + return cloned; +} diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index f98ba715..0a685186 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -49,6 +49,8 @@ public: async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr tp, async_overflow_policy overflow_policy = async_overflow_policy::block); + std::shared_ptr clone(std::string new_name) override; + protected: void sink_it_(const details::log_msg &msg) override; void flush_() override; diff --git a/include/spdlog/fmt/bin_to_hex.h b/include/spdlog/fmt/bin_to_hex.h index c6c9bda8..de126061 100644 --- a/include/spdlog/fmt/bin_to_hex.h +++ b/include/spdlog/fmt/bin_to_hex.h @@ -122,7 +122,6 @@ struct formatter> auto inserter = ctx.out(); #endif - for (auto &item : the_range) { auto ch = static_cast(item); diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index f6f05343..eae23b99 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -162,6 +162,14 @@ SPDLOG_INLINE void logger::set_error_handler(err_handler handler) custom_err_handler_ = handler; } +// create new logger with same sinks and configuration. +SPDLOG_INLINE std::shared_ptr logger::clone(std::string logger_name) +{ + auto cloned = std::make_shared(*this); + cloned->name_ = std::move(logger_name); + return cloned; +} + // protected methods SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) { diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 767abdbd..d18b6234 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -356,6 +356,9 @@ public: // error handler void set_error_handler(err_handler); + // create new logger with same sinks and configuration. + virtual std::shared_ptr clone(std::string logger_name); + protected: std::string name_; std::vector sinks_; diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index e5bc3b43..54878192 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -1,5 +1,7 @@ #include "includes.h" #include "test_sink.h" +#include "spdlog/fmt/bin_to_hex.h" + template std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info) @@ -92,7 +94,54 @@ TEST_CASE("periodic flush", "[periodic_flush]") spdlog::drop_all(); } -#include "spdlog/fmt/bin_to_hex.h" +TEST_CASE("clone-logger", "[clone]") +{ + using namespace spdlog; + auto test_sink = std::make_shared(); + auto logger = std::make_shared("orig", test_sink); + logger->set_pattern("%v"); + auto cloned = logger->clone("clone"); + + REQUIRE(cloned->name() == "clone"); + REQUIRE(logger->sinks() == cloned->sinks()); + REQUIRE(logger->level() == cloned->level()); + REQUIRE(logger->flush_level() == cloned->flush_level()); + logger->info("Some message 1"); + cloned->info("Some message 2"); + + REQUIRE(test_sink->lines().size()==2); + REQUIRE(test_sink->lines()[0] == "Some message 1"); + REQUIRE(test_sink->lines()[1] == "Some message 2"); + + spdlog::drop_all(); +} + +TEST_CASE("clone async", "[clone]") +{ + using namespace spdlog; + + 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"); + auto cloned = logger->clone("clone"); + + REQUIRE(cloned->name() == "clone"); + REQUIRE(logger->sinks() == cloned->sinks()); + REQUIRE(logger->level() == cloned->level()); + REQUIRE(logger->flush_level() == cloned->flush_level()); + + logger->info("Some message 1"); + cloned->info("Some message 2"); + + spdlog::details::os::sleep_for_millis(10); + + REQUIRE(test_sink->lines().size()==2); + REQUIRE(test_sink->lines()[0] == "Some message 1"); + REQUIRE(test_sink->lines()[1] == "Some message 2"); + + spdlog::drop_all(); +} TEST_CASE("to_hex", "[to_hex]") {