fix: strip nullstream
This commit is contained in:
parent
e92ab7df52
commit
bbe788adfc
@ -1784,39 +1784,52 @@ GLOG_EXPORT int posix_strerror_r(int err, char *buf, size_t len);
|
|||||||
GLOG_EXPORT std::string StrError(int err);
|
GLOG_EXPORT std::string StrError(int err);
|
||||||
|
|
||||||
// A class for which we define operator<<, which does nothing.
|
// A class for which we define operator<<, which does nothing.
|
||||||
class GLOG_EXPORT NullStream : public LogMessage::LogStream {
|
class GLOG_EXPORT NullStreamBase {
|
||||||
public:
|
public:
|
||||||
// Initialize the LogStream so the messages can be written somewhere
|
// Initialize the LogStream so the messages can be written somewhere
|
||||||
// (they'll never be actually displayed). This will be needed if a
|
// (they'll never be actually displayed). This will be needed if a
|
||||||
// NullStream& is implicitly converted to LogStream&, in which case
|
// NullStream& is implicitly converted to LogStream&, in which case
|
||||||
// the overloaded NullStream::operator<< will not be invoked.
|
// the overloaded NullStream::operator<< will not be invoked.
|
||||||
NullStream();
|
NullStreamBase() noexcept;
|
||||||
NullStream(const char* /*file*/, int /*line*/,
|
NullStreamBase(const char* /*file*/, int /*line*/,
|
||||||
const CheckOpString& /*result*/);
|
const CheckOpString& /*result*/) noexcept;
|
||||||
NullStream& stream();
|
NullStreamBase(const NullStreamBase& other) = delete;
|
||||||
|
NullStreamBase& operator=(const NullStreamBase& other) = delete;
|
||||||
|
NullStreamBase& stream() noexcept;
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
// A very short buffer for messages (which we discard anyway). This
|
// Prevent the class to be deleted through a base class pointer
|
||||||
// will be needed if NullStream& converted to LogStream& (e.g. as a
|
~NullStreamBase();
|
||||||
// result of a conditional expression).
|
|
||||||
char message_buffer_[2];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class GLOG_EXPORT NullStream final : public NullStreamBase {};
|
||||||
|
|
||||||
// Do nothing. This operator is inline, allowing the message to be
|
// Do nothing. This operator is inline, allowing the message to be
|
||||||
// compiled away. The message will not be compiled away if we do
|
// compiled away. The message will not be compiled away if we do
|
||||||
// something like (flag ? LOG(INFO) : LOG(ERROR)) << message; when
|
// something like (flag ? LOG(INFO) : LOG(ERROR)) << message; when
|
||||||
// SKIP_LOG=WARNING. In those cases, NullStream will be implicitly
|
// SKIP_LOG=WARNING. In those cases, NullStream will be implicitly
|
||||||
// converted to LogStream and the message will be computed and then
|
// converted to LogStream and the message will be computed and then
|
||||||
// quietly discarded.
|
// quietly discarded.
|
||||||
template<class T>
|
template <class T>
|
||||||
inline NullStream& operator<<(NullStream &str, const T &) { return str; }
|
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
|
// Similar to NullStream, but aborts the program (without stack
|
||||||
// trace), like LogMessageFatal.
|
// trace), like LogMessageFatal.
|
||||||
class GLOG_EXPORT NullStreamFatal : public NullStream {
|
class GLOG_EXPORT NullStreamFatal final : public NullStreamBase {
|
||||||
public:
|
public:
|
||||||
using NullStream::NullStream;
|
using NullStreamBase::NullStreamBase;
|
||||||
[[noreturn]] ~NullStreamFatal();
|
[[noreturn]]
|
||||||
|
#if defined(__GNUG__)
|
||||||
|
// Avoid optimizing the destructor away
|
||||||
|
[[gnu::used]]
|
||||||
|
#endif // defined(__GNUG__)
|
||||||
|
~NullStreamFatal();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Install a signal handler that will dump signal information and a stack
|
// Install a signal handler that will dump signal information and a stack
|
||||||
|
|||||||
@ -1932,11 +1932,11 @@ void LogMessage::RecordCrashReason(
|
|||||||
GLOG_EXPORT logging_fail_func_t g_logging_fail_func =
|
GLOG_EXPORT logging_fail_func_t g_logging_fail_func =
|
||||||
reinterpret_cast<logging_fail_func_t>(&abort);
|
reinterpret_cast<logging_fail_func_t>(&abort);
|
||||||
|
|
||||||
NullStream::NullStream() : LogMessage::LogStream(message_buffer_, 1, 0) {}
|
NullStreamBase::NullStreamBase() noexcept = default;
|
||||||
NullStream::NullStream(const char* /*file*/, int /*line*/,
|
NullStreamBase::NullStreamBase(const char* /*file*/, int /*line*/,
|
||||||
const CheckOpString& /*result*/)
|
const CheckOpString& /*result*/) noexcept {}
|
||||||
: LogMessage::LogStream(message_buffer_, 1, 0) {}
|
NullStreamBase& NullStreamBase::stream() noexcept { return *this; }
|
||||||
NullStream& NullStream::stream() { return *this; }
|
NullStreamBase::~NullStreamBase() = default;
|
||||||
|
|
||||||
NullStreamFatal::~NullStreamFatal() { _exit(EXIT_FAILURE); }
|
NullStreamFatal::~NullStreamFatal() { _exit(EXIT_FAILURE); }
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user