From 2d81721f119dafb04957ddccff3b87544f027a73 Mon Sep 17 00:00:00 2001 From: Stefano Caiazza Date: Sun, 8 Sep 2019 18:32:23 +0200 Subject: [PATCH 1/2] Brought the registry constructor in public --- include/spdlog/details/registry-inl.h | 1 - include/spdlog/details/registry.h | 10 +++++++--- tests/test_registry.cpp | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index 075a0c7e..c5f3e2f4 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -8,7 +8,6 @@ #endif #include "spdlog/common.h" -#include "spdlog/details/periodic_worker.h" #include "spdlog/logger.h" #include "spdlog/details/pattern_formatter.h" diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 35a117d9..628a7ea9 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -9,6 +9,7 @@ // This class is thread safe #include "spdlog/common.h" +#include "spdlog/details/periodic_worker.h" #include #include @@ -27,6 +28,10 @@ class periodic_worker; class registry { public: + // Default constructor + registry(); + ~registry() = default; + registry(const registry &) = delete; registry &operator=(const registry &) = delete; @@ -79,12 +84,11 @@ public: void set_automatic_registration(bool automatic_regsistration); + // The unique global instance of the registry, usefult to access it + // from the global scope. NOTE It doesn't make it a singleton static registry &instance(); private: - registry(); - ~registry() = default; - void throw_if_exists_(const std::string &logger_name); void register_logger_(std::shared_ptr new_logger); std::mutex logger_map_mutex_, flusher_mutex_; diff --git a/tests/test_registry.cpp b/tests/test_registry.cpp index 9759a505..6a4639af 100644 --- a/tests/test_registry.cpp +++ b/tests/test_registry.cpp @@ -114,3 +114,22 @@ TEST_CASE("disable automatic registration", "[registry]") spdlog::set_level(spdlog::level::info); spdlog::set_automatic_registration(true); } + +TEST_CASE("concrete registry and global instance", "[registry]") +{ + spdlog::details::registry Local{}; + auto& Global = spdlog::details::registry::instance(); + REQUIRE((&Global != &Local)); + + SECTION("register drop on local registry") + { + Local.drop_all(); + + auto sink = std::make_shared(); + auto new_logger = std::make_shared(tested_logger_name, std::move(sink)); + Local.initialize_logger(new_logger); + + REQUIRE(Local.get(tested_logger_name) != nullptr); + } + +} From 88ced28c59ef7ada023f299e180ff7fcd0d0ec19 Mon Sep 17 00:00:00 2001 From: Stefano Caiazza Date: Sun, 8 Sep 2019 18:58:21 +0200 Subject: [PATCH 2/2] Added the factory function to the registry directly --- include/spdlog/details/registry.h | 10 ++++++++++ include/spdlog/details/synchronous_factory.h | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 628a7ea9..8c277486 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -84,6 +84,16 @@ public: void set_automatic_registration(bool automatic_regsistration); + // Factory function to create a new logger and register it in this registry + template + std::shared_ptr create(std::string logger_name, SinkArgs&& ... args) + { + auto sink = std::make_shared(std::forward(args)...); + auto new_logger = std::make_shared(std::move(logger_name), std::move(sink)); + this->initialize_logger(new_logger); + return new_logger; + } + // The unique global instance of the registry, usefult to access it // from the global scope. NOTE It doesn't make it a singleton static registry &instance(); diff --git a/include/spdlog/details/synchronous_factory.h b/include/spdlog/details/synchronous_factory.h index 68f5c214..22328845 100644 --- a/include/spdlog/details/synchronous_factory.h +++ b/include/spdlog/details/synchronous_factory.h @@ -15,10 +15,8 @@ struct synchronous_factory template static std::shared_ptr create(std::string logger_name, SinkArgs &&... args) { - auto sink = std::make_shared(std::forward(args)...); - auto new_logger = std::make_shared(std::move(logger_name), std::move(sink)); - details::registry::instance().initialize_logger(new_logger); - return new_logger; + return details::registry::instance().create( + std::move(logger_name), std::forward(args)...); } }; } // namespace spdlog \ No newline at end of file