Danny Wang | 112c203 | 2024-01-25 18:42:17 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_PERIPHERALS_LOGGING_LOGGING_H_ |
| 6 | #define COMPONENTS_PERIPHERALS_LOGGING_LOGGING_H_ |
| 7 | |
| 8 | #include <sstream> |
| 9 | |
| 10 | #include "base/logging.h" |
| 11 | #include "components/peripherals/logging/log_buffer.h" |
| 12 | |
| 13 | // Use the PR_LOG() macro for all logging related to Cross Device Features so |
| 14 | // the debug page can reflect all logs related to this feature in the internal |
| 15 | // debug WebUI (chrome://nearby-internals). |
| 16 | #define PR_LOG(severity, feature) \ |
| 17 | PeripheralsScopedLogMessage(__FILE__, __LINE__, logging::LOGGING_##severity, \ |
| 18 | feature) \ |
| 19 | .stream() |
| 20 | |
| 21 | // An intermediate object used by the PR_LOG macro, wrapping a |
| 22 | // logging::LogMessage instance. When this object is destroyed, the message will |
| 23 | // be logged with the standard logging system and also added to Peripherals |
| 24 | // specific log buffer. |
| 25 | class PeripheralsScopedLogMessage { |
| 26 | public: |
| 27 | PeripheralsScopedLogMessage(const char* file, |
| 28 | int line, |
| 29 | logging::LogSeverity severity, |
| 30 | Feature feature); |
| 31 | PeripheralsScopedLogMessage(const PeripheralsScopedLogMessage&) = delete; |
| 32 | PeripheralsScopedLogMessage& operator=(const PeripheralsScopedLogMessage&) = |
| 33 | delete; |
| 34 | ~PeripheralsScopedLogMessage(); |
| 35 | |
| 36 | std::ostream& stream() { return stream_; } |
| 37 | |
| 38 | private: |
| 39 | const char* file_; |
| 40 | Feature feature_; |
| 41 | int line_; |
| 42 | logging::LogSeverity severity_; |
| 43 | std::ostringstream stream_; |
| 44 | }; |
| 45 | |
| 46 | #endif // COMPONENTS_PERIPHERALS_LOGGING_LOGGING_H_ |