2017-10-13 07:04:31 +08:00
|
|
|
/*
|
2018-03-09 21:26:33 +08:00
|
|
|
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
|
|
|
*/
|
2017-10-13 07:04:31 +08:00
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2018-11-12 00:15:24 +08:00
|
|
|
#if SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_DEBUG
|
|
|
|
#error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG"
|
|
|
|
#endif
|
|
|
|
|
2017-10-13 07:04:31 +08:00
|
|
|
TEST_CASE("debug and trace w/o format string", "[macros]]")
|
|
|
|
{
|
2018-11-12 00:15:24 +08:00
|
|
|
|
2017-11-06 18:39:04 +08:00
|
|
|
prepare_logdir();
|
2019-10-20 22:40:56 +08:00
|
|
|
std::string filename = "test_logs/simple_log";
|
2017-11-06 18:39:04 +08:00
|
|
|
|
2018-07-07 17:12:45 +08:00
|
|
|
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
|
2017-11-06 18:39:04 +08:00
|
|
|
logger->set_pattern("%v");
|
|
|
|
logger->set_level(spdlog::level::trace);
|
|
|
|
|
2018-11-12 00:15:24 +08:00
|
|
|
SPDLOG_LOGGER_TRACE(logger, "Test message 1");
|
|
|
|
SPDLOG_LOGGER_DEBUG(logger, "Test message 2");
|
2017-11-06 18:39:04 +08:00
|
|
|
logger->flush();
|
|
|
|
|
2020-02-05 05:23:36 +08:00
|
|
|
using spdlog::details::os::default_eol;
|
|
|
|
REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 2{}", default_eol)));
|
2018-11-12 00:15:24 +08:00
|
|
|
REQUIRE(count_lines(filename) == 1);
|
2018-11-23 00:48:32 +08:00
|
|
|
|
|
|
|
spdlog::set_default_logger(logger);
|
|
|
|
|
|
|
|
SPDLOG_TRACE("Test message 3");
|
|
|
|
SPDLOG_DEBUG("Test message {}", 4);
|
|
|
|
logger->flush();
|
|
|
|
|
2020-02-05 05:23:36 +08:00
|
|
|
require_message_count(filename, 2);
|
|
|
|
REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 4{}", default_eol)));
|
2017-10-13 07:04:31 +08:00
|
|
|
}
|
|
|
|
|
2018-11-12 00:15:24 +08:00
|
|
|
TEST_CASE("disable param evaluation", "[macros]")
|
2017-10-13 07:04:31 +08:00
|
|
|
{
|
2018-11-12 00:15:24 +08:00
|
|
|
SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated"));
|
2017-10-13 07:04:31 +08:00
|
|
|
}
|
2019-10-10 02:41:02 +08:00
|
|
|
|
2019-11-03 21:19:59 +08:00
|
|
|
TEST_CASE("pass logger pointer", "[macros]")
|
2019-11-02 16:40:37 +08:00
|
|
|
{
|
|
|
|
auto logger = spdlog::create<spdlog::sinks::null_sink_mt>("refmacro");
|
2019-11-03 21:19:59 +08:00
|
|
|
auto &ref = *logger;
|
2019-11-02 16:40:37 +08:00
|
|
|
SPDLOG_LOGGER_TRACE(&ref, "Test message 1");
|
|
|
|
SPDLOG_LOGGER_DEBUG(&ref, "Test message 2");
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:42:28 +08:00
|
|
|
// ensure that even if right macro level is on- don't evaluate if the logger's level is not high enough
|
2019-11-03 23:07:37 +08:00
|
|
|
// TEST_CASE("disable param evaluation2", "[macros]")
|
2019-11-03 21:19:59 +08:00
|
|
|
//{
|
|
|
|
// auto logger = std::make_shared<spdlog::logger>("test-macro");
|
|
|
|
// logger->set_level(spdlog::level::off);
|
|
|
|
// int x = 0;
|
|
|
|
// SPDLOG_LOGGER_DEBUG(logger, "Test message {}", ++x);
|
|
|
|
// REQUIRE(x == 0);
|
|
|
|
//}
|