test_macros.cpp 1.81 KB
Newer Older
1
/*
gabime's avatar
gabime committed
2 3
 * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
 */
4 5 6

#include "includes.h"

7 8 9 10
#if SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_DEBUG
#error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG"
#endif

11 12
TEST_CASE("debug and trace w/o format string", "[macros]]")
{
13

gabime's avatar
gabime committed
14
    prepare_logdir();
gabime's avatar
gabime committed
15
    std::string filename = "test_logs/simple_log";
gabime's avatar
gabime committed
16

17
    auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
gabime's avatar
gabime committed
18 19 20
    logger->set_pattern("%v");
    logger->set_level(spdlog::level::trace);

21 22
    SPDLOG_LOGGER_TRACE(logger, "Test message 1");
    SPDLOG_LOGGER_DEBUG(logger, "Test message 2");
gabime's avatar
gabime committed
23 24 25
    logger->flush();

    REQUIRE(ends_with(file_contents(filename), "Test message 2\n"));
26
    REQUIRE(count_lines(filename) == 1);
gabime's avatar
gabime committed
27 28 29 30 31 32 33 34 35

    spdlog::set_default_logger(logger);

    SPDLOG_TRACE("Test message 3");
    SPDLOG_DEBUG("Test message {}", 4);
    logger->flush();

    REQUIRE(ends_with(file_contents(filename), "Test message 4\n"));
    REQUIRE(count_lines(filename) == 2);
36 37
}

38
TEST_CASE("disable param evaluation", "[macros]")
39
{
40
    SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated"));
41
}
gabime's avatar
gabime committed
42

43
TEST_CASE("pass logger pointer", "[macros]")
Paul Kunysch's avatar
Paul Kunysch committed
44 45
{
    auto logger = spdlog::create<spdlog::sinks::null_sink_mt>("refmacro");
46
    auto &ref = *logger;
Paul Kunysch's avatar
Paul Kunysch committed
47 48 49 50
    SPDLOG_LOGGER_TRACE(&ref, "Test message 1");
    SPDLOG_LOGGER_DEBUG(&ref, "Test message 2");
}

51
// ensure that even if right macro level is on- don't evaluate if the logger's level is not high enough
gabime's avatar
gabime committed
52
// TEST_CASE("disable param evaluation2", "[macros]")
53 54 55 56 57 58 59
//{
//    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);
//}