[go: nahoru, domu]

customization: Create PR_LOG macro in //components/peripherals

The new PR_LOG macro will be used for all peripheral device feature
logging. This CL is created based on the CD_LOG macro used for cross
device feature logging.

Bug: b/241965717 b/216049298
Test: ash_unittests
Change-Id: I91aad6e65706ff6ed32f8128a2f10a330cd65f64
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5189972
Commit-Queue: Danny Wang <wangdanny@google.com>
Reviewed-by: Cait Phillips <caitkp@chromium.org>
Reviewed-by: David Padlipsky <dpad@google.com>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1252228}
diff --git a/components/peripherals/logging/logging.h b/components/peripherals/logging/logging.h
new file mode 100644
index 0000000..b6e03da2
--- /dev/null
+++ b/components/peripherals/logging/logging.h
@@ -0,0 +1,46 @@
+// Copyright 2024 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_PERIPHERALS_LOGGING_LOGGING_H_
+#define COMPONENTS_PERIPHERALS_LOGGING_LOGGING_H_
+
+#include <sstream>
+
+#include "base/logging.h"
+#include "components/peripherals/logging/log_buffer.h"
+
+// Use the PR_LOG() macro for all logging related to Cross Device Features so
+// the debug page can reflect all logs related to this feature in the internal
+// debug WebUI (chrome://nearby-internals).
+#define PR_LOG(severity, feature)                                              \
+  PeripheralsScopedLogMessage(__FILE__, __LINE__, logging::LOGGING_##severity, \
+                              feature)                                         \
+      .stream()
+
+// An intermediate object used by the PR_LOG macro, wrapping a
+// logging::LogMessage instance. When this object is destroyed, the message will
+// be logged with the standard logging system and also added to Peripherals
+// specific log buffer.
+class PeripheralsScopedLogMessage {
+ public:
+  PeripheralsScopedLogMessage(const char* file,
+                              int line,
+                              logging::LogSeverity severity,
+                              Feature feature);
+  PeripheralsScopedLogMessage(const PeripheralsScopedLogMessage&) = delete;
+  PeripheralsScopedLogMessage& operator=(const PeripheralsScopedLogMessage&) =
+      delete;
+  ~PeripheralsScopedLogMessage();
+
+  std::ostream& stream() { return stream_; }
+
+ private:
+  const char* file_;
+  Feature feature_;
+  int line_;
+  logging::LogSeverity severity_;
+  std::ostringstream stream_;
+};
+
+#endif  // COMPONENTS_PERIPHERALS_LOGGING_LOGGING_H_