Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
folery opened this issue Aug 1, 2024 · 1 comment

Comments

@folery
Copy link

folery commented Aug 1, 2024

#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:

#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:

#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?

@JensHuthmann
Copy link

I am using this as a work around for #933

#ifndef GOOGLE_STRIP_LOG
#define GOOGLE_STRIP_LOG 0
#endif

#include <glog/logging.h>

#define MY_LOG_INT_(severity, lvl) \
    if constexpr (GOOGLE_STRIP_LOG >= lvl) {} \
    else LOG(severity)

#define MY_LOGIF_INT_(severity, test, lvl) \
    if constexpr (GOOGLE_STRIP_LOG >= lvl) {} \
    else LOG_IF(severity, test)

#define SEVERITY_LVL_INFO (1)
#define SEVERITY_LVL_WARNING (2)
#define SEVERITY_LVL_ERROR (3)
#define SEVERITY_LVL_FATAL (4)

#define MY_LOG(severity) \
    if constexpr (GOOGLE_STRIP_LOG >= SEVERITY_LVL_##severity) {} \
    else LOG(severity)

#define MY_LOG_BUILD(severity, code) \
    if constexpr (GOOGLE_STRIP_LOG >= SEVERITY_LVL_##severity) {} \
    else { code }

#define MY_LOG_IF(severity, test) MY_LOGIF_INT_(severity, test, SEVERITY_LVL_##severity)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants