logging.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_QUICK_PAIR_COMMON_LOGGING_H_
  5. #define ASH_QUICK_PAIR_COMMON_LOGGING_H_
  6. #include <sstream>
  7. #include "base/component_export.h"
  8. #include "base/logging.h"
  9. namespace ash {
  10. namespace quick_pair {
  11. // Use the QP_LOG() macro for all logging related to Quick Pair implementations
  12. // (e.g. Fast Pair), so the system is aware of all logs related to this feature.
  13. // We display these logs in the debug WebUI (chrome://nearby-internals).
  14. //
  15. // QP_LOG() has the same interface as the standard LOG() macro and also creates
  16. // a normal log message of the same severity.
  17. // Examples:
  18. // QP_LOG(INFO) << "Waiting for " << x << " pending requests.";
  19. // QP_LOG(ERROR) << "Request failed: " << error_string;
  20. #define QP_LOG(severity) \
  21. ash::quick_pair::ScopedLogMessage(__FILE__, __LINE__, \
  22. logging::LOG_##severity) \
  23. .stream()
  24. // Disables all logging while in scope. Intended to be called only from test
  25. // code, to clean up test output.
  26. class COMPONENT_EXPORT(QUICK_PAIR_COMMON) ScopedDisableLoggingForTesting {
  27. public:
  28. ScopedDisableLoggingForTesting();
  29. ~ScopedDisableLoggingForTesting();
  30. };
  31. // An intermediate object used by the QP_LOG macro, wrapping a
  32. // logging::LogMessage instance. When this object is destroyed, the message will
  33. // be logged with the standard logging system and also added to Proximity Auth
  34. // specific log buffer. You should use the QP_LOG() macro instead of this class
  35. // directly.
  36. class COMPONENT_EXPORT(QUICK_PAIR_COMMON) ScopedLogMessage {
  37. public:
  38. ScopedLogMessage(const char* file, int line, logging::LogSeverity severity);
  39. ScopedLogMessage(const ScopedLogMessage&) = delete;
  40. ScopedLogMessage& operator=(const ScopedLogMessage&) = delete;
  41. ~ScopedLogMessage();
  42. std::ostream& stream() { return stream_; }
  43. private:
  44. const char* file_;
  45. int line_;
  46. logging::LogSeverity severity_;
  47. std::ostringstream stream_;
  48. };
  49. } // namespace quick_pair
  50. } // namespace ash
  51. #endif // ASH_QUICK_PAIR_COMMON_LOGGING_H_