From b34d4d793ab6d98244f2e446b91d61461b408f05 Mon Sep 17 00:00:00 2001 From: Alexandre Petitjean Date: Tue, 26 May 2015 12:36:47 +0200 Subject: [PATCH 1/4] Add shared registry capability --- include/spdlog/details/registry.h | 22 +++++--- include/spdlog/details/registry_shared.h | 66 ++++++++++++++++++++++++ include/spdlog/details/spdlog_impl.h | 33 ++++++------ include/spdlog/tweakme.h | 6 +++ 4 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 include/spdlog/details/registry_shared.h diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index fd8e4be9..3bb2eacd 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -144,12 +144,15 @@ public: _async_mode = false; } - static registry_t& instance() + static registry_t* instance() { static registry_t s_instance; - return s_instance; + return &s_instance; } +protected: + registry_t() {} + private: void register_logger_impl(std::shared_ptr logger) { @@ -158,7 +161,6 @@ private: throw spdlog_ex("logger with name " + logger_name + " already exists"); _loggers[logger->name()] = logger; } - registry_t(){} registry_t(const registry_t&) = delete; registry_t& operator=(const registry_t&) = delete; Mutex _mutex; @@ -171,10 +173,14 @@ private: std::function _worker_warmup_cb = nullptr; std::chrono::milliseconds _flush_interval_ms; }; -#ifdef SPDLOG_NO_REGISTRY_MUTEX -typedef registry_t registry; -#else -typedef registry_t registry; -#endif + +#ifndef SPDLOG_USE_SHARED_REGISTRY +# ifdef SPDLOG_NO_REGISTRY_MUTEX + typedef registry_t registry; +# else + typedef registry_t registry; +# endif //SPDLOG_NO_REGISTRY_MUTEX +#endif //SPDLOG_USE_SHARED_REGISTRY + } } diff --git a/include/spdlog/details/registry_shared.h b/include/spdlog/details/registry_shared.h new file mode 100644 index 00000000..d20c92bd --- /dev/null +++ b/include/spdlog/details/registry_shared.h @@ -0,0 +1,66 @@ +/*************************************************************************/ +/* spdlog - an extremely fast and easy to use c++11 logging library. */ +/* Copyright (c) 2014 Gabi Melman. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#pragma once + +#include "registry.h" + +namespace spdlog +{ +namespace details +{ +template class registry_shared_t : public registry_t +{ +public: + static std::shared_ptr > instance() + { + if(!_instance) + _instance = std::make_shared >(); + return _instance; + } + + static void set_registry(std::shared_ptr > registry) + { + _instance = registry; + } + + registry_shared_t() : registry_t() {} + +private: + static std::shared_ptr > _instance; +}; + +#ifdef SPDLOG_USE_SHARED_REGISTRY +# ifdef SPDLOG_NO_REGISTRY_MUTEX + typedef registry_shared_t registry; +# else + typedef registry_shared_t registry; +# endif //SPDLOG_NO_REGISTRY_MUTEX +#endif //SPDLOG_USE_SHARED_REGISTRY + +template +std::shared_ptr > registry_shared_t::_instance = nullptr; + +} +} diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index cfd6f826..49b3650f 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -28,23 +28,24 @@ // Global registry functions // #include "registry.h" +#include "registry_shared.h" #include "../sinks/file_sinks.h" #include "../sinks/stdout_sinks.h" #include "../sinks/syslog_sink.h" inline void spdlog::register_logger(std::shared_ptr logger) { - return details::registry::instance().register_logger(logger); + return details::registry::instance()->register_logger(logger); } inline std::shared_ptr spdlog::get(const std::string& name) { - return details::registry::instance().get(name); + return details::registry::instance()->get(name); } inline void spdlog::drop(const std::string &name) { - details::registry::instance().drop(name); + details::registry::instance()->drop(name); } // Create multi/single threaded rotating file logger @@ -72,22 +73,22 @@ inline std::shared_ptr spdlog::daily_logger_st(const std::string // Create stdout/stderr loggers inline std::shared_ptr spdlog::stdout_logger_mt(const std::string& logger_name) { - return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_mt::instance()); + return details::registry::instance()->create(logger_name, spdlog::sinks::stdout_sink_mt::instance()); } inline std::shared_ptr spdlog::stdout_logger_st(const std::string& logger_name) { - return details::registry::instance().create(logger_name, spdlog::sinks::stdout_sink_st::instance()); + return details::registry::instance()->create(logger_name, spdlog::sinks::stdout_sink_st::instance()); } inline std::shared_ptr spdlog::stderr_logger_mt(const std::string& logger_name) { - return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_mt::instance()); + return details::registry::instance()->create(logger_name, spdlog::sinks::stderr_sink_mt::instance()); } inline std::shared_ptr spdlog::stderr_logger_st(const std::string& logger_name) { - return details::registry::instance().create(logger_name, spdlog::sinks::stderr_sink_st::instance()); + return details::registry::instance()->create(logger_name, spdlog::sinks::stderr_sink_st::instance()); } #ifdef __linux__ @@ -103,7 +104,7 @@ inline std::shared_ptr spdlog::syslog_logger(const std::string& inline std::shared_ptr spdlog::create(const std::string& logger_name, spdlog::sinks_init_list sinks) { - return details::registry::instance().create(logger_name, sinks); + return details::registry::instance()->create(logger_name, sinks); } @@ -111,44 +112,44 @@ template inline std::shared_ptr spdlog::create(const std::string& logger_name, const Args&... args) { sink_ptr sink = std::make_shared(args...); - return details::registry::instance().create(logger_name, { sink }); + return details::registry::instance()->create(logger_name, { sink }); } template inline std::shared_ptr spdlog::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end) { - return details::registry::instance().create(logger_name, sinks_begin, sinks_end); + return details::registry::instance()->create(logger_name, sinks_begin, sinks_end); } inline void spdlog::set_formatter(spdlog::formatter_ptr f) { - details::registry::instance().formatter(f); + details::registry::instance()->formatter(f); } inline void spdlog::set_pattern(const std::string& format_string) { - return details::registry::instance().set_pattern(format_string); + return details::registry::instance()->set_pattern(format_string); } inline void spdlog::set_level(level::level_enum log_level) { - return details::registry::instance().set_level(log_level); + return details::registry::instance()->set_level(log_level); } inline void spdlog::set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy, const std::function& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms) { - details::registry::instance().set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms); + details::registry::instance()->set_async_mode(queue_size, overflow_policy, worker_warmup_cb, flush_interval_ms); } inline void spdlog::set_sync_mode() { - details::registry::instance().set_sync_mode(); + details::registry::instance()->set_sync_mode(); } inline void spdlog::drop_all() { - details::registry::instance().drop_all(); + details::registry::instance()->drop_all(); } diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index b651658b..c66e1e27 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -72,3 +72,9 @@ // Note that upon creating a logger the registry is modified by spdlog.. // #define SPDLOG_NO_REGISTRY_MUTEX /////////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to enable use of a shared registry +// #define SPDLOG_USE_SHARED_REGISTRY +/////////////////////////////////////////////////////////////////////////////// From 5e207175c51787317d60ff7a8e0e43648e46eed8 Mon Sep 17 00:00:00 2001 From: Alexandre Petitjean Date: Tue, 26 May 2015 14:22:39 +0200 Subject: [PATCH 2/4] Simplify implementation --- include/spdlog/details/registry.h | 28 ++++++---- include/spdlog/details/registry_shared.h | 66 ------------------------ include/spdlog/details/spdlog_impl.h | 1 - include/spdlog/tweakme.h | 6 --- 4 files changed, 17 insertions(+), 84 deletions(-) delete mode 100644 include/spdlog/details/registry_shared.h diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 3bb2eacd..45dbd94f 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -144,16 +144,23 @@ public: _async_mode = false; } - static registry_t* instance() + static std::shared_ptr > instance() { - static registry_t s_instance; - return &s_instance; + if(!_instance) + _instance = std::make_shared>(); + return _instance; + } + + static void set_registry(std::shared_ptr > registry) + { + _instance = registry; } -protected: registry_t() {} private: + static std::shared_ptr > _instance; + void register_logger_impl(std::shared_ptr logger) { auto logger_name = logger->name(); @@ -173,14 +180,13 @@ private: std::function _worker_warmup_cb = nullptr; std::chrono::milliseconds _flush_interval_ms; }; - -#ifndef SPDLOG_USE_SHARED_REGISTRY -# ifdef SPDLOG_NO_REGISTRY_MUTEX - typedef registry_t registry; +#ifdef SPDLOG_NO_REGISTRY_MUTEX +typedef registry_t registry; # else - typedef registry_t registry; -# endif //SPDLOG_NO_REGISTRY_MUTEX -#endif //SPDLOG_USE_SHARED_REGISTRY +typedef registry_t registry; +#endif //SPDLOG_NO_REGISTRY_MUTEX + +std::shared_ptr registry::_instance = nullptr; } } diff --git a/include/spdlog/details/registry_shared.h b/include/spdlog/details/registry_shared.h deleted file mode 100644 index d20c92bd..00000000 --- a/include/spdlog/details/registry_shared.h +++ /dev/null @@ -1,66 +0,0 @@ -/*************************************************************************/ -/* spdlog - an extremely fast and easy to use c++11 logging library. */ -/* Copyright (c) 2014 Gabi Melman. */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#pragma once - -#include "registry.h" - -namespace spdlog -{ -namespace details -{ -template class registry_shared_t : public registry_t -{ -public: - static std::shared_ptr > instance() - { - if(!_instance) - _instance = std::make_shared >(); - return _instance; - } - - static void set_registry(std::shared_ptr > registry) - { - _instance = registry; - } - - registry_shared_t() : registry_t() {} - -private: - static std::shared_ptr > _instance; -}; - -#ifdef SPDLOG_USE_SHARED_REGISTRY -# ifdef SPDLOG_NO_REGISTRY_MUTEX - typedef registry_shared_t registry; -# else - typedef registry_shared_t registry; -# endif //SPDLOG_NO_REGISTRY_MUTEX -#endif //SPDLOG_USE_SHARED_REGISTRY - -template -std::shared_ptr > registry_shared_t::_instance = nullptr; - -} -} diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 49b3650f..cdbe1a94 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -28,7 +28,6 @@ // Global registry functions // #include "registry.h" -#include "registry_shared.h" #include "../sinks/file_sinks.h" #include "../sinks/stdout_sinks.h" #include "../sinks/syslog_sink.h" diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index c66e1e27..b651658b 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -72,9 +72,3 @@ // Note that upon creating a logger the registry is modified by spdlog.. // #define SPDLOG_NO_REGISTRY_MUTEX /////////////////////////////////////////////////////////////////////////////// - - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to enable use of a shared registry -// #define SPDLOG_USE_SHARED_REGISTRY -/////////////////////////////////////////////////////////////////////////////// From 29f0b2d0d39c66e7ff09e1996b17c338ffe3c055 Mon Sep 17 00:00:00 2001 From: Alexandre Petitjean Date: Tue, 26 May 2015 14:31:17 +0200 Subject: [PATCH 3/4] Fix template issue --- include/spdlog/details/registry.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 45dbd94f..0b744795 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -144,14 +144,14 @@ public: _async_mode = false; } - static std::shared_ptr > instance() + static std::shared_ptr> instance() { if(!_instance) _instance = std::make_shared>(); return _instance; } - static void set_registry(std::shared_ptr > registry) + static void set_registry(std::shared_ptr> registry) { _instance = registry; } @@ -159,7 +159,7 @@ public: registry_t() {} private: - static std::shared_ptr > _instance; + static std::shared_ptr> _instance; void register_logger_impl(std::shared_ptr logger) { @@ -180,13 +180,14 @@ private: std::function _worker_warmup_cb = nullptr; std::chrono::milliseconds _flush_interval_ms; }; + +template +std::shared_ptr> registry_t::_instance = nullptr; + #ifdef SPDLOG_NO_REGISTRY_MUTEX typedef registry_t registry; # else typedef registry_t registry; #endif //SPDLOG_NO_REGISTRY_MUTEX - -std::shared_ptr registry::_instance = nullptr; - } } From f8160adfdb2f964085a4e17eb8a303d52d842b71 Mon Sep 17 00:00:00 2001 From: Alexandre Petitjean Date: Tue, 26 May 2015 15:29:05 +0200 Subject: [PATCH 4/4] clearer check on registry instance --- include/spdlog/details/registry.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 0b744795..3fd64930 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -146,7 +146,7 @@ public: static std::shared_ptr> instance() { - if(!_instance) + if(_instance == nullptr) _instance = std::make_shared>(); return _instance; }