From 370ac0fb9e7dfeedf91ed508ce2c3ae8c6d82fc2 Mon Sep 17 00:00:00 2001 From: joliph Date: Mon, 6 Jun 2022 22:01:08 +0800 Subject: [PATCH 1/2] simplify DLOG macro definition in NDEBUG mode --- src/glog/logging.h.in | 50 ++++++++----------------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/src/glog/logging.h.in b/src/glog/logging.h.in index 95a573b16..043c87d91 100644 --- a/src/glog/logging.h.in +++ b/src/glog/logging.h.in @@ -691,12 +691,8 @@ class LogSink; // defined below #define LOG_STRING(severity, outvec) \ LOG_TO_STRING_##severity(static_cast*>(outvec)).stream() -#define LOG_IF(severity, condition) \ - static_cast(0), \ - !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity) -#define SYSLOG_IF(severity, condition) \ - static_cast(0), \ - !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & SYSLOG(severity) +#define LOG_IF(severity, condition) if(condition) LOG(severity) +#define SYSLOG_IF(severity, condition) if(condition) SYSLOG(severity) #define LOG_ASSERT(condition) \ LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition @@ -993,9 +989,7 @@ DECLARE_CHECK_STROP_IMPL(strcasecmp, false) __FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, counter, \ &@ac_google_namespace@::LogMessage::SendToLog) -#define PLOG_IF(severity, condition) \ - static_cast(0), \ - !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & PLOG(severity) +#define PLOG_IF(severity, condition) if(condition) PLOG(severity) // A CHECK() macro that postpends errno if the condition is false. E.g. // @@ -1327,30 +1321,17 @@ const LogSeverity GLOG_0 = GLOG_ERROR; #else // !DCHECK_IS_ON() -#define DLOG(severity) \ - static_cast(0), \ - true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity) +#define DLOG(severity) if(false) LOG(severity) -#define DVLOG(verboselevel) \ - static_cast(0), \ - (true || !VLOG_IS_ON(verboselevel)) ? \ - (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(INFO) +#define DVLOG(verboselevel) if(false && VLOG_IS_ON(verboselevel)) LOG(INFO) -#define DLOG_IF(severity, condition) \ - static_cast(0), \ - (true || !(condition)) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity) +#define DLOG_IF(severity, condition) if(false && (condition)) LOG(severity) -#define DLOG_EVERY_N(severity, n) \ - static_cast(0), \ - true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity) +#define DLOG_EVERY_N(severity, n) if(false) LOG(severity) -#define DLOG_IF_EVERY_N(severity, condition, n) \ - static_cast(0), \ - (true || !(condition))? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity) +#define DLOG_IF_EVERY_N(severity, condition, n) if(false && (condition)) LOG(severity) -#define DLOG_ASSERT(condition) \ - static_cast(0), \ - true ? (void) 0 : LOG_ASSERT(condition) +#define DLOG_ASSERT(condition) if(false) LOG_ASSERT(condition) // MSVC warning C4127: conditional expression is constant #define DCHECK(condition) \ @@ -1695,19 +1676,6 @@ class GLOG_EXPORT ErrnoLogMessage : public LogMessage { }; -// This class is used to explicitly ignore values in the conditional -// logging macros. This avoids compiler warnings like "value computed -// is not used" and "statement has no effect". - -class GLOG_EXPORT LogMessageVoidify { - public: - LogMessageVoidify() { } - // This has to be an operator with a precedence lower than << but - // higher than ?: - void operator&(std::ostream&) { } -}; - - // Flushes all log files that contains messages that are at least of // the specified severity level. Thread-safe. GLOG_EXPORT void FlushLogFiles(LogSeverity min_severity); From 82b251822e31925d6920bb7586c99b7a4e095afc Mon Sep 17 00:00:00 2001 From: rain <30932933+lstaroth@users.noreply.github.com> Date: Sun, 7 Aug 2022 01:23:37 +0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Sergiu Deitsch --- src/glog/logging.h.in | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/glog/logging.h.in b/src/glog/logging.h.in index 043c87d91..61654efca 100644 --- a/src/glog/logging.h.in +++ b/src/glog/logging.h.in @@ -1321,17 +1321,17 @@ const LogSeverity GLOG_0 = GLOG_ERROR; #else // !DCHECK_IS_ON() -#define DLOG(severity) if(false) LOG(severity) +#define DLOG(severity) if((false)) LOG(severity) -#define DVLOG(verboselevel) if(false && VLOG_IS_ON(verboselevel)) LOG(INFO) +#define DVLOG(verboselevel) if((false) && VLOG_IS_ON(verboselevel)) LOG(INFO) -#define DLOG_IF(severity, condition) if(false && (condition)) LOG(severity) +#define DLOG_IF(severity, condition) if((false) && (condition)) LOG(severity) -#define DLOG_EVERY_N(severity, n) if(false) LOG(severity) +#define DLOG_EVERY_N(severity, n) if((false)) LOG(severity) -#define DLOG_IF_EVERY_N(severity, condition, n) if(false && (condition)) LOG(severity) +#define DLOG_IF_EVERY_N(severity, condition, n) if((false) && (condition)) LOG(severity) -#define DLOG_ASSERT(condition) if(false) LOG_ASSERT(condition) +#define DLOG_ASSERT(condition) if((false)) LOG_ASSERT(condition) // MSVC warning C4127: conditional expression is constant #define DCHECK(condition) \