New compile time API using SPDLOG_ACTIVE_LEVEL

This commit is contained in:
gabime
2018-11-11 18:15:24 +02:00
parent f1e79bde2e
commit 0e77c3391b
8 changed files with 91 additions and 66 deletions

View File

@@ -10,8 +10,7 @@
#include <ostream>
#include <string>
#define SPDLOG_TRACE_ON
#define SPDLOG_DEBUG_ON
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#define SPDLOG_ENABLE_MESSAGE_COUNTER
#include "spdlog/spdlog.h"

View File

@@ -4,8 +4,13 @@
#include "includes.h"
#if SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_DEBUG
#error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG"
#endif
TEST_CASE("debug and trace w/o format string", "[macros]]")
{
prepare_logdir();
std::string filename = "logs/simple_log";
@@ -13,28 +18,15 @@ TEST_CASE("debug and trace w/o format string", "[macros]]")
logger->set_pattern("%v");
logger->set_level(spdlog::level::trace);
SPDLOG_TRACE(logger, "Test message 1");
SPDLOG_DEBUG(logger, "Test message 2");
SPDLOG_LOGGER_TRACE(logger, "Test message 1");
SPDLOG_LOGGER_DEBUG(logger, "Test message 2");
logger->flush();
REQUIRE(ends_with(file_contents(filename), "Test message 2\n"));
REQUIRE(count_lines(filename) == 2);
REQUIRE(count_lines(filename) == 1);
}
TEST_CASE("debug and trace with format strings", "[macros]]")
TEST_CASE("disable param evaluation", "[macros]")
{
prepare_logdir();
std::string filename = "logs/simple_log";
auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
logger->set_pattern("%v");
logger->set_level(spdlog::level::trace);
SPDLOG_TRACE(logger, "Test message {}", 1);
// SPDLOG_DEBUG(logger, "Test message 2");
SPDLOG_DEBUG(logger, "Test message {}", 222);
logger->flush();
REQUIRE(ends_with(file_contents(filename), "Test message 222\n"));
REQUIRE(count_lines(filename) == 2);
SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated"));
}