diff --git a/include/spdlog/async.h b/include/spdlog/async.h index 9264a4e3..bf7dee46 100644 --- a/include/spdlog/async.h +++ b/include/spdlog/async.h @@ -52,7 +52,10 @@ struct async_factory_impl 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.register_and_init(new_logger); + registry_inst.init_with_global_defaults(new_logger); +#ifndef SPDLOG_DISABLE_GLOBAL_REGISTRATION + registry_inst.register_logger(new_logger); +#endif return new_logger; } }; diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 439395f7..8f7e7f01 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -47,13 +47,8 @@ public: loggers_[logger_name] = std::move(new_logger); } - void register_and_init(std::shared_ptr new_logger) + void init_with_global_defaults(std::shared_ptr new_logger) { - std::lock_guard lock(logger_map_mutex_); - auto logger_name = new_logger->name(); - throw_if_exists_(logger_name); - - // set the global formatter pattern new_logger->set_formatter(formatter_->clone()); if (err_handler_) @@ -63,9 +58,6 @@ public: new_logger->set_level(level_); new_logger->flush_on(flush_level_); - - // add to registry - loggers_[logger_name] = std::move(new_logger); } std::shared_ptr get(const std::string &logger_name) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index bcca2e84..d2d8a194 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -29,7 +29,10 @@ struct synchronous_factory { auto sink = std::make_shared(std::forward(args)...); auto new_logger = std::make_shared(std::move(logger_name), std::move(sink)); - details::registry::instance().register_and_init(new_logger); + details::registry::instance().init_with_global_defaults(new_logger); +#ifndef SPDLOG_DISABLE_GLOBAL_REGISTRATION + details::registry::instance().register_logger(new_logger); +#endif return new_logger; } }; diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index d91159f7..a18593c4 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -128,3 +128,11 @@ // // #define SPDLOG_DISABLE_DEFAULT_LOGGER /////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to disable global logger registration +// This is useful if you don't need loggers to be globally accessible and part of the global registry. +// This option will allow you to use the same name for different loggers. +// +// #define SPDLOG_DISABLE_GLOBAL_REGISTRATION +///////////////////////////////////////////////////////////////////////////////