spdlog
Loading...
Searching...
No Matches
test_macros.cpp
Go to the documentation of this file.
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
13TEST_CASE("debug and trace w/o format string", "[macros]]")
14{
15
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
28 REQUIRE(ends_with(file_contents(TEST_FILENAME), fmt::format("Test message 2{}", default_eol)));
30
32
33 SPDLOG_TRACE("Test message 3");
34 SPDLOG_DEBUG("Test message {}", 4);
35 logger->flush();
36
38 REQUIRE(ends_with(file_contents(TEST_FILENAME), fmt::format("Test message 4{}", default_eol)));
39}
40
41TEST_CASE("disable param evaluation", "[macros]")
42{
43 SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated"));
44}
45
46TEST_CASE("pass logger pointer", "[macros]")
47{
48 auto logger = spdlog::create<spdlog::sinks::null_sink_mt>("refmacro");
49 auto &ref = *logger;
50 SPDLOG_LOGGER_TRACE(&ref, "Test message 1");
51 SPDLOG_LOGGER_DEBUG(&ref, "Test message 2");
52}
53
54// ensure that even if right macro level is on- don't evaluate if the logger's level is not high enough
55// TEST_CASE("disable param evaluation2", "[macros]")
56//{
57// auto logger = std::make_shared<spdlog::logger>("test-macro");
58// logger->set_level(spdlog::level::off);
59// int x = 0;
60// SPDLOG_LOGGER_DEBUG(logger, "Test message {}", ++x);
61// REQUIRE(x == 0);
62//}
int count_lines(const char *filename)
#define TEST_CASE(...)
Definition catch.hpp:15119
#define REQUIRE(...)
Definition catch.hpp:15083
#define SPDLOG_FILENAME_T(s)
Definition common.h:107
static SPDLOG_CONSTEXPR const char * default_eol
Definition details/os.h:32
SPDLOG_INLINE void set_default_logger(std::shared_ptr< spdlog::logger > default_logger)
Definition spdlog-inl.h:120
#define SPDLOG_DEBUG(...)
Definition spdlog.h:303
#define SPDLOG_TRACE(...)
Definition spdlog.h:295
#define SPDLOG_LOGGER_TRACE(logger,...)
Definition spdlog.h:294
#define SPDLOG_LOGGER_DEBUG(logger,...)
Definition spdlog.h:302
#define TEST_FILENAME
std::string file_contents(const std::string &filename)
Definition utils.cpp:24
void require_message_count(const std::string &filename, const std::size_t messages)
Definition utils.cpp:49
void prepare_logdir()
Definition utils.cpp:10
bool ends_with(std::string const &value, std::string const &ending)
Definition utils.cpp:73