logger_impl.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "components/media_router/browser/logger_impl.h"
  5. #include "base/i18n/time_formatting.h"
  6. #include "base/json/json_string_value_serializer.h"
  7. #include "base/strings/string_piece.h"
  8. #include "base/values.h"
  9. #include "components/media_router/common/media_source.h"
  10. #include "components/media_router/common/mojom/logger.mojom-shared.h"
  11. #include "url/gurl.h"
  12. namespace media_router {
  13. namespace {
  14. constexpr size_t kEntriesCapacity = 1000;
  15. constexpr size_t kComponentMaxLength = 64;
  16. constexpr size_t kMessageMaxLength = 1024;
  17. constexpr size_t kSourceMaxLength = 64;
  18. const char* AsString(LoggerImpl::Severity severity) {
  19. switch (severity) {
  20. case LoggerImpl::Severity::kInfo:
  21. return "Info";
  22. case LoggerImpl::Severity::kWarning:
  23. return "Warning";
  24. case LoggerImpl::Severity::kError:
  25. return "Error";
  26. }
  27. }
  28. const char* AsString(mojom::LogCategory category) {
  29. switch (category) {
  30. case mojom::LogCategory::kDiscovery:
  31. return "Discovery";
  32. case mojom::LogCategory::kRoute:
  33. return "Route";
  34. case mojom::LogCategory::kMirroring:
  35. return "Mirroring";
  36. case mojom::LogCategory::kUi:
  37. return "UI";
  38. }
  39. }
  40. base::StringPiece TruncateComponent(base::StringPiece component) {
  41. return component.substr(0, kComponentMaxLength);
  42. }
  43. base::StringPiece TruncateMessage(base::StringPiece message) {
  44. return message.substr(0, kMessageMaxLength);
  45. }
  46. // Gets the last four characters of an ID string.
  47. base::StringPiece TruncateId(base::StringPiece id) {
  48. if (id.size() <= 4)
  49. return id;
  50. return id.substr(id.size() - 4);
  51. }
  52. } // namespace
  53. LoggerImpl::LoggerImpl() : capacity_(kEntriesCapacity) {}
  54. LoggerImpl::~LoggerImpl() = default;
  55. void LoggerImpl::LogInfo(mojom::LogCategory category,
  56. const std::string& component,
  57. const std::string& message,
  58. const std::string& sink_id,
  59. const std::string& media_source,
  60. const std::string& session_id) {
  61. Log(Severity::kInfo, category, base::Time::Now(), component, message, sink_id,
  62. media_source, session_id);
  63. }
  64. void LoggerImpl::LogWarning(mojom::LogCategory category,
  65. const std::string& component,
  66. const std::string& message,
  67. const std::string& sink_id,
  68. const std::string& media_source,
  69. const std::string& session_id) {
  70. Log(Severity::kWarning, category, base::Time::Now(), component, message,
  71. sink_id, media_source, session_id);
  72. }
  73. void LoggerImpl::LogError(mojom::LogCategory category,
  74. const std::string& component,
  75. const std::string& message,
  76. const std::string& sink_id,
  77. const std::string& media_source,
  78. const std::string& session_id) {
  79. Log(Severity::kError, category, base::Time::Now(), component, message,
  80. sink_id, media_source, session_id);
  81. }
  82. void LoggerImpl::BindReceiver(mojo::PendingReceiver<mojom::Logger> receiver) {
  83. receivers_.Add(this, std::move(receiver));
  84. }
  85. std::string LoggerImpl::GetLogsAsJson() const {
  86. std::string json;
  87. JSONStringValueSerializer serializer(&json);
  88. serializer.set_pretty_print(true);
  89. if (!serializer.Serialize(GetLogsAsValue())) {
  90. DVLOG(1) << "Failed to serialize log to JSON.";
  91. return "";
  92. }
  93. return json;
  94. }
  95. base::Value LoggerImpl::GetLogsAsValue() const {
  96. base::Value entries_val(base::Value::Type::LIST);
  97. for (const auto& entry : entries_)
  98. entries_val.Append(AsValue(entry));
  99. return entries_val;
  100. }
  101. LoggerImpl::Entry::Entry(Severity severity,
  102. mojom::LogCategory category,
  103. base::Time time,
  104. base::StringPiece component,
  105. base::StringPiece message,
  106. base::StringPiece sink_id,
  107. std::string media_source,
  108. base::StringPiece session_id)
  109. : severity(severity),
  110. category(category),
  111. time(time),
  112. component(component),
  113. message(message),
  114. sink_id(sink_id),
  115. media_source(std::move(media_source)),
  116. session_id(session_id) {}
  117. LoggerImpl::Entry::Entry(Entry&& other)
  118. : severity(other.severity),
  119. category(other.category),
  120. time(other.time),
  121. component(std::move(other.component)),
  122. message(std::move(other.message)),
  123. sink_id(std::move(other.sink_id)),
  124. media_source(std::move(other.media_source)),
  125. session_id(std::move(other.session_id)) {}
  126. LoggerImpl::Entry::~Entry() = default;
  127. void LoggerImpl::Log(Severity severity,
  128. mojom::LogCategory category,
  129. base::Time time,
  130. const std::string& component,
  131. const std::string& message,
  132. const std::string& sink_id,
  133. const std::string& media_source,
  134. const std::string& session_id) {
  135. entries_.emplace_back(
  136. severity, category, time, TruncateComponent(component),
  137. TruncateMessage(message), TruncateId(sink_id),
  138. MediaSource(media_source).TruncateForLogging(kSourceMaxLength),
  139. TruncateId(session_id));
  140. if (entries_.size() > capacity_)
  141. entries_.pop_front();
  142. }
  143. // static
  144. base::Value LoggerImpl::AsValue(const LoggerImpl::Entry& entry) {
  145. base::Value entry_val(base::Value::Type::DICTIONARY);
  146. entry_val.SetKey("severity", base::Value(AsString(entry.severity)));
  147. entry_val.SetKey("category", base::Value(AsString(entry.category)));
  148. entry_val.SetKey(
  149. "time",
  150. base::Value(base::TimeFormatTimeOfDayWithMilliseconds(entry.time)));
  151. entry_val.SetKey("component", base::Value(entry.component));
  152. entry_val.SetKey("message", base::Value(entry.message));
  153. entry_val.SetKey("sinkId", base::Value(entry.sink_id));
  154. entry_val.SetKey("mediaSource", base::Value(entry.media_source));
  155. entry_val.SetKey("sessionId", base::Value(entry.session_id));
  156. return entry_val;
  157. }
  158. } // namespace media_router