From 158fdae52b04d57b2922d458a371d48f67da14b2 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Fri, 22 Dec 2023 00:53:08 +0100 Subject: [PATCH] fix: strip nullstream --- src/glog/logging.h.in | 34 +++++++++++++++++++--------------- src/logging.cc | 9 ++++----- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/glog/logging.h.in b/src/glog/logging.h.in index f878231c8..fe30fd659 100644 --- a/src/glog/logging.h.in +++ b/src/glog/logging.h.in @@ -1784,38 +1784,42 @@ GLOG_EXPORT int posix_strerror_r(int err, char *buf, size_t len); GLOG_EXPORT std::string StrError(int err); // A class for which we define operator<<, which does nothing. -class GLOG_EXPORT NullStream : public LogMessage::LogStream { +class GLOG_EXPORT NullStreamBase { public: // Initialize the LogStream so the messages can be written somewhere // (they'll never be actually displayed). This will be needed if a // NullStream& is implicitly converted to LogStream&, in which case // the overloaded NullStream::operator<< will not be invoked. - NullStream(); - NullStream(const char* /*file*/, int /*line*/, - const CheckOpString& /*result*/); - NullStream& stream(); - - private: - // A very short buffer for messages (which we discard anyway). This - // will be needed if NullStream& converted to LogStream& (e.g. as a - // result of a conditional expression). - char message_buffer_[2]; + NullStreamBase() noexcept; + NullStreamBase(const char* /*file*/, int /*line*/, + const CheckOpString& /*result*/) noexcept; + NullStreamBase(const NullStreamBase& other) = delete; + NullStreamBase& operator=(const NullStreamBase& other) = delete; + NullStreamBase& stream() noexcept; }; +class GLOG_EXPORT NullStream final : public NullStreamBase {}; + // Do nothing. This operator is inline, allowing the message to be // compiled away. The message will not be compiled away if we do // something like (flag ? LOG(INFO) : LOG(ERROR)) << message; when // SKIP_LOG=WARNING. In those cases, NullStream will be implicitly // converted to LogStream and the message will be computed and then // quietly discarded. -template -inline NullStream& operator<<(NullStream &str, const T &) { return str; } +template +inline NullStreamBase& operator<<(NullStreamBase& str, const T&) noexcept { + return str; +} +inline NullStreamBase& operator<<( + NullStreamBase& str, std::ostream& (* /*unused*/)(std::ostream&)) noexcept { + return str; +} // Similar to NullStream, but aborts the program (without stack // trace), like LogMessageFatal. -class GLOG_EXPORT NullStreamFatal : public NullStream { +class GLOG_EXPORT NullStreamFatal final : public NullStreamBase { public: - using NullStream::NullStream; + using NullStreamBase::NullStreamBase; [[noreturn]] ~NullStreamFatal(); }; diff --git a/src/logging.cc b/src/logging.cc index bfafe7cdd..7988f6f43 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -1932,11 +1932,10 @@ void LogMessage::RecordCrashReason( GLOG_EXPORT logging_fail_func_t g_logging_fail_func = reinterpret_cast(&abort); -NullStream::NullStream() : LogMessage::LogStream(message_buffer_, 1, 0) {} -NullStream::NullStream(const char* /*file*/, int /*line*/, - const CheckOpString& /*result*/) - : LogMessage::LogStream(message_buffer_, 1, 0) {} -NullStream& NullStream::stream() { return *this; } +NullStreamBase::NullStreamBase() noexcept = default; +NullStreamBase::NullStreamBase(const char* /*file*/, int /*line*/, + const CheckOpString& /*result*/) noexcept {} +NullStreamBase& NullStreamBase::stream() noexcept { return *this; } NullStreamFatal::~NullStreamFatal() { _exit(EXIT_FAILURE); }