Fix uninitialized LogSink::min_severity_ and improve InitializeLogging - Default min_severity_ to LS_INFO at declaration in LogSink to avoid uninitialized access. - Add queue_name() getter to LogLineRef. - Transfer log_queue_name, log_timestamp, log_to_stderr, and debug_severity in InitializeLogging. - Update tests to verify changes. Bug: None Change-Id: Ie972b4c9a7f948e27b316b2127be4a53b0c35435 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/466980 Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org> Cr-Commit-Position: refs/heads/main@{#47557}
diff --git a/rtc_base/logging.cc b/rtc_base/logging.cc index d897272..076167c 100644 --- a/rtc_base/logging.cc +++ b/rtc_base/logging.cc
@@ -111,6 +111,10 @@ GetOrInitConfig(&config, config_applied); if (config_applied) { MutexLock lock(&GetLoggingLock()); + LogMessage::SetLogQueueNames(config.log_queue_name()); + LogMessage::LogTimestamps(config.log_timestamp()); + LogMessage::SetLogToStderr(config.log_to_stderr()); + g_dbg_sev = config.debug_severity(); LogMessage::UpdateMinLogSeverity(); } return config_applied;
diff --git a/rtc_base/logging.h b/rtc_base/logging.h index 60c05c7..d7521b3 100644 --- a/rtc_base/logging.h +++ b/rtc_base/logging.h
@@ -125,6 +125,7 @@ Timestamp timestamp() const { return timestamp_; } absl::string_view tag() const { return tag_; } LoggingSeverity severity() const { return severity_; } + absl::string_view queue_name() const { return queue_name_; } #if RTC_LOG_ENABLED() std::string DefaultLogLine() const; @@ -190,7 +191,7 @@ #if RTC_LOG_ENABLED() // Members for LogMessage class to keep linked list of the registered sinks. LogSink* next_ = nullptr; - LoggingSeverity min_severity_; + LoggingSeverity min_severity_ = LS_INFO; #endif };
diff --git a/rtc_base/logging_unittest.cc b/rtc_base/logging_unittest.cc index aea44c1..d31d9e5 100644 --- a/rtc_base/logging_unittest.cc +++ b/rtc_base/logging_unittest.cc
@@ -506,6 +506,54 @@ #endif } +TEST(LogTest, InitializeLoggingTransfersQueueName) { + GTEST_FLAG_SET(death_test_style, "threadsafe"); +#if defined(WEBRTC_WIN) + _putenv_s("WEBRTC_TEST_SKIP_LOGGING_INIT", "1"); +#else + setenv("WEBRTC_TEST_SKIP_LOGGING_INIT", "1", 1); +#endif + EXPECT_EXIT( + { + LoggingConfig config; + config.set_log_queue_name(true); + + struct CustomSink : public LogSink { + void OnLogMessage(const LogLineRef& line) override { + queue_name = std::string(line.queue_name()); + } + void OnLogMessage(const std::string& message) override {} + void OnLogMessage(absl::string_view message) override {} + std::string queue_name; + }; + + auto sink = std::make_unique<CustomSink>(); + CustomSink* sink_ptr = sink.get(); + config.AddSink(std::move(sink)); + + if (!InitializeLogging(std::move(config))) { + exit(2); + } + + std::unique_ptr<Thread> thread = Thread::Create(); + thread->SetName("TestQueue", nullptr); + thread->Start(); + + thread->BlockingCall([&]() { RTC_LOG(LS_INFO) << "Hello"; }); + + if (sink_ptr->queue_name != "TestQueue") { + exit(1); + } + exit(0); + }, + ::testing::ExitedWithCode(0), ""); +#if defined(WEBRTC_WIN) + _putenv_s("WEBRTC_TEST_SKIP_LOGGING_INIT", ""); +#else + unsetenv("WEBRTC_TEST_SKIP_LOGGING_INIT"); +#endif +} + #endif // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) && // !defined(WEBRTC_IOS)