From 408a1620447e69ffda61568fd6595fd2716ba1f7 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 27 Aug 2019 01:16:57 +0300 Subject: [PATCH] Removed logger::clone() from API --- example/example.cpp | 9 ------- include/spdlog/async_logger-inl.h | 6 ----- include/spdlog/async_logger.h | 1 - include/spdlog/logger-inl.h | 9 ------- include/spdlog/logger.h | 26 ++++++++++++++------ include/spdlog/spdlog.h | 3 +-- tests/test_misc.cpp | 41 ------------------------------- 7 files changed, 19 insertions(+), 76 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index ce57fc9d..9a070c1e 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -47,7 +47,6 @@ int main(int, char *[]) basic_example(); rotating_example(); daily_example(); - clone_example(); async_example(); binary_example(); multi_sink_example(); @@ -106,14 +105,6 @@ void daily_example() auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); } -// Clone a logger and give it new name. -// Useful for creating component/subsystem loggers from some "root" logger. -void clone_example() -{ - auto network_logger = spdlog::default_logger()->clone("network"); - network_logger->info("Logging network stuff.."); -} - #include "spdlog/async.h" void async_example() { diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 96137921..f8dee643 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -78,9 +78,3 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_() spdlog::logger::flush_(); } -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 0a685186..f56e2dd1 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -49,7 +49,6 @@ 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; diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index c86ba426..cc7be86d 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -64,7 +64,6 @@ SPDLOG_INLINE void swap(logger &a, logger &b) a.swap(b); } - SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const { return msg_level >= level_.load(std::memory_order_relaxed); @@ -159,14 +158,6 @@ 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 09815447..bce77396 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -81,7 +81,7 @@ public: void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) { auto level_enabled = should_log(lvl); - if(!level_enabled && !tracer_) + if (!level_enabled && !tracer_) { return; } @@ -90,8 +90,14 @@ public: fmt::memory_buffer buf; fmt::format_to(buf, fmt, args...); details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - if (level_enabled) sink_it_(log_msg); - if (tracer_) backtrace_add_(log_msg); + if (level_enabled) + { + sink_it_(log_msg); + } + if (tracer_) + { + backtrace_add_(log_msg); + } } SPDLOG_LOGGER_CATCH() } @@ -149,15 +155,21 @@ public: void log(source_loc loc, level::level_enum lvl, const T &msg) { auto level_enabled = should_log(lvl); - if(!level_enabled && !tracer_) + if (!level_enabled && !tracer_) { return; } SPDLOG_TRY { details::log_msg log_msg(loc, name_, lvl, msg); - if (level_enabled) sink_it_(log_msg); - if (tracer_) backtrace_add_(log_msg); + if (level_enabled) + { + sink_it_(log_msg); + } + if (tracer_) + { + backtrace_add_(log_msg); + } } SPDLOG_LOGGER_CATCH() } @@ -340,8 +352,6 @@ 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_; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index cf20dba8..72ba8974 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -285,8 +285,7 @@ inline void critical(wstring_view_t fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...)\ - logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ +#define SPDLOG_LOGGER_CALL(logger, level, ...) logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index 7400dd8f..e5bc3b43 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -92,47 +92,6 @@ TEST_CASE("periodic flush", "[periodic_flush]") spdlog::drop_all(); } -TEST_CASE("clone-logger", "[clone]") -{ - using namespace spdlog; - - auto logger = spdlog::create("orig"); - 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"); - - auto test_sink = std::static_pointer_cast(cloned->sinks()[0]); - - spdlog::drop_all(); -} - -TEST_CASE("clone async", "[clone]") -{ - using namespace spdlog; - - auto logger = spdlog::create_async("orig"); - 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); - - auto test_sink = std::static_pointer_cast(cloned->sinks()[0]); - - spdlog::drop_all(); -} - #include "spdlog/fmt/bin_to_hex.h" TEST_CASE("to_hex", "[to_hex]")