From eea9d6136f78ed69139a68439983cd00d16b58dd Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 3 Jun 2019 23:56:18 +0300 Subject: [PATCH] Moved default sync factory to seperate file to avoid cyclic includes --- include/spdlog/common.h | 2 ++ include/spdlog/details/registry.h | 4 ++-- include/spdlog/details/synchronous_factory.h | 22 ++++++++++++++++++++ include/spdlog/sinks/android_sink.h | 5 +++-- include/spdlog/sinks/ansicolor_sink.h | 1 + include/spdlog/sinks/basic_file_sink.h | 5 +++-- include/spdlog/sinks/daily_file_sink.h | 5 +++-- include/spdlog/sinks/null_sink.h | 5 +++-- include/spdlog/sinks/rotating_file_sink.h | 5 +++-- include/spdlog/sinks/stdout_color_sinks.h | 11 ++++++---- include/spdlog/sinks/stdout_sinks.h | 9 ++++---- include/spdlog/sinks/systemd_sink.h | 5 +++-- include/spdlog/spdlog.h | 14 +------------ 13 files changed, 58 insertions(+), 35 deletions(-) create mode 100644 include/spdlog/details/synchronous_factory.h diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 905f469b..47f79bdc 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -222,6 +222,8 @@ std::unique_ptr make_unique(Args &&... args) } #endif } // namespace details + + } // namespace spdlog #ifdef SPDLOG_HEADER_ONLY diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index bc6b2cc2..4070fa82 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -3,8 +3,8 @@ #pragma once -// Loggers registy of unique name->logger pointer -// An attempt to create a logger with an already existing name will be ignored +// Loggers registry of unique name->logger pointer +// An attempt to create a logger with an already existing name will result with spdlog_ex exception. // If user requests a non existing logger, nullptr will be returned // This class is thread safe diff --git a/include/spdlog/details/synchronous_factory.h b/include/spdlog/details/synchronous_factory.h new file mode 100644 index 00000000..2e9c1e41 --- /dev/null +++ b/include/spdlog/details/synchronous_factory.h @@ -0,0 +1,22 @@ +// Copyright(c) 2015-present Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "registry.h" + +namespace spdlog { + +// Default logger factory- creates synchronous loggers + class logger; + + 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; + } + }; +} \ No newline at end of file diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index 36d78bf6..f2b7a683 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -7,6 +7,7 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/details/os.h" #include "spdlog/sinks/base_sink.h" +#include "spdlog/details/synchronous_factory.h" #include #include @@ -99,13 +100,13 @@ using android_sink_st = android_sink; // Create and register android syslog logger -template +template inline std::shared_ptr android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog") { return Factory::template create(logger_name, tag); } -template +template inline std::shared_ptr android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog") { return Factory::template create(logger_name, tag); diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 8d9f7cd2..11ffd7fa 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -77,6 +77,7 @@ private: void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); }; + using ansicolor_stdout_sink_mt = ansicolor_sink; using ansicolor_stdout_sink_st = ansicolor_sink; diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h index 5a8e38f6..366dba8e 100644 --- a/include/spdlog/sinks/basic_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -6,6 +6,7 @@ #include "spdlog/details/file_helper.h" #include "spdlog/details/null_mutex.h" #include "spdlog/sinks/base_sink.h" +#include "spdlog/details/synchronous_factory.h" #include #include @@ -38,13 +39,13 @@ using basic_file_sink_st = basic_file_sink; // // factory functions // -template +template inline std::shared_ptr basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false) { return Factory::template create(logger_name, filename, truncate); } -template +template inline std::shared_ptr basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false) { return Factory::template create(logger_name, filename, truncate); diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 7c40f97b..e3f17864 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -8,6 +8,7 @@ #include "spdlog/fmt/fmt.h" #include "spdlog/sinks/base_sink.h" #include "spdlog/details/os.h" +#include "spdlog/details/synchronous_factory.h" #include #include @@ -120,14 +121,14 @@ using daily_file_sink_st = daily_file_sink; // // factory functions // -template +template inline std::shared_ptr daily_logger_mt( const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false) { return Factory::template create(logger_name, filename, hour, minute, truncate); } -template +template inline std::shared_ptr daily_logger_st( const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false) { diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h index 800a992c..fd78a5ec 100644 --- a/include/spdlog/sinks/null_sink.h +++ b/include/spdlog/sinks/null_sink.h @@ -5,6 +5,7 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/sinks/base_sink.h" +#include "spdlog/details/synchronous_factory.h" #include @@ -24,7 +25,7 @@ using null_sink_st = null_sink; } // namespace sinks -template +template inline std::shared_ptr null_logger_mt(const std::string &logger_name) { auto null_logger = Factory::template create(logger_name); @@ -32,7 +33,7 @@ inline std::shared_ptr null_logger_mt(const std::string &logger_name) return null_logger; } -template +template inline std::shared_ptr null_logger_st(const std::string &logger_name) { auto null_logger = Factory::template create(logger_name); diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index a74d7095..6286eb6a 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -5,6 +5,7 @@ #include "spdlog/sinks/base_sink.h" #include "spdlog/details/file_helper.h" +#include "spdlog/details/synchronous_factory.h" #include #include @@ -56,14 +57,14 @@ using rotating_file_sink_st = rotating_file_sink; // factory functions // -template +template inline std::shared_ptr rotating_logger_mt( const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false) { return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open); } -template +template inline std::shared_ptr rotating_logger_st( const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false) { diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h index ecdb42fd..6a9795f7 100644 --- a/include/spdlog/sinks/stdout_color_sinks.h +++ b/include/spdlog/sinks/stdout_color_sinks.h @@ -9,6 +9,8 @@ #include "spdlog/sinks/ansicolor_sink.h" #endif +#include "spdlog/details/synchronous_factory.h" + namespace spdlog { namespace sinks { #ifdef _WIN32 @@ -24,16 +26,17 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st; #endif } // namespace sinks -template + +template std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); -template +template std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); -template +template std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); -template +template std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); } // namespace spdlog diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index 85085130..117142c8 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -6,6 +6,7 @@ #include "spdlog/details/console_globals.h" #include "spdlog/details/null_mutex.h" #include "spdlog/details/pattern_formatter.h" +#include "spdlog/details/synchronous_factory.h" #include #include @@ -70,25 +71,25 @@ using stderr_sink_st = stdout_sink +template inline std::shared_ptr stdout_logger_mt(const std::string &logger_name) { return Factory::template create(logger_name); } -template +template inline std::shared_ptr stdout_logger_st(const std::string &logger_name) { return Factory::template create(logger_name); } -template +template inline std::shared_ptr stderr_logger_mt(const std::string &logger_name) { return Factory::template create(logger_name); } -template +template inline std::shared_ptr stderr_logger_st(const std::string &logger_name) { return Factory::template create(logger_name); diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index 81af39de..da1c3e2c 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -4,6 +4,7 @@ #pragma once #include "spdlog/sinks/base_sink.h" +#include "spdlog/details/synchronous_factory.h" #include #include @@ -65,13 +66,13 @@ using systemd_sink_st = systemd_sink; } // namespace sinks // Create and register a syslog logger -template +template inline std::shared_ptr systemd_logger_mt(const std::string &logger_name) { return Factory::template create(logger_name); } -template +template inline std::shared_ptr systemd_logger_st(const std::string &logger_name) { return Factory::template create(logger_name); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 56a69e4c..ce32bc6e 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -13,6 +13,7 @@ #include "spdlog/details/registry.h" #include "spdlog/logger.h" #include "spdlog/version.h" +#include "spdlog/details/synchronous_factory.h" #include #include @@ -21,19 +22,6 @@ namespace spdlog { -// Default logger factory- creates synchronous loggers -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; - } -}; - using default_factory = synchronous_factory; // Create and register a logger with a templated sink type