spdlog/tests/registry.cpp

81 lines
2.7 KiB
C++
Raw Normal View History

2015-05-16 01:30:37 +08:00
#include "includes.h"
2016-07-15 23:41:59 +08:00
static const char *tested_logger_name = "null_logger";
2016-08-20 18:51:07 +08:00
static const char *tested_logger_name2 = "null_logger2";
2015-05-16 01:30:37 +08:00
TEST_CASE("register_drop", "[registry]")
{
spdlog::drop_all();
2016-07-15 23:41:59 +08:00
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
2018-03-09 21:26:33 +08:00
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
// Throw if registring existing name
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), const spdlog::spdlog_ex &);
2015-05-16 01:30:37 +08:00
}
2018-10-12 08:04:55 +08:00
TEST_CASE("explicit register", "[registry]")
2015-05-16 01:30:37 +08:00
{
spdlog::drop_all();
2016-07-15 23:41:59 +08:00
auto logger = std::make_shared<spdlog::logger>(tested_logger_name, std::make_shared<spdlog::sinks::null_sink_st>());
2015-05-16 01:30:37 +08:00
spdlog::register_logger(logger);
2016-07-15 23:41:59 +08:00
REQUIRE(spdlog::get(tested_logger_name) != nullptr);
2018-03-09 21:26:33 +08:00
// Throw if registring existing name
REQUIRE_THROWS_AS(spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name), const spdlog::spdlog_ex &);
2015-05-16 01:30:37 +08:00
}
2018-10-12 08:04:55 +08:00
TEST_CASE("apply_all", "[registry]")
2016-08-20 18:51:07 +08:00
{
2016-08-20 18:55:50 +08:00
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;
2018-08-13 17:27:25 +08:00
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger>) { counter++; });
2016-08-20 18:55:50 +08:00
REQUIRE(counter == 2);
counter = 0;
spdlog::drop(tested_logger_name2);
2018-03-09 21:26:33 +08:00
spdlog::apply_all([&counter](std::shared_ptr<spdlog::logger> l) {
2016-08-20 18:55:50 +08:00
REQUIRE(l->name() == tested_logger_name);
counter++;
2018-03-09 21:26:33 +08:00
});
2016-08-20 18:55:50 +08:00
REQUIRE(counter == 1);
2016-08-20 18:51:07 +08:00
}
2018-10-12 08:04:55 +08:00
TEST_CASE("drop", "[registry]")
2015-05-16 01:30:37 +08:00
{
spdlog::drop_all();
2016-07-15 23:41:59 +08:00
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
spdlog::drop(tested_logger_name);
REQUIRE_FALSE(spdlog::get(tested_logger_name));
2015-05-16 01:30:37 +08:00
}
2018-10-12 08:04:55 +08:00
TEST_CASE("drop_all", "[registry]")
2015-05-16 01:30:37 +08:00
{
spdlog::drop_all();
2016-07-15 23:41:59 +08:00
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
2016-08-20 18:51:07 +08:00
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name2);
2015-05-16 01:30:37 +08:00
spdlog::drop_all();
2016-07-15 23:41:59 +08:00
REQUIRE_FALSE(spdlog::get(tested_logger_name));
2018-08-16 23:53:55 +08:00
REQUIRE_FALSE(spdlog::get(tested_logger_name2));
2015-05-16 01:30:37 +08:00
}
2018-10-12 08:04:55 +08:00
TEST_CASE("drop non existing", "[registry]")
2015-05-16 01:30:37 +08:00
{
spdlog::drop_all();
2016-07-15 23:41:59 +08:00
spdlog::create<spdlog::sinks::null_sink_mt>(tested_logger_name);
2015-05-16 01:30:37 +08:00
spdlog::drop("some_name");
REQUIRE_FALSE(spdlog::get("some_name"));
2016-07-15 23:41:59 +08:00
REQUIRE(spdlog::get(tested_logger_name));
2015-05-16 01:30:37 +08:00
spdlog::drop_all();
}
2018-10-12 08:04:55 +08:00
TEST_CASE("default logger", "[registry]")
{
spdlog::drop_all();
spdlog::set_default_logger(std::move(spdlog::null_logger_st(tested_logger_name)));
REQUIRE(spdlog::get(tested_logger_name) == spdlog::get());
spdlog::drop_all();
}