#1119·glog

Can we use google::NullStream instead of google::LogMessageVoidify in LOG_IF?

Author: foleryCreated Aug 1, 2024Updated Oct 31, 2024
bash
#define LOG_IF(severity, condition) \
  static_cast<void>(0),             \
      !(condition)                  \
          ? (void)0                 \
          : google::logging::internal::LogMessageVoidify() & LOG(severity)

This is how LOG_IF is implemented now. After reading the doc https://github.com/asplingzhang/asplingzhang.github.io/blob/main/docs/_posts/webrtc/logging/2022-06-14-why-static_cast-void-and-LogMessageVoidify-needed-in-logging-macros.md, I understand the reason for using LogMessageVoidify, while it turns the LOG_IF from ostream into void. If someone wants to wrap LOG_IF or VLOG, it causes error:

bash
#define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel))

#define MYLOG \
    static_cast<void>(0),             \
      !(condition)                  \
          ? (void)0                 \
          : google::logging::internal::LogMessageVoidify() & VLOG(1)

The code above causes errors like error: no match for 'operator&' (operand types are 'google::LogMessageVoidify' and 'void') I can't use VLOG_IF in my wrapper for other reasons.

So I want to know if I can use NullStream instead like:

bash
#define LOG_IF(severity, condition) \
      !(condition)                  \
          ? google::NullStream().stream()                \
          :  LOG(severity)

Thus LOG_IF is still an ostream and we can wrap it. What's the difference between using google::NullStream and LogMessageVoidify?