mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
atyle
This commit is contained in:
parent
efc8de6d64
commit
4efbd950d6
@ -78,11 +78,14 @@ int main(int, char*[])
|
||||
|
||||
// Change default log error handler
|
||||
err_handler_example();
|
||||
|
||||
// Apply a function on all registered loggers
|
||||
spd::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->info("End of example."); });
|
||||
|
||||
// Release and close all loggers
|
||||
// Apply a function on all registered loggers
|
||||
spd::apply_all([&](std::shared_ptr<spdlog::logger> l)
|
||||
{
|
||||
l->info("End of example.");
|
||||
});
|
||||
|
||||
// Release and close all loggers
|
||||
spdlog::drop_all();
|
||||
}
|
||||
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
|
||||
|
@ -71,12 +71,12 @@ public:
|
||||
return new_logger;
|
||||
}
|
||||
|
||||
void apply_all(std::function<void(std::shared_ptr<logger>)> fun)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(_mutex);
|
||||
for (auto &l : _loggers)
|
||||
fun(l.second);
|
||||
}
|
||||
void apply_all(std::function<void(std::shared_ptr<logger>)> fun)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(_mutex);
|
||||
for (auto &l : _loggers)
|
||||
fun(l.second);
|
||||
}
|
||||
|
||||
void drop(const std::string& logger_name)
|
||||
{
|
||||
|
@ -165,7 +165,7 @@ inline void spdlog::set_sync_mode()
|
||||
|
||||
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()
|
||||
|
@ -113,7 +113,7 @@ std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_b
|
||||
|
||||
|
||||
// Create and register a logger with templated sink type
|
||||
// Example:
|
||||
// Example:
|
||||
// spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt");
|
||||
template <typename Sink, typename... Args>
|
||||
std::shared_ptr<spdlog::logger> create(const std::string& logger_name, Args...);
|
||||
@ -123,7 +123,7 @@ std::shared_ptr<spdlog::logger> create(const std::string& logger_name, Args...);
|
||||
void register_logger(std::shared_ptr<logger> logger);
|
||||
|
||||
// Apply a user defined function on all registered loggers
|
||||
// Example:
|
||||
// Example:
|
||||
// spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
|
||||
void apply_all(std::function<void(std::shared_ptr<logger>)> fun);
|
||||
|
||||
|
@ -25,24 +25,28 @@ TEST_CASE("explicit register" "[registry]")
|
||||
|
||||
TEST_CASE("apply_all" "[registry]")
|
||||
{
|
||||
spdlog::drop_all();
|
||||
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
spdlog::register_logger(logger);
|
||||
auto logger2 = std::make_shared<spdlog::logger>(tested_logger_name2, std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
spdlog::register_logger(logger2);
|
||||
spdlog::drop_all();
|
||||
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
spdlog::register_logger(logger);
|
||||
auto logger2 = std::make_shared<spdlog::logger>(tested_logger_name2, std::make_shared<spdlog::sinks::null_sink_st>());
|
||||
spdlog::register_logger(logger2);
|
||||
|
||||
int counter = 0;
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l){counter++;});
|
||||
REQUIRE(counter == 2);
|
||||
int counter = 0;
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l)
|
||||
{
|
||||
counter++;
|
||||
});
|
||||
REQUIRE(counter == 2);
|
||||
|
||||
counter = 0;
|
||||
spdlog::drop(tested_logger_name2);
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l)
|
||||
{
|
||||
REQUIRE(l->name() == tested_logger_name);
|
||||
counter++; }
|
||||
);
|
||||
REQUIRE(counter == 1);
|
||||
counter = 0;
|
||||
spdlog::drop(tested_logger_name2);
|
||||
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l)
|
||||
{
|
||||
REQUIRE(l->name() == tested_logger_name);
|
||||
counter++;
|
||||
}
|
||||
);
|
||||
REQUIRE(counter == 1);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user