spdlog/tests/test_cfg.cpp

184 lines
5.6 KiB
C++
Raw Normal View History

2020-09-26 19:41:33 +08:00
2019-12-22 00:52:35 +08:00
#include "includes.h"
#include "test_sink.h"
2019-12-23 02:40:19 +08:00
#include <spdlog/cfg/env.h>
#include <spdlog/cfg/argv.h>
2019-12-22 01:45:14 +08:00
2020-03-06 21:09:46 +08:00
using spdlog::cfg::load_argv_levels;
using spdlog::cfg::load_env_levels;
2019-12-23 04:33:19 +08:00
using spdlog::sinks::test_sink_st;
2019-12-22 00:52:35 +08:00
2019-12-23 02:40:19 +08:00
TEST_CASE("env", "[cfg]")
2019-12-22 00:52:35 +08:00
{
2019-12-22 02:15:18 +08:00
spdlog::drop("l1");
2019-12-23 04:33:19 +08:00
auto l1 = spdlog::create<test_sink_st>("l1");
2020-03-18 17:46:32 +08:00
#ifdef CATCH_PLATFORM_WINDOWS
2019-12-22 06:17:32 +08:00
_putenv_s("SPDLOG_LEVEL", "l1=warn");
#else
2019-12-22 01:25:54 +08:00
setenv("SPDLOG_LEVEL", "l1=warn", 1);
2019-12-22 06:17:32 +08:00
#endif
2020-03-06 21:09:46 +08:00
load_env_levels();
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::warn);
2019-12-23 04:33:19 +08:00
spdlog::set_default_logger(spdlog::create<test_sink_st>("cfg-default"));
2023-09-23 05:27:32 +08:00
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::info);
2019-12-22 00:52:35 +08:00
}
2019-12-23 02:40:19 +08:00
TEST_CASE("argv1", "[cfg]")
2019-12-22 01:25:54 +08:00
{
2019-12-22 02:15:18 +08:00
spdlog::drop("l1");
2019-12-22 01:52:37 +08:00
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn"};
2020-04-08 22:04:10 +08:00
load_argv_levels(2, argv);
2019-12-22 01:25:54 +08:00
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::info);
2019-12-22 01:25:54 +08:00
}
2019-12-23 02:40:19 +08:00
TEST_CASE("argv2", "[cfg]")
2019-12-22 01:25:54 +08:00
{
2019-12-22 02:15:18 +08:00
spdlog::drop("l1");
2019-12-22 01:52:37 +08:00
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=warn,trace"};
2020-04-08 22:04:10 +08:00
load_argv_levels(2, argv);
2019-12-23 04:33:19 +08:00
auto l1 = spdlog::create<test_sink_st>("l1");
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::trace);
2019-12-22 01:25:54 +08:00
}
2019-12-23 02:40:19 +08:00
TEST_CASE("argv3", "[cfg]")
2019-12-22 01:25:54 +08:00
{
2023-09-23 05:27:32 +08:00
spdlog::set_level(spdlog::log_level::trace);
2020-09-27 07:08:24 +08:00
2019-12-22 02:15:18 +08:00
spdlog::drop("l1");
2020-09-27 07:08:24 +08:00
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk_name=warn"};
2020-04-08 22:04:10 +08:00
load_argv_levels(2, argv);
2019-12-23 04:33:19 +08:00
auto l1 = spdlog::create<test_sink_st>("l1");
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::trace);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::trace);
2019-12-22 01:25:54 +08:00
}
2019-12-23 02:40:19 +08:00
TEST_CASE("argv4", "[cfg]")
2019-12-22 01:25:54 +08:00
{
2023-09-23 05:27:32 +08:00
spdlog::set_level(spdlog::log_level::info);
2019-12-22 02:15:18 +08:00
spdlog::drop("l1");
2019-12-22 01:52:37 +08:00
const char *argv[] = {"ignore", "SPDLOG_LEVEL=junk"};
2020-04-08 22:04:10 +08:00
load_argv_levels(2, argv);
2019-12-23 04:33:19 +08:00
auto l1 = spdlog::create<test_sink_st>("l1");
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::info);
2019-12-22 01:25:54 +08:00
}
2019-12-22 02:01:25 +08:00
2019-12-23 02:40:19 +08:00
TEST_CASE("argv5", "[cfg]")
2019-12-22 02:01:25 +08:00
{
2023-09-23 05:27:32 +08:00
spdlog::set_level(spdlog::log_level::info);
2019-12-22 02:15:18 +08:00
spdlog::drop("l1");
2019-12-22 02:01:25 +08:00
const char *argv[] = {"ignore", "ignore", "SPDLOG_LEVEL=l1=warn,trace"};
2020-04-08 22:04:10 +08:00
load_argv_levels(3, argv);
2019-12-23 04:33:19 +08:00
auto l1 = spdlog::create<test_sink_st>("l1");
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::trace);
spdlog::set_level(spdlog::log_level::info);
2019-12-22 02:15:18 +08:00
}
2019-12-23 02:40:19 +08:00
TEST_CASE("argv6", "[cfg]")
2019-12-22 02:15:18 +08:00
{
2023-09-23 05:27:32 +08:00
spdlog::set_level(spdlog::log_level::err);
2019-12-22 02:15:18 +08:00
const char *argv[] = {""};
2020-04-08 22:04:10 +08:00
load_argv_levels(1, argv);
2023-09-23 05:27:32 +08:00
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::err);
spdlog::set_level(spdlog::log_level::info);
2019-12-22 02:15:18 +08:00
}
2019-12-23 02:40:19 +08:00
TEST_CASE("argv7", "[cfg]")
2019-12-22 02:15:18 +08:00
{
2023-09-23 05:27:32 +08:00
spdlog::set_level(spdlog::log_level::err);
2019-12-22 02:15:18 +08:00
const char *argv[] = {""};
2020-04-08 22:04:10 +08:00
load_argv_levels(0, argv);
2023-09-23 05:27:32 +08:00
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::err);
spdlog::set_level(spdlog::log_level::info);
2019-12-22 02:01:25 +08:00
}
2020-09-25 19:44:01 +08:00
2020-09-25 23:19:50 +08:00
TEST_CASE("level-not-set-test1", "[cfg]")
2020-09-25 19:44:01 +08:00
{
spdlog::drop("l1");
const char *argv[] = {"ignore", ""};
load_argv_levels(2, argv);
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
2023-09-23 05:27:32 +08:00
l1->set_level(spdlog::log_level::trace);
REQUIRE(l1->level() == spdlog::log_level::trace);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::info);
2020-09-25 19:44:01 +08:00
}
2020-09-25 23:19:50 +08:00
TEST_CASE("level-not-set-test2", "[cfg]")
2020-09-25 19:44:01 +08:00
{
spdlog::drop("l1");
2020-09-25 23:19:50 +08:00
spdlog::drop("l2");
2020-09-25 19:44:01 +08:00
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
2020-09-25 23:19:50 +08:00
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
2023-09-23 05:27:32 +08:00
l1->set_level(spdlog::log_level::warn);
2020-09-25 23:19:50 +08:00
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
2023-09-23 05:27:32 +08:00
l2->set_level(spdlog::log_level::warn);
2020-09-25 23:19:50 +08:00
load_argv_levels(2, argv);
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::trace);
REQUIRE(l2->level() == spdlog::log_level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::info);
2020-09-25 23:19:50 +08:00
}
TEST_CASE("level-not-set-test3", "[cfg]")
{
spdlog::drop("l1");
spdlog::drop("l2");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace"};
2020-09-25 19:44:01 +08:00
load_argv_levels(2, argv);
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::trace);
REQUIRE(l2->level() == spdlog::log_level::info);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::info);
2020-09-25 19:44:01 +08:00
}
2020-09-25 23:19:50 +08:00
TEST_CASE("level-not-set-test4", "[cfg]")
{
spdlog::drop("l1");
spdlog::drop("l2");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=trace,warn"};
load_argv_levels(2, argv);
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::trace);
REQUIRE(l2->level() == spdlog::log_level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::warn);
2020-09-26 19:41:33 +08:00
}
TEST_CASE("level-not-set-test5", "[cfg]")
{
spdlog::drop("l1");
spdlog::drop("l2");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=l1=junk,warn"};
load_argv_levels(2, argv);
auto l1 = spdlog::create<spdlog::sinks::test_sink_st>("l1");
auto l2 = spdlog::create<spdlog::sinks::test_sink_st>("l2");
2023-09-23 05:27:32 +08:00
REQUIRE(l1->level() == spdlog::log_level::warn);
REQUIRE(l2->level() == spdlog::log_level::warn);
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::warn);
2020-09-25 23:19:50 +08:00
}
2020-09-26 19:41:33 +08:00
TEST_CASE("restore-to-default", "[cfg]")
{
spdlog::drop("l1");
spdlog::drop("l2");
const char *argv[] = {"ignore", "SPDLOG_LEVEL=info"};
load_argv_levels(2, argv);
2023-09-23 05:27:32 +08:00
REQUIRE(spdlog::default_logger()->level() == spdlog::log_level::info);
2020-09-26 19:41:33 +08:00
}