Fix dangling else in LOG and LOGF macros (#231)

* Fix dangling else in LOG and LOGF macros
Closes #224

* added unit test
This commit is contained in:
maj-tom 2017-12-06 20:31:55 -08:00 committed by Kjell Hedström
parent e7d3b9d7b1
commit 0ddfd6dccc
2 changed files with 39 additions and 2 deletions

View File

@ -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, ...) \

View File

@ -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;