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