1 | /* |
2 | * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE |
3 | */ |
4 | |
5 | #include "includes.h" |
6 | |
7 | #if SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_DEBUG |
8 | # error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG" |
9 | #endif |
10 | |
11 | #define TEST_FILENAME "test_logs/simple_log" |
12 | |
13 | TEST_CASE("debug and trace w/o format string" , "[macros]]" ) |
14 | { |
15 | |
16 | prepare_logdir(); |
17 | spdlog::filename_t filename = SPDLOG_FILENAME_T(TEST_FILENAME); |
18 | |
19 | auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger" , filename); |
20 | logger->set_pattern("%v" ); |
21 | logger->set_level(spdlog::level::trace); |
22 | |
23 | SPDLOG_LOGGER_TRACE(logger, "Test message 1" ); |
24 | SPDLOG_LOGGER_DEBUG(logger, "Test message 2" ); |
25 | logger->flush(); |
26 | |
27 | using spdlog::details::os::default_eol; |
28 | REQUIRE(ends_with(file_contents(TEST_FILENAME), spdlog::fmt_lib::format("Test message 2{}" , default_eol))); |
29 | REQUIRE(count_lines(TEST_FILENAME) == 1); |
30 | |
31 | auto orig_default_logger = spdlog::default_logger(); |
32 | spdlog::set_default_logger(logger); |
33 | |
34 | SPDLOG_TRACE("Test message 3" ); |
35 | SPDLOG_DEBUG("Test message {}" , 4); |
36 | logger->flush(); |
37 | |
38 | require_message_count(TEST_FILENAME, 2); |
39 | REQUIRE(ends_with(file_contents(TEST_FILENAME), spdlog::fmt_lib::format("Test message 4{}" , default_eol))); |
40 | spdlog::set_default_logger(std::move(orig_default_logger)); |
41 | } |
42 | |
43 | TEST_CASE("disable param evaluation" , "[macros]" ) |
44 | { |
45 | SPDLOG_TRACE("Test message {}" , throw std::runtime_error("Should not be evaluated" )); |
46 | } |
47 | |
48 | TEST_CASE("pass logger pointer" , "[macros]" ) |
49 | { |
50 | auto logger = spdlog::create<spdlog::sinks::null_sink_mt>("refmacro" ); |
51 | auto &ref = *logger; |
52 | SPDLOG_LOGGER_TRACE(&ref, "Test message 1" ); |
53 | SPDLOG_LOGGER_DEBUG(&ref, "Test message 2" ); |
54 | } |
55 | |