Use GlobalLock to protect logging

rtc::CriticalSection has non-trivial destructor and thus
shouldn't be used for variable with static storage duration


Bug: None
Change-Id: I5b9d9036aa90eb0c652f6b17ea1162dea0362640
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/156563
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29469}
This commit is contained in:
Danil Chapovalov
2019-10-11 17:18:29 +02:00
committed by Commit Bot
parent 65c57ff6af
commit ef98ae6bbb
2 changed files with 9 additions and 7 deletions

View File

@ -247,6 +247,7 @@ rtc_source_set("logging") {
":platform_thread_types",
":stringutils",
":timeutils",
"//third_party/abseil-cpp/absl/base:core_headers",
"//third_party/abseil-cpp/absl/meta:type_traits",
"//third_party/abseil-cpp/absl/strings",
]

View File

@ -35,6 +35,7 @@ static const int kMaxLogLineSize = 1024 - 60;
#include <cstdarg>
#include <vector>
#include "absl/base/attributes.h"
#include "rtc_base/checks.h"
#include "rtc_base/critical_section.h"
#include "rtc_base/logging.h"
@ -67,7 +68,7 @@ const char* FilenameFromPath(const char* file) {
}
// Global lock for log subsystem, only needed to serialize access to streams_.
CriticalSection g_log_crit;
ABSL_CONST_INIT GlobalLock g_log_crit;
} // namespace
// Inefficient default implementation, override is recommended.
@ -199,7 +200,7 @@ LogMessage::~LogMessage() {
#endif
}
CritScope cs(&g_log_crit);
GlobalLockScope cs(&g_log_crit);
for (auto& kv : streams_) {
if (severity_ >= kv.second) {
#if defined(WEBRTC_ANDROID)
@ -248,7 +249,7 @@ void LogMessage::LogTimestamps(bool on) {
void LogMessage::LogToDebug(LoggingSeverity min_sev) {
g_dbg_sev = min_sev;
CritScope cs(&g_log_crit);
GlobalLockScope cs(&g_log_crit);
UpdateMinLogSeverity();
}
@ -257,7 +258,7 @@ void LogMessage::SetLogToStderr(bool log_to_stderr) {
}
int LogMessage::GetLogToStream(LogSink* stream) {
CritScope cs(&g_log_crit);
GlobalLockScope cs(&g_log_crit);
LoggingSeverity sev = LS_NONE;
for (auto& kv : streams_) {
if (!stream || stream == kv.first) {
@ -268,13 +269,13 @@ int LogMessage::GetLogToStream(LogSink* stream) {
}
void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
CritScope cs(&g_log_crit);
GlobalLockScope cs(&g_log_crit);
streams_.push_back(std::make_pair(stream, min_sev));
UpdateMinLogSeverity();
}
void LogMessage::RemoveLogToStream(LogSink* stream) {
CritScope cs(&g_log_crit);
GlobalLockScope cs(&g_log_crit);
for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
if (stream == it->first) {
streams_.erase(it);
@ -443,7 +444,7 @@ bool LogMessage::IsNoop(LoggingSeverity severity) {
// 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.
CritScope cs(&g_log_crit);
GlobalLockScope cs(&g_log_crit);
if (streams_.size() > 0)
return false;