@@ -703,8 +703,12 @@ class LogSink; // defined below
703703#define LOG_STRING (severity , outvec ) \
704704 LOG_TO_STRING_##severity(static_cast<std::vector<std::string>*>(outvec)).stream()
705705
706- #define LOG_IF (severity , condition ) if(condition) LOG(severity)
707- #define SYSLOG_IF (severity , condition ) if(condition) SYSLOG(severity)
706+ #define LOG_IF (severity , condition ) \
707+ static_cast<void>(0), \
708+ !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
709+ #define SYSLOG_IF (severity , condition ) \
710+ static_cast<void>(0), \
711+ !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & SYSLOG(severity)
708712
709713#define LOG_ASSERT (condition ) \
710714 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
@@ -1001,7 +1005,9 @@ DECLARE_CHECK_STROP_IMPL(strcasecmp, false)
10011005 __FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, counter, \
10021006 &@ac_google_namespace@::LogMessage::SendToLog)
10031007
1004- #define PLOG_IF (severity , condition ) if(condition) PLOG(severity)
1008+ #define PLOG_IF (severity , condition ) \
1009+ static_cast<void>(0), \
1010+ !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & PLOG(severity)
10051011
10061012// A CHECK() macro that postpends errno if the condition is false. E.g.
10071013//
@@ -1333,17 +1339,30 @@ const LogSeverity GLOG_0 = GLOG_ERROR;
13331339
13341340#else // !DCHECK_IS_ON()
13351341
1336- #define DLOG (severity ) if((false)) LOG(severity)
1342+ #define DLOG (severity ) \
1343+ static_cast<void>(0), \
1344+ true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
13371345
1338- #define DVLOG (verboselevel ) if((false) && VLOG_IS_ON(verboselevel)) LOG(INFO)
1346+ #define DVLOG (verboselevel ) \
1347+ static_cast<void>(0), \
1348+ (true || !VLOG_IS_ON(verboselevel)) ? \
1349+ (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(INFO)
13391350
1340- #define DLOG_IF (severity , condition ) if((false) && (condition)) LOG(severity)
1351+ #define DLOG_IF (severity , condition ) \
1352+ static_cast<void>(0), \
1353+ (true || !(condition)) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
13411354
1342- #define DLOG_EVERY_N (severity , n ) if((false)) LOG(severity)
1355+ #define DLOG_EVERY_N (severity , n ) \
1356+ static_cast<void>(0), \
1357+ true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
13431358
1344- #define DLOG_IF_EVERY_N (severity , condition , n ) if((false) && (condition)) LOG(severity)
1359+ #define DLOG_IF_EVERY_N (severity , condition , n ) \
1360+ static_cast<void>(0), \
1361+ (true || !(condition))? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
13451362
1346- #define DLOG_ASSERT (condition ) if((false)) LOG_ASSERT(condition)
1363+ #define DLOG_ASSERT (condition ) \
1364+ static_cast<void>(0), \
1365+ true ? (void) 0 : LOG_ASSERT(condition)
13471366
13481367// MSVC warning C4127: conditional expression is constant
13491368#define DCHECK (condition ) \
@@ -1688,6 +1707,19 @@ class GLOG_EXPORT ErrnoLogMessage : public LogMessage {
16881707};
16891708
16901709
1710+ // This class is used to explicitly ignore values in the conditional
1711+ // logging macros. This avoids compiler warnings like "value computed
1712+ // is not used" and "statement has no effect".
1713+
1714+ class GLOG_EXPORT LogMessageVoidify {
1715+ public :
1716+ LogMessageVoidify () { }
1717+ // This has to be an operator with a precedence lower than << but
1718+ // higher than ?:
1719+ void operator & (std ::ostream & ) { }
1720+ };
1721+
1722+
16911723// Flushes all log files that contains messages that are at least of
16921724// the specified severity level. Thread-safe.
16931725GLOG_EXPORT void FlushLogFiles (LogSeverity min_severity );
0 commit comments