This commit is contained in:
gabime 2016-08-20 13:55:50 +03:00
parent efc8de6d64
commit 4efbd950d6
5 changed files with 36 additions and 29 deletions

View File

@ -79,8 +79,11 @@ int main(int, char*[])
// Change default log error handler // Change default log error handler
err_handler_example(); err_handler_example();
// Apply a function on all registered loggers // Apply a function on all registered loggers
spd::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->info("End of example."); }); spd::apply_all([&](std::shared_ptr<spdlog::logger> l)
{
l->info("End of example.");
});
// Release and close all loggers // Release and close all loggers
spdlog::drop_all(); spdlog::drop_all();

View File

@ -71,12 +71,12 @@ public:
return new_logger; return new_logger;
} }
void apply_all(std::function<void(std::shared_ptr<logger>)> fun) void apply_all(std::function<void(std::shared_ptr<logger>)> fun)
{ {
std::lock_guard<Mutex> lock(_mutex); std::lock_guard<Mutex> lock(_mutex);
for (auto &l : _loggers) for (auto &l : _loggers)
fun(l.second); fun(l.second);
} }
void drop(const std::string& logger_name) void drop(const std::string& logger_name)
{ {

View File

@ -165,7 +165,7 @@ inline void spdlog::set_sync_mode()
inline void spdlog::apply_all(std::function<void(std::shared_ptr<logger>)> fun) inline void spdlog::apply_all(std::function<void(std::shared_ptr<logger>)> fun)
{ {
details::registry::instance().apply_all(fun); details::registry::instance().apply_all(fun);
} }
inline void spdlog::drop_all() inline void spdlog::drop_all()

View File

@ -25,24 +25,28 @@ TEST_CASE("explicit register" "[registry]")
TEST_CASE("apply_all" "[registry]") TEST_CASE("apply_all" "[registry]")
{ {
spdlog::drop_all(); spdlog::drop_all();
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>()); auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
spdlog::register_logger(logger); spdlog::register_logger(logger);
auto logger2 = std::make_shared<spdlog::logger>(tested_logger_name2, std::make_shared<spdlog::sinks::null_sink_st>()); auto logger2 = std::make_shared<spdlog::logger>(tested_logger_name2, std::make_shared<spdlog::sinks::null_sink_st>());
spdlog::register_logger(logger2); spdlog::register_logger(logger2);
int counter = 0; int counter = 0;
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l){counter++;}); spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l)
REQUIRE(counter == 2); {
counter++;
});
REQUIRE(counter == 2);
counter = 0; counter = 0;
spdlog::drop(tested_logger_name2); spdlog::drop(tested_logger_name2);
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l) spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l)
{ {
REQUIRE(l->name() == tested_logger_name); REQUIRE(l->name() == tested_logger_name);
counter++; } counter++;
); }
REQUIRE(counter == 1); );
REQUIRE(counter == 1);
} }