Migrate logging.h to webrtc namespace

Bug: webrtc:42232595
Change-Id: I490fd10f9b80b8ed86b0e270903e23adfb875f85
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/381780
Auto-Submit: Evan Shrubsole <eshr@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Evan Shrubsole <eshr@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#44169}
diff --git a/common_video/h264/sps_vui_rewriter_unittest.cc b/common_video/h264/sps_vui_rewriter_unittest.cc
index f0f7b4d..525872f 100644
--- a/common_video/h264/sps_vui_rewriter_unittest.cc
+++ b/common_video/h264/sps_vui_rewriter_unittest.cc
@@ -303,8 +303,8 @@
 void TestSps(const VuiHeader& vui,
              const ColorSpace* color_space,
              SpsVuiRewriter::ParseResult expected_parse_result) {
-  rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
-  rtc::Buffer original_sps;
+  LogMessage::LogToDebug(rtc::LS_VERBOSE);
+  Buffer original_sps;
   GenerateFakeSps(vui, &original_sps);
 
   std::optional<SpsParser::SpsState> sps;
@@ -391,7 +391,7 @@
                         SpsVuiRewriter::ParseResult::kVuiRewritten)));
 
 TEST(SpsVuiRewriterOutgoingVuiTest, ParseOutgoingBitstreamOptimalVui) {
-  rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
+  LogMessage::LogToDebug(rtc::LS_VERBOSE);
 
   rtc::Buffer optimal_sps;
   GenerateFakeSps(kVuiNoFrameBuffering, &optimal_sps);
@@ -407,7 +407,7 @@
 }
 
 TEST(SpsVuiRewriterOutgoingVuiTest, ParseOutgoingBitstreamNoVui) {
-  rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
+  LogMessage::LogToDebug(rtc::LS_VERBOSE);
 
   rtc::Buffer sps;
   GenerateFakeSps(kVuiNotPresent, &sps);
@@ -438,7 +438,7 @@
 }
 
 TEST(SpsVuiRewriterOutgoingAudTest, ParseOutgoingBitstreamWithAud) {
-  rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
+  LogMessage::LogToDebug(rtc::LS_VERBOSE);
 
   rtc::Buffer optimal_sps;
   GenerateFakeSps(kVuiNoFrameBuffering, &optimal_sps);
diff --git a/rtc_base/logging.cc b/rtc_base/logging.cc
index 83dd1bc..cf28db4 100644
--- a/rtc_base/logging.cc
+++ b/rtc_base/logging.cc
@@ -53,7 +53,7 @@
 #include "rtc_base/thread_annotations.h"
 #include "rtc_base/time_utils.h"
 
-namespace rtc {
+namespace webrtc {
 namespace {
 
 // By default, release builds don't log, debug builds at info level
@@ -78,16 +78,16 @@
 }
 
 // Global lock for log subsystem, only needed to serialize access to streams_.
-webrtc::Mutex& GetLoggingLock() {
-  static webrtc::Mutex& mutex = *new webrtc::Mutex();
+Mutex& GetLoggingLock() {
+  static Mutex& mutex = *new Mutex();
   return mutex;
 }
 
 }  // namespace
 
 std::string LogLineRef::DefaultLogLine() const {
-  webrtc::StringBuilder log_output;
-  if (timestamp_ != webrtc::Timestamp::MinusInfinity()) {
+  StringBuilder log_output;
+  if (timestamp_ != Timestamp::MinusInfinity()) {
     // TODO(kwiberg): Switch to absl::StrFormat, if binary size is ok.
     char timestamp[50];  // Maximum string length of an int64_t is 20.
     int len =
@@ -141,15 +141,15 @@
     int64_t log_start_time = LogStartTime();
     // Use SystemTimeMillis so that even if tests use fake clocks, the timestamp
     // in log messages represents the real system time.
-    int64_t time = TimeDiff(SystemTimeMillis(), log_start_time);
+    int64_t time = rtc::TimeDiff(rtc::SystemTimeMillis(), log_start_time);
     // Also ensure WallClockStartTime is initialized, so that it matches
     // LogStartTime.
     WallClockStartTime();
-    log_line_.set_timestamp(webrtc::Timestamp::Millis(time));
+    log_line_.set_timestamp(Timestamp::Millis(time));
   }
 
   if (log_thread_) {
-    log_line_.set_thread_id(CurrentThreadId());
+    log_line_.set_thread_id(rtc::CurrentThreadId());
   }
 
   if (file != nullptr) {
@@ -162,7 +162,7 @@
 
   if (err_ctx != ERRCTX_NONE) {
     char tmp_buf[1024];
-    webrtc::SimpleStringBuilder tmp(tmp_buf);
+    SimpleStringBuilder tmp(tmp_buf);
     tmp.AppendFormat("[0x%08X]", err);
     switch (err_ctx) {
       case ERRCTX_ERRNO:
@@ -212,7 +212,7 @@
     OutputToDebug(log_line_);
   }
 
-  webrtc::MutexLock lock(&GetLoggingLock());
+  MutexLock lock(&GetLoggingLock());
   for (LogSink* entry = streams_; entry != nullptr; entry = entry->next_) {
     if (log_line_.severity() >= entry->min_severity_) {
       entry->OnLogMessage(log_line_);
@@ -226,7 +226,7 @@
 #endif
 }
 
-webrtc::StringBuilder& LogMessage::stream() {
+StringBuilder& LogMessage::stream() {
   return print_stream_;
 }
 
@@ -238,7 +238,7 @@
   return g_dbg_sev;
 }
 int64_t LogMessage::LogStartTime() {
-  static const int64_t g_start = SystemTimeMillis();
+  static const int64_t g_start = rtc::SystemTimeMillis();
   return g_start;
 }
 
@@ -257,7 +257,7 @@
 
 void LogMessage::LogToDebug(LoggingSeverity min_sev) {
   g_dbg_sev = min_sev;
-  webrtc::MutexLock lock(&GetLoggingLock());
+  MutexLock lock(&GetLoggingLock());
   UpdateMinLogSeverity();
 }
 
@@ -266,7 +266,7 @@
 }
 
 int LogMessage::GetLogToStream(LogSink* stream) {
-  webrtc::MutexLock lock(&GetLoggingLock());
+  MutexLock lock(&GetLoggingLock());
   LoggingSeverity sev = LS_NONE;
   for (LogSink* entry = streams_; entry != nullptr; entry = entry->next_) {
     if (stream == nullptr || stream == entry) {
@@ -277,7 +277,7 @@
 }
 
 void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
-  webrtc::MutexLock lock(&GetLoggingLock());
+  MutexLock lock(&GetLoggingLock());
   stream->min_severity_ = min_sev;
   stream->next_ = streams_;
   streams_ = stream;
@@ -286,7 +286,7 @@
 }
 
 void LogMessage::RemoveLogToStream(LogSink* stream) {
-  webrtc::MutexLock lock(&GetLoggingLock());
+  MutexLock lock(&GetLoggingLock());
   for (LogSink** entry = &streams_; *entry != nullptr;
        entry = &(*entry)->next_) {
     if (*entry == stream) {
@@ -303,7 +303,7 @@
   LoggingSeverity debug_level = GetLogToDebug();
 
   std::vector<std::string> tokens;
-  tokenize(params, ' ', &tokens);
+  rtc::tokenize(params, ' ', &tokens);
 
   for (const std::string& token : tokens) {
     if (token.empty())
@@ -542,10 +542,10 @@
 }
 
 }  // namespace webrtc_logging_impl
-}  // namespace rtc
+}  // namespace webrtc
 #endif
 
-namespace rtc {
+namespace webrtc {
 // Default implementation, override is recomended.
 void LogSink::OnLogMessage(const LogLineRef& log_line) {
 #if defined(WEBRTC_ANDROID)
@@ -583,4 +583,4 @@
 void LogSink::OnLogMessage(absl::string_view msg) {
   OnLogMessage(std::string(msg));
 }
-}  // namespace rtc
+}  // namespace webrtc
diff --git a/rtc_base/logging.h b/rtc_base/logging.h
index 9a024f6..2fa7b2e 100644
--- a/rtc_base/logging.h
+++ b/rtc_base/logging.h
@@ -79,7 +79,7 @@
 #define RTC_LOG_ENABLED() 1
 #endif
 
-namespace rtc {
+namespace webrtc {
 
 //////////////////////////////////////////////////////////////////////
 // The meanings of the levels are:
@@ -120,8 +120,8 @@
   absl::string_view message() const { return message_; }
   absl::string_view filename() const { return filename_; }
   int line() const { return line_; }
-  std::optional<PlatformThreadId> thread_id() const { return thread_id_; }
-  webrtc::Timestamp timestamp() const { return timestamp_; }
+  std::optional<rtc::PlatformThreadId> thread_id() const { return thread_id_; }
+  Timestamp timestamp() const { return timestamp_; }
   absl::string_view tag() const { return tag_; }
   LoggingSeverity severity() const { return severity_; }
 
@@ -136,18 +136,18 @@
   void set_message(std::string message) { message_ = std::move(message); }
   void set_filename(absl::string_view filename) { filename_ = filename; }
   void set_line(int line) { line_ = line; }
-  void set_thread_id(std::optional<PlatformThreadId> thread_id) {
+  void set_thread_id(std::optional<rtc::PlatformThreadId> thread_id) {
     thread_id_ = thread_id;
   }
-  void set_timestamp(webrtc::Timestamp timestamp) { timestamp_ = timestamp; }
+  void set_timestamp(Timestamp timestamp) { timestamp_ = timestamp; }
   void set_tag(absl::string_view tag) { tag_ = tag; }
   void set_severity(LoggingSeverity severity) { severity_ = severity; }
 
   std::string message_;
   absl::string_view filename_;
   int line_ = 0;
-  std::optional<PlatformThreadId> thread_id_;
-  webrtc::Timestamp timestamp_ = webrtc::Timestamp::MinusInfinity();
+  std::optional<rtc::PlatformThreadId> thread_id_;
+  Timestamp timestamp_ = Timestamp::MinusInfinity();
   // The default Android debug output tag.
   absl::string_view tag_ = "libjingle";
   // The severity level of this message
@@ -175,7 +175,7 @@
   virtual void OnLogMessage(const LogLineRef& line);
 
  private:
-  friend class ::rtc::LogMessage;
+  friend class LogMessage;
 #if RTC_LOG_ENABLED()
   // Members for LogMessage class to keep linked list of the registered sinks.
   LogSink* next_ = nullptr;
@@ -467,7 +467,7 @@
   LogMessage& operator=(const LogMessage&) = delete;
 
   void AddTag(const char* tag);
-  webrtc::StringBuilder& stream();
+  StringBuilder& stream();
   // Returns the time at which this function was called for the first time.
   // The time will be used as the logging start time.
   // If this is not called externally, the LogMessage ctor also calls it, in
@@ -533,7 +533,7 @@
   ~LogMessage() = default;
 
   inline void AddTag(const char* tag) {}
-  inline rtc::StringBuilder& stream() { return print_stream_; }
+  inline StringBuilder& stream() { return print_stream_; }
   inline static int64_t LogStartTime() { return 0; }
   inline static uint32_t WallClockStartTime() { return 0; }
   inline static void LogThreads(bool on = true) {}
@@ -609,29 +609,30 @@
 #endif  // RTC_LOG_ENABLED()
 
   // The stringbuilder that buffers the formatted message before output
-  webrtc::StringBuilder print_stream_;
+  StringBuilder print_stream_;
 };
 
 //////////////////////////////////////////////////////////////////////
 // Logging Helpers
 //////////////////////////////////////////////////////////////////////
 
-#define RTC_LOG_FILE_LINE(sev, file, line)        \
-  ::rtc::webrtc_logging_impl::LogCall() &         \
-      ::rtc::webrtc_logging_impl::LogStreamer<>() \
-          << ::rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
+#define RTC_LOG_FILE_LINE(sev, file, line)           \
+  ::webrtc::webrtc_logging_impl::LogCall() &         \
+      ::webrtc::webrtc_logging_impl::LogStreamer<>() \
+          << ::webrtc::webrtc_logging_impl::LogMetadata(file, line, sev)
 
-#define RTC_LOG(sev)                          \
-  !::rtc::LogMessage::IsNoop<::rtc::sev>() && \
-      RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
+#define RTC_LOG(sev)                                \
+  !::webrtc::LogMessage::IsNoop<::webrtc::sev>() && \
+      RTC_LOG_FILE_LINE(::webrtc::sev, __FILE__, __LINE__)
 
-#define RTC_LOG_IF(sev, condition)                           \
-  !::rtc::LogMessage::IsNoop<::rtc::sev>() && (condition) && \
-      RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
+#define RTC_LOG_IF(sev, condition)                                 \
+  !::webrtc::LogMessage::IsNoop<::webrtc::sev>() && (condition) && \
+      RTC_LOG_FILE_LINE(::webrtc::sev, __FILE__, __LINE__)
 
 // The _V version is for when a variable is passed in.
-#define RTC_LOG_V(sev) \
-  !::rtc::LogMessage::IsNoop(sev) && RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
+#define RTC_LOG_V(sev)                  \
+  !::webrtc::LogMessage::IsNoop(sev) && \
+      RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
 
 // The _F version prefixes the message with the current function name.
 #if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
@@ -647,19 +648,19 @@
 #define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
 #endif
 
-#define RTC_LOG_CHECK_LEVEL(sev) ::rtc::LogCheckLevel(::rtc::sev)
-#define RTC_LOG_CHECK_LEVEL_V(sev) ::rtc::LogCheckLevel(sev)
+#define RTC_LOG_CHECK_LEVEL(sev) ::webrtc::LogCheckLevel(::webrtc::sev)
+#define RTC_LOG_CHECK_LEVEL_V(sev) ::webrtc::LogCheckLevel(sev)
 
 inline bool LogCheckLevel(LoggingSeverity sev) {
   return (LogMessage::GetMinLogSeverity() <= sev);
 }
 
-#define RTC_LOG_E(sev, ctx, err)                                 \
-  !::rtc::LogMessage::IsNoop<::rtc::sev>() &&                    \
-      ::rtc::webrtc_logging_impl::LogCall() &                    \
-          ::rtc::webrtc_logging_impl::LogStreamer<>()            \
-              << ::rtc::webrtc_logging_impl::LogMetadataErr {    \
-    {__FILE__, __LINE__, ::rtc::sev}, ::rtc::ERRCTX_##ctx, (err) \
+#define RTC_LOG_E(sev, ctx, err)                                       \
+  !::webrtc::LogMessage::IsNoop<::webrtc::sev>() &&                    \
+      ::webrtc::webrtc_logging_impl::LogCall() &                       \
+          ::webrtc::webrtc_logging_impl::LogStreamer<>()               \
+              << ::webrtc::webrtc_logging_impl::LogMetadataErr {       \
+    {__FILE__, __LINE__, ::webrtc::sev}, ::webrtc::ERRCTX_##ctx, (err) \
   }
 
 #define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
@@ -692,12 +693,12 @@
 }
 }  // namespace webrtc_logging_impl
 
-#define RTC_LOG_TAG(sev, tag)                                 \
-  !::rtc::LogMessage::IsNoop(sev) &&                          \
-      ::rtc::webrtc_logging_impl::LogCall() &                 \
-          ::rtc::webrtc_logging_impl::LogStreamer<>()         \
-              << ::rtc::webrtc_logging_impl::LogMetadataTag { \
-    sev, ::rtc::webrtc_logging_impl::AdaptString(tag)         \
+#define RTC_LOG_TAG(sev, tag)                                    \
+  !::webrtc::LogMessage::IsNoop(sev) &&                          \
+      ::webrtc::webrtc_logging_impl::LogCall() &                 \
+          ::webrtc::webrtc_logging_impl::LogStreamer<>()         \
+              << ::webrtc::webrtc_logging_impl::LogMetadataTag { \
+    sev, ::webrtc::webrtc_logging_impl::AdaptString(tag)         \
   }
 
 #else
@@ -716,10 +717,10 @@
 #define RTC_DLOG_F(sev) RTC_LOG_F(sev)
 #define RTC_DLOG_IF_F(sev, condition) RTC_LOG_IF_F(sev, condition)
 #else
-#define RTC_DLOG_EAT_STREAM_PARAMS()                \
-  while (false)                                     \
-  ::rtc::webrtc_logging_impl::LogMessageVoidify() & \
-      (::rtc::webrtc_logging_impl::LogStreamer<>())
+#define RTC_DLOG_EAT_STREAM_PARAMS()                   \
+  while (false)                                        \
+  ::webrtc::webrtc_logging_impl::LogMessageVoidify() & \
+      (::webrtc::webrtc_logging_impl::LogStreamer<>())
 #define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
 #define RTC_DLOG_IF(sev, condition) RTC_DLOG_EAT_STREAM_PARAMS()
 #define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
@@ -727,6 +728,20 @@
 #define RTC_DLOG_IF_F(sev, condition) RTC_DLOG_EAT_STREAM_PARAMS()
 #endif
 
+}  // namespace webrtc
+
+// Re-export symbols from the webrtc namespace for backwards compatibility.
+// TODO(bugs.webrtc.org/4222596): Remove once all references are updated.
+namespace rtc {
+using ::webrtc::LoggingSeverity;
+using ::webrtc::LogLineRef;
+using ::webrtc::LogMessage;
+using ::webrtc::LogSink;
+using ::webrtc::LS_ERROR;
+using ::webrtc::LS_INFO;
+using ::webrtc::LS_NONE;
+using ::webrtc::LS_VERBOSE;
+using ::webrtc::LS_WARNING;
 }  // namespace rtc
 
 #endif  // RTC_BASE_LOGGING_H_
diff --git a/rtc_base/logging_unittest.cc b/rtc_base/logging_unittest.cc
index d50ade0..2ad31be 100644
--- a/rtc_base/logging_unittest.cc
+++ b/rtc_base/logging_unittest.cc
@@ -22,7 +22,7 @@
 #include "test/gmock.h"
 #include "test/gtest.h"
 
-namespace rtc {
+namespace webrtc {
 
 namespace {
 
@@ -221,12 +221,12 @@
 class LogThread {
  public:
   void Start() {
-    thread_ = PlatformThread::SpawnJoinable(
+    thread_ = rtc::PlatformThread::SpawnJoinable(
         [] { RTC_LOG(LS_VERBOSE) << "RTC_LOG"; }, "LogThread");
   }
 
  private:
-  PlatformThread thread_;
+  rtc::PlatformThread thread_;
 };
 
 // Ensure we don't crash when adding/removing streams while threads are going.
@@ -315,7 +315,9 @@
   LogMessage::AddLogToStream(&stream, LS_VERBOSE);
 
   const std::string message(80, 'X');
-  { LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_VERBOSE); }
+  {
+    LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_VERBOSE);
+  }
 
   // We now know how many bytes the logging framework will tag onto every msg.
   const size_t logging_overhead = str.size();
@@ -324,16 +326,16 @@
   str.reserve(120000);
   static const int kRepetitions = 1000;
 
-  int64_t start = TimeMillis(), finish;
+  int64_t start = rtc::TimeMillis(), finish;
   for (int i = 0; i < kRepetitions; ++i) {
     LogMessageForTesting(__FILE__, __LINE__, LS_VERBOSE).stream() << message;
   }
-  finish = TimeMillis();
+  finish = rtc::TimeMillis();
 
   LogMessage::RemoveLogToStream(&stream);
 
   EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
-  RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start)
+  RTC_LOG(LS_INFO) << "Total log time: " << rtc::TimeDiff(finish, start)
                    << " ms "
                       " total bytes logged: "
                    << str.size();
@@ -412,5 +414,5 @@
   LogMessage::RemoveLogToStream(&stream);
 }
 
-}  // namespace rtc
+}  // namespace webrtc
 #endif  // RTC_LOG_ENABLED()
diff --git a/rtc_base/task_queue_stdlib_unittest.cc b/rtc_base/task_queue_stdlib_unittest.cc
index ab0d7fd..68d2aca 100644
--- a/rtc_base/task_queue_stdlib_unittest.cc
+++ b/rtc_base/task_queue_stdlib_unittest.cc
@@ -33,7 +33,7 @@
                          TaskQueueTest,
                          ::testing::Values(CreateTaskQueueFactory));
 
-class StringPtrLogSink : public rtc::LogSink {
+class StringPtrLogSink : public LogSink {
  public:
   explicit StringPtrLogSink(std::string* log_data) : log_data_(log_data) {}
 
@@ -50,14 +50,14 @@
 TEST(TaskQueueStdlib, AvoidsSpammingLogOnInactivity) {
   std::string log_output;
   StringPtrLogSink stream(&log_output);
-  rtc::LogMessage::AddLogToStream(&stream, rtc::LS_VERBOSE);
+  LogMessage::AddLogToStream(&stream, rtc::LS_VERBOSE);
   auto task_queue = CreateTaskQueueStdlibFactory()->CreateTaskQueue(
       "test", TaskQueueFactory::Priority::NORMAL);
   auto wait_duration = rtc::Event::kDefaultWarnDuration + TimeDelta::Seconds(1);
   SleepMs(wait_duration.ms());
   EXPECT_EQ(log_output.length(), 0u);
   task_queue = nullptr;
-  rtc::LogMessage::RemoveLogToStream(&stream);
+  LogMessage::RemoveLogToStream(&stream);
 }
 
 }  // namespace
diff --git a/test/test_main_lib.cc b/test/test_main_lib.cc
index 1922df0..d0ac182 100644
--- a/test/test_main_lib.cc
+++ b/test/test_main_lib.cc
@@ -134,20 +134,20 @@
 
     // Default to LS_INFO, even for release builds to provide better test
     // logging.
-    if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
-      rtc::LogMessage::LogToDebug(rtc::LS_INFO);
+    if (LogMessage::GetLogToDebug() > LS_INFO)
+      LogMessage::LogToDebug(LS_INFO);
 
     if (absl::GetFlag(FLAGS_verbose))
-      rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
+      LogMessage::LogToDebug(LS_VERBOSE);
 
-    rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
-                                    absl::GetFlag(FLAGS_verbose));
+    LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
+                               absl::GetFlag(FLAGS_verbose));
 
     // InitFieldTrialsFromString stores the char*, so the char array must
     // outlive the application.
     field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
-    webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
-    webrtc::metrics::Enable();
+    field_trial::InitFieldTrialsFromString(field_trials_.c_str());
+    metrics::Enable();
 
 #if defined(WEBRTC_WIN)
     winsock_init_ = std::make_unique<rtc::WinsockInitializer>();