spdlog/tests/test_macros.cpp

55 lines
1.8 KiB
C++
Raw Normal View History

/*
2023-09-25 21:08:29 +08:00
* This content is released under the MIT License as specified in
* https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
2018-03-09 21:26:33 +08:00
*/
#include "includes.h"
#if SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_DEBUG
2023-09-25 21:08:29 +08:00
#error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG"
#endif
2021-01-07 07:55:57 +08:00
#define TEST_FILENAME "test_logs/simple_log"
2023-09-25 21:08:29 +08:00
TEST_CASE("debug and trace w/o format string", "[macros]") {
2017-11-06 18:39:04 +08:00
prepare_logdir();
2021-01-07 07:55:57 +08:00
spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME);
2017-11-06 18:39:04 +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);
SPDLOG_LOGGER_TRACE(logger, "Test message 1");
SPDLOG_LOGGER_DEBUG(logger, "Test message 2");
2017-11-06 18:39:04 +08:00
logger->flush();
using spdlog::details::os::default_eol;
2023-09-25 21:08:29 +08:00
REQUIRE(ends_with(file_contents(TEST_FILENAME),
spdlog::fmt_lib::format("Test message 2{}", default_eol)));
2021-01-07 07:55:57 +08:00
REQUIRE(count_lines(TEST_FILENAME) == 1);
2021-12-06 19:37:32 +08:00
auto orig_default_logger = spdlog::default_logger();
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();
2021-01-07 07:55:57 +08:00
require_message_count(TEST_FILENAME, 2);
2023-09-25 21:08:29 +08:00
REQUIRE(ends_with(file_contents(TEST_FILENAME),
spdlog::fmt_lib::format("Test message 4{}", default_eol)));
2021-12-06 19:37:32 +08:00
spdlog::set_default_logger(std::move(orig_default_logger));
}
2023-09-25 21:08:29 +08:00
TEST_CASE("disable param evaluation", "[macros]") {
SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated"));
}
2019-10-10 02:41:02 +08:00
2023-09-25 21:08:29 +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");
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");
}