diff --git a/rtc_base/logging.cc b/rtc_base/logging.cc index bd2afcc9dd..94ddcba6d2 100644 --- a/rtc_base/logging.cc +++ b/rtc_base/logging.cc @@ -90,6 +90,7 @@ bool LogMessage::log_to_stderr_ = true; // cleanup by setting to null, or let it leak (safe at program exit). ABSL_CONST_INIT LogSink* LogMessage::streams_ RTC_GUARDED_BY(g_log_mutex_) = nullptr; +ABSL_CONST_INIT std::atomic LogMessage::streams_empty_ = {true}; // Boolean options default to false (0) bool LogMessage::thread_, LogMessage::timestamp_; @@ -269,6 +270,7 @@ void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) { stream->min_severity_ = min_sev; stream->next_ = streams_; streams_ = stream; + streams_empty_.store(false, std::memory_order_relaxed); UpdateMinLogSeverity(); } @@ -281,6 +283,7 @@ void LogMessage::RemoveLogToStream(LogSink* stream) { break; } } + streams_empty_.store(streams_ == nullptr, std::memory_order_relaxed); UpdateMinLogSeverity(); } @@ -438,12 +441,7 @@ void LogMessage::OutputToDebug(const std::string& str, bool LogMessage::IsNoop(LoggingSeverity severity) { if (severity >= g_dbg_sev || severity >= g_min_sev) return false; - - // TODO(tommi): We're grabbing this lock for every LogMessage instance that - // is going to be logged. This introduces unnecessary synchronization for - // a feature that's mostly used for testing. - webrtc::MutexLock lock(&g_log_mutex_); - return streams_ == nullptr; + return streams_empty_.load(std::memory_order_relaxed); } void LogMessage::FinishPrintStream() { diff --git a/rtc_base/logging.h b/rtc_base/logging.h index 0aa1e676d1..1e5f7258a3 100644 --- a/rtc_base/logging.h +++ b/rtc_base/logging.h @@ -46,6 +46,7 @@ #include +#include #include // no-presubmit-check TODO(webrtc:8982) #include #include @@ -557,6 +558,12 @@ class LogMessage { // The output streams and their associated severities static LogSink* streams_; + // Holds true with high probability if |streams_| is empty, false with high + // probability otherwise. Operated on with std::memory_order_relaxed because + // it's ok to loose or log some additional statements near the instant streams + // are added/removed. + static std::atomic streams_empty_; + // Flags for formatting options static bool thread_, timestamp_;