From 117c966b04e7e144d6c8c18cdf8ddcab011748dc Mon Sep 17 00:00:00 2001 From: Stefano Caiazza Date: Tue, 19 Nov 2019 18:35:28 +0100 Subject: [PATCH] Implemented the async factory in the local registry --- include/spdlog/async.h | 23 ++--------------------- include/spdlog/details/registry.h | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/include/spdlog/async.h b/include/spdlog/async.h index afaf263f..29538b5b 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -24,10 +24,6 @@ namespace spdlog { -namespace details { -static const size_t default_async_q_size = 8192; -} - // async logger factory - creates async loggers backed with thread pool. // if a global thread pool doesn't already exist, create it with default queue // size of 8192 items and single thread. @@ -37,23 +33,8 @@ struct async_factory_impl template static std::shared_ptr create(std::string logger_name, SinkArgs &&... args) { - auto ®istry_inst = details::registry::instance(); - - // create global thread pool if not already exists.. - - auto &mutex = registry_inst.tp_mutex(); - std::lock_guard tp_lock(mutex); - auto tp = registry_inst.get_tp(); - if (tp == nullptr) - { - tp = std::make_shared(details::default_async_q_size, 1); - registry_inst.set_tp(tp); - } - - auto sink = std::make_shared(std::forward(args)...); - auto new_logger = std::make_shared(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy); - registry_inst.initialize_logger(new_logger); - return new_logger; + return details::registry::instance().create_async( + std::move(logger_name), OverflowPolicy, std::forward(args)...); } }; diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 53230280..11b80e7b 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -9,6 +9,7 @@ // This class is thread safe #include +#include #include #include @@ -19,11 +20,14 @@ namespace spdlog { class logger; +class async_logger; namespace details { class thread_pool; class periodic_worker; +static const size_t default_async_q_size = 8192; + class registry { public: @@ -93,6 +97,25 @@ public: return new_logger; } + template + std::shared_ptr create_async(std::string logger_name, + async_overflow_policy OverflowPolicy, + SinkArgs &&... args) + { + // create global thread pool if not already exists.. + + std::lock_guard tp_lock(tp_mutex_); + if (tp_ == nullptr) + { + tp_ = std::make_shared(details::default_async_q_size, 1); + } + + auto sink = std::make_shared(std::forward(args)...); + auto new_logger = std::make_shared(std::move(logger_name), std::move(sink), std::move(tp_), OverflowPolicy); + 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();