* using API call instead of macro for toggling automatic registration

* added unit test for disabling automatic registration
This commit is contained in:
Pablo Arias 2018-11-10 14:34:04 +01:00
parent f95b189fe3
commit fbc58ebef8
5 changed files with 37 additions and 17 deletions

View File

@ -52,10 +52,7 @@ struct async_factory_impl
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
registry_inst.init_with_global_defaults(new_logger);
#ifndef SPDLOG_DISABLE_GLOBAL_REGISTRATION
registry_inst.register_logger(new_logger);
#endif
registry_inst.initialize_logger(new_logger);
return new_logger;
}
};

View File

@ -47,7 +47,7 @@ public:
loggers_[logger_name] = std::move(new_logger);
}
void init_with_global_defaults(std::shared_ptr<logger> new_logger)
void initialize_logger(std::shared_ptr<logger> new_logger)
{
new_logger->set_formatter(formatter_->clone());
@ -58,6 +58,11 @@ public:
new_logger->set_level(level_);
new_logger->flush_on(flush_level_);
if (automatic_registration_)
{
register_logger(new_logger);
}
}
std::shared_ptr<logger> get(const std::string &logger_name)
@ -215,6 +220,11 @@ public:
return tp_mutex_;
}
void set_automatic_registration(bool automatic_regsistration)
{
automatic_registration_ = automatic_regsistration;
}
static registry &instance()
{
static registry s_instance;
@ -261,6 +271,7 @@ private:
std::shared_ptr<thread_pool> tp_;
std::unique_ptr<periodic_worker> periodic_flusher_;
std::shared_ptr<logger> default_logger_;
bool automatic_registration_ = true;
};
} // namespace details

View File

@ -29,10 +29,7 @@ struct synchronous_factory
{
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
auto new_logger = std::make_shared<logger>(std::move(logger_name), std::move(sink));
details::registry::instance().init_with_global_defaults(new_logger);
#ifndef SPDLOG_DISABLE_GLOBAL_REGISTRATION
details::registry::instance().register_logger(new_logger);
#endif
details::registry::instance().initialize_logger(new_logger);
return new_logger;
}
};
@ -128,6 +125,12 @@ inline void shutdown()
details::registry::instance().shutdown();
}
// Automatic registration of loggers when using spdlog::create() or spdlog::create_async
inline void set_automatic_registration(bool automatic_registation)
{
details::registry::instance().set_automatic_registration(automatic_registation);
}
// API for using default logger (stdout_color_mt),
// e.g: spdlog::info("Message {}", 1);
//

View File

@ -128,11 +128,3 @@
//
// #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
///////////////////////////////////////////////////////////////////////////////

View File

@ -93,3 +93,20 @@ TEST_CASE("set_default_logger(nullptr)", "[registry]")
spdlog::set_default_logger(nullptr);
REQUIRE_FALSE(spdlog::default_logger());
}
TEST_CASE("disable automatic registration", "[registry]")
{
// set some global parameters
spdlog::level::level_enum log_level = spdlog::level::level_enum::warn;
spdlog::set_level(log_level);
// but disable automatic registration
spdlog::set_automatic_registration(false);
auto logger1 = spdlog::create<spdlog::sinks::daily_file_sink_st>(tested_logger_name, "filename", 11, 59);
auto logger2 = spdlog::create_async<spdlog::sinks::stdout_color_sink_mt>(tested_logger_name2);
// loggers should not be part of the registry
REQUIRE_FALSE(spdlog::get(tested_logger_name));
REQUIRE_FALSE(spdlog::get(tested_logger_name2));
// but make sure they are still initialized according to global defaults
REQUIRE(logger1->level() == log_level);
REQUIRE(logger2->level() == log_level);
}