From 0ddfd6dccc86818fa0e48c1e311c41d0120dec3b Mon Sep 17 00:00:00 2001 From: maj-tom Date: Wed, 6 Dec 2017 20:31:55 -0800 Subject: [PATCH] Fix dangling else in LOG and LOGF macros (#231) * Fix dangling else in LOG and LOGF macros Closes #224 * added unit test --- src/g3log/g3log.hpp | 4 ++-- test_unit/test_io.cpp | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/g3log/g3log.hpp b/src/g3log/g3log.hpp index 26fc41c..6dab3ee 100644 --- a/src/g3log/g3log.hpp +++ b/src/g3log/g3log.hpp @@ -145,7 +145,7 @@ namespace g3 { // LOG(level) is the API for the stream log -#define LOG(level) if(g3::logLevel(level)) INTERNAL_LOG_MESSAGE(level).stream() +#define LOG(level) if(!g3::logLevel(level)){ } else INTERNAL_LOG_MESSAGE(level).stream() // 'Conditional' stream log @@ -209,7 +209,7 @@ And here is possible output : Width trick: 10 : A string \endverbatim */ #define LOGF(level, printf_like_message, ...) \ - if(g3::logLevel(level)) INTERNAL_LOG_MESSAGE(level).capturef(printf_like_message, ##__VA_ARGS__) + if(!g3::logLevel(level)){ } else INTERNAL_LOG_MESSAGE(level).capturef(printf_like_message, ##__VA_ARGS__) // Conditional log printf syntax #define LOGF_IF(level,boolean_expression, printf_like_message, ...) \ diff --git a/test_unit/test_io.cpp b/test_unit/test_io.cpp index 9dd592a..b088757 100644 --- a/test_unit/test_io.cpp +++ b/test_unit/test_io.cpp @@ -269,6 +269,43 @@ TEST(LogTest, LOG) { ASSERT_TRUE(verifyContent(file_content, t_warning3)); } +TEST(LogTest, LOG_after_if) { + std::string file_content; + { + RestoreFileLogger logger(log_directory); + if(false == file_content.empty()) + LOG(INFO) << "This-should-NOT-show-up"; + else + LOG(INFO) << "This-should-show-up"; + + logger.reset(); // force flush of logger + file_content = readFileToText(logger.logFile()); + } + + ASSERT_FALSE(verifyContent(file_content, "This-should-NOT-show-up")); + ASSERT_TRUE(verifyContent(file_content, "This-should-show-up")); +} + + +TEST(LogTest, LOG_after_if_with_parentesis) { + std::string file_content; + { + RestoreFileLogger logger(log_directory); + if(false == file_content.empty()) { + LOG(INFO) << "This-should-NOT-show-up"; + } else { + LOG(INFO) << "This-should-show-up"; + } + + logger.reset(); // force flush of logger + file_content = readFileToText(logger.logFile()); + } + + ASSERT_FALSE(verifyContent(file_content, "This-should-NOT-show-up")); + ASSERT_TRUE(verifyContent(file_content, "This-should-show-up")); +} + + TEST(LogTest, LOG_F_IF) { std::string file_content;