From 9369fe8c27260ca7929559a0f4e11980a8b3ade6 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 9 Oct 2019 21:41:02 +0300 Subject: [PATCH] Fix #1262 --- include/spdlog/logger-inl.h | 5 +++++ include/spdlog/logger.h | 4 ++++ include/spdlog/spdlog.h | 5 ++++- tests/test_macros.cpp | 10 ++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 05c57e8e..f7605de5 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -69,6 +69,11 @@ SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const return msg_level >= level_.load(std::memory_order_relaxed); } +SPDLOG_INLINE bool logger::should_backtrace() const +{ + return tracer_.enabled(); +} + SPDLOG_INLINE void logger::set_level(level::level_enum log_level) { level_.store(log_level); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 0e4eaa00..56b982fe 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -319,8 +319,12 @@ public: #endif // _WIN32 #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT + // return true logging is enabled for the given level. bool should_log(level::level_enum msg_level) const; + // return true if backtrace logging is enabled. + bool should_backtrace() const; + void set_level(level::level_enum log_level); level::level_enum level() const; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 3638ba12..a0d3c912 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -285,7 +285,10 @@ inline void critical(wstring_view_t fmt, const Args &... args) // SPDLOG_LEVEL_OFF // -#define SPDLOG_LOGGER_CALL(logger, level, ...) logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) +#define SPDLOG_LOGGER_CALL(logger, level, ...) do {\ + if(logger->should_log(level) || logger->should_backtrace()) \ + logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__);\ + } while(0) #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #define SPDLOG_LOGGER_TRACE(logger, ...) SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 22a5ccbf..6a57a166 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -39,3 +39,13 @@ TEST_CASE("disable param evaluation", "[macros]") { SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated")); } + +// ensure that even if right macro level is on- don't eavluate if the logger's level is not high enough +TEST_CASE("disable param evaluation2", "[macros]") +{ + auto logger = std::make_shared("test-macro"); + logger->set_level(spdlog::level::off); + int x = 0; + SPDLOG_LOGGER_DEBUG(logger, "Test message {}", ++x); + REQUIRE(x == 0); +}