From 21ed31844c90c45569b70ee440cc8c83730fdeb3 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 13 Oct 2017 02:04:31 +0300 Subject: [PATCH] fixed bug in SPDLOG_TRACE_IF macro and added some related tests --- include/spdlog/spdlog.h | 4 ++-- tests/includes.h | 3 +++ tests/test_macros.cpp | 43 +++++++++++++++++++++++++++++++++++++ tests/tests.vcxproj | 1 + tests/tests.vcxproj.filters | 3 +++ tests/utils.cpp | 8 +++++++ tests/utils.h | 1 + 7 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/test_macros.cpp diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index c4b6a0bb..1653c601 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -170,10 +170,10 @@ void drop_all(); #define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x) #ifdef _MSC_VER #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) - #define SPDLOG_TRACE_IF(logger, flag, ...) logger.trace_if(flag, "[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) + #define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) #else #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) - #define SPDLOG_TRACE_IF(logger, flag, ...) logger.trace_if(flag, "[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) + #define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) #endif #else #define SPDLOG_TRACE(logger, ...) diff --git a/tests/includes.h b/tests/includes.h index 0590fc60..6c2a539f 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -10,6 +10,9 @@ #include "catch.hpp" #include "utils.h" +#define SPDLOG_TRACE_ON +#define SPDLOG_DEBUG_ON + #include "../include/spdlog/spdlog.h" #include "../include/spdlog/sinks/null_sink.h" #include "../include/spdlog/sinks/ostream_sink.h" diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp new file mode 100644 index 00000000..7aeadc43 --- /dev/null +++ b/tests/test_macros.cpp @@ -0,0 +1,43 @@ +/* +* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE +*/ + +#include "includes.h" + +TEST_CASE("debug and trace w/o format string", "[macros]]") +{ + prepare_logdir(); + std::string filename = "logs/simple_log"; + + auto logger = spdlog::create("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 2"); + logger->flush(); + + REQUIRE(ends_with(file_contents(filename), "Test message 2\n")); + REQUIRE(count_lines(filename) == 2); +} + + +TEST_CASE("debug and trace with format strings", "[macros]]") +{ + prepare_logdir(); + std::string filename = "logs/simple_log"; + + auto logger = spdlog::create("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); +} + diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index f5c854db..9b63d53b 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -132,6 +132,7 @@ + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index b5612d0c..0effbef9 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -39,6 +39,9 @@ Source Files + + Source Files + diff --git a/tests/utils.cpp b/tests/utils.cpp index e0785352..d454b2d6 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -46,3 +46,11 @@ std::size_t get_filesize(const std::string& filename) return static_cast(ifs.tellg()); } + + +// source: https://stackoverflow.com/a/2072890/192001 +bool ends_with(std::string const & value, std::string const & ending) +{ + if (ending.size() > value.size()) return false; + return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); +} diff --git a/tests/utils.h b/tests/utils.h index 1d9b6213..819bcee6 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -13,3 +13,4 @@ std::size_t count_lines(const std::string& filename); std::size_t get_filesize(const std::string& filename); +bool ends_with(std::string const & value, std::string const & ending); \ No newline at end of file