2023-09-23 22:59:51 +08:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-09-29 05:20:26 +08:00
|
|
|
#include "includes.h"
|
|
|
|
#include "test_sink.h"
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("test_default_level", "[log_level]") {
|
2023-09-23 22:59:51 +08:00
|
|
|
auto test_sink = std::make_shared<spdlog::sinks::test_sink_st>();
|
|
|
|
REQUIRE(test_sink->log_level() == spdlog::level::trace);
|
|
|
|
spdlog::logger logger("test-level", test_sink);
|
2023-09-25 01:43:14 +08:00
|
|
|
REQUIRE(test_sink->log_level() == spdlog::level::trace);
|
2023-09-23 22:59:51 +08:00
|
|
|
REQUIRE(logger.log_level() == spdlog::level::info);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that logger log only messages with level bigger or equal to its level
|
|
|
|
void test_logger_level(spdlog::level level);
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("test_logger_levels", "[log_level]") {
|
|
|
|
for (size_t i = 0; i < spdlog::levels_count; i++) {
|
2023-09-23 22:59:51 +08:00
|
|
|
auto level = static_cast<spdlog::level>(i);
|
|
|
|
test_logger_level(level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test that logger log all messages with level bigger or equal to its level
|
|
|
|
void test_sink_level(spdlog::level level);
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("test_sink_levels", "[log_level]") {
|
|
|
|
for (size_t i = 0; i < spdlog::levels_count; i++) {
|
2023-09-23 22:59:51 +08:00
|
|
|
auto level = static_cast<spdlog::level>(i);
|
|
|
|
test_sink_level(level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2023-09-25 21:05:07 +08:00
|
|
|
// test helpers to check that logger/sink displays only messages with level bigger or equal to its
|
|
|
|
// level
|
2023-09-23 22:59:51 +08:00
|
|
|
//
|
2023-09-25 21:05:07 +08:00
|
|
|
auto get_expected_messages(spdlog::level level) {
|
2023-09-23 22:59:51 +08:00
|
|
|
// expected messages for each level
|
|
|
|
static const std::map<spdlog::level, std::vector<std::string>> messages = {
|
2023-09-25 21:05:07 +08:00
|
|
|
{spdlog::level::trace,
|
|
|
|
{"trace hello", "debug hello", "info hello", "warning hello", "error hello",
|
|
|
|
"critical hello"}},
|
|
|
|
{spdlog::level::debug,
|
|
|
|
{"debug hello", "info hello", "warning hello", "error hello", "critical hello"}},
|
2023-09-25 01:43:14 +08:00
|
|
|
{spdlog::level::info, {"info hello", "warning hello", "error hello", "critical hello"}},
|
2023-09-25 21:05:07 +08:00
|
|
|
{spdlog::level::warn, {"warning hello", "error hello", "critical hello"}},
|
|
|
|
{spdlog::level::err, {"error hello", "critical hello"}},
|
|
|
|
{spdlog::level::critical, {"critical hello"}},
|
|
|
|
{spdlog::level::off, {}},
|
|
|
|
{spdlog::level::n_levels, {}}};
|
2023-09-23 22:59:51 +08:00
|
|
|
return messages.at(level);
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
void test_logger_level(spdlog::level level) {
|
2023-09-23 22:59:51 +08:00
|
|
|
auto test_sink = std::make_shared<spdlog::sinks::test_sink_st>();
|
|
|
|
spdlog::logger logger("test-level", test_sink);
|
|
|
|
logger.set_level(level);
|
|
|
|
logger.set_pattern("%l %v");
|
|
|
|
REQUIRE(logger.log_level() == level);
|
|
|
|
REQUIRE(test_sink->log_level() == spdlog::level::trace);
|
|
|
|
|
|
|
|
logger.trace("hello");
|
|
|
|
logger.debug("hello");
|
|
|
|
logger.info("hello");
|
|
|
|
logger.warn("hello");
|
|
|
|
logger.error("hello");
|
|
|
|
logger.critical("hello");
|
|
|
|
|
|
|
|
auto lines = test_sink->lines();
|
2023-09-25 01:43:14 +08:00
|
|
|
REQUIRE(lines == get_expected_messages(level));
|
2023-09-23 22:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// test that sink displays all messages with level bigger or equal to its level
|
2023-09-25 21:05:07 +08:00
|
|
|
void test_sink_level(spdlog::level level) {
|
2023-09-23 22:59:51 +08:00
|
|
|
auto test_sink = std::make_shared<spdlog::sinks::test_sink_st>();
|
|
|
|
spdlog::logger logger("test-level", test_sink);
|
|
|
|
logger.set_level(spdlog::level::trace);
|
|
|
|
test_sink->set_level(level);
|
|
|
|
|
|
|
|
REQUIRE(logger.log_level() == spdlog::level::trace);
|
|
|
|
REQUIRE(test_sink->log_level() == level);
|
|
|
|
|
|
|
|
logger.set_pattern("%l %v");
|
|
|
|
|
|
|
|
logger.trace("hello");
|
|
|
|
logger.debug("hello");
|
|
|
|
logger.info("hello");
|
|
|
|
logger.warn("hello");
|
|
|
|
logger.error("hello");
|
|
|
|
logger.critical("hello");
|
|
|
|
|
|
|
|
auto lines = test_sink->lines();
|
2023-09-25 01:43:14 +08:00
|
|
|
REQUIRE(lines == get_expected_messages(level));
|
2023-09-23 22:59:51 +08:00
|
|
|
}
|