logger_impl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2020 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 COMPONENTS_MEDIA_ROUTER_BROWSER_LOGGER_IMPL_H_
  5. #define COMPONENTS_MEDIA_ROUTER_BROWSER_LOGGER_IMPL_H_
  6. #include "base/containers/circular_deque.h"
  7. #include "base/gtest_prod_util.h"
  8. #include "base/strings/string_piece_forward.h"
  9. #include "base/time/time.h"
  10. #include "components/media_router/common/mojom/logger.mojom.h"
  11. #include "mojo/public/cpp/bindings/pending_receiver.h"
  12. #include "mojo/public/cpp/bindings/receiver_set.h"
  13. #include "mojo/public/cpp/bindings/remote.h"
  14. namespace base {
  15. class Value;
  16. } // namespace base
  17. namespace media_router {
  18. class LoggerImpl : mojom::Logger {
  19. public:
  20. enum class Severity { kInfo, kWarning, kError };
  21. LoggerImpl();
  22. ~LoggerImpl() override;
  23. LoggerImpl(const LoggerImpl&) = delete;
  24. LoggerImpl& operator=(const LoggerImpl&) = delete;
  25. // mojom::Logger overrides:
  26. void LogInfo(mojom::LogCategory category,
  27. const std::string& component,
  28. const std::string& message,
  29. const std::string& sink_id,
  30. const std::string& media_source,
  31. const std::string& session_id) override;
  32. void LogWarning(mojom::LogCategory category,
  33. const std::string& component,
  34. const std::string& message,
  35. const std::string& sink_id,
  36. const std::string& media_source,
  37. const std::string& session_id) override;
  38. void LogError(mojom::LogCategory category,
  39. const std::string& component,
  40. const std::string& message,
  41. const std::string& sink_id,
  42. const std::string& media_source,
  43. const std::string& session_id) override;
  44. void BindReceiver(mojo::PendingReceiver<mojom::Logger> receiver) override;
  45. std::string GetLogsAsJson() const;
  46. base::Value GetLogsAsValue() const;
  47. private:
  48. FRIEND_TEST_ALL_PREFIXES(LoggerImplTest, RecordAndGetLogs);
  49. struct Entry {
  50. public:
  51. Entry(Severity severity,
  52. mojom::LogCategory category,
  53. base::Time time,
  54. base::StringPiece component,
  55. base::StringPiece message,
  56. base::StringPiece sink_id,
  57. std::string media_source,
  58. base::StringPiece session_id);
  59. Entry(Entry&& other);
  60. Entry(const Entry&) = delete;
  61. ~Entry();
  62. Severity severity;
  63. mojom::LogCategory category;
  64. base::Time time;
  65. // This is usually the name of the class that is emitting the log.
  66. std::string component;
  67. std::string message;
  68. // May be empty if the entry is not associated with a sink.
  69. std::string sink_id;
  70. // May be empty if the entry is not associated with a media source.
  71. std::string media_source;
  72. // May be empty if the entry is not associated with a session.
  73. std::string session_id;
  74. };
  75. // Called by tests that want to specify |time|.
  76. void Log(Severity severity,
  77. mojom::LogCategory category,
  78. base::Time time,
  79. const std::string& component,
  80. const std::string& message,
  81. const std::string& sink_id,
  82. const std::string& media_source,
  83. const std::string& session_id);
  84. static base::Value AsValue(const Entry& entry);
  85. mojo::ReceiverSet<mojom::Logger> receivers_;
  86. base::circular_deque<Entry> entries_;
  87. size_t const capacity_;
  88. };
  89. } // namespace media_router
  90. #endif // COMPONENTS_MEDIA_ROUTER_BROWSER_LOGGER_IMPL_H_