blob: ee9befeb7c65322d23baffcca6f2d2105032d48c [file] [log] [blame]
Sebastian Jansson52de8b02019-01-16 17:25:44 +01001/*
2 * Copyright 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10#include "test/logging/memory_log_writer.h"
11
Mirko Bonadei317a1f02019-09-17 17:06:18 +020012#include <memory>
13
Sebastian Jansson52de8b02019-01-16 17:25:44 +010014#include "rtc_base/checks.h"
15#include "rtc_base/logging.h"
Mirko Bonadei317a1f02019-09-17 17:06:18 +020016
Sebastian Jansson52de8b02019-01-16 17:25:44 +010017namespace webrtc {
18namespace {
19class MemoryLogWriter final : public RtcEventLogOutput {
20 public:
21 explicit MemoryLogWriter(std::map<std::string, std::string>* target,
22 std::string filename)
23 : target_(target), filename_(filename) {}
24 ~MemoryLogWriter() final {
25 size_t size;
26 buffer_.GetSize(&size);
27 target_->insert({filename_, std::string(buffer_.GetBuffer(), size)});
28 }
29 bool IsActive() const override { return true; }
30 bool Write(const std::string& value) override {
31 size_t written;
32 int error;
33 return buffer_.Write(value.data(), value.size(), &written, &error) ==
34 rtc::SR_SUCCESS;
35 }
36 void Flush() override {}
37
38 private:
39 std::map<std::string, std::string>* const target_;
40 const std::string filename_;
41 rtc::MemoryStream buffer_;
42};
43
44class MemoryLogWriterFactory : public LogWriterFactoryInterface {
45 public:
46 explicit MemoryLogWriterFactory(std::map<std::string, std::string>* target)
47 : target_(target) {}
48 ~MemoryLogWriterFactory() final {}
49 std::unique_ptr<RtcEventLogOutput> Create(std::string filename) override {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020050 return std::make_unique<MemoryLogWriter>(target_, filename);
Sebastian Jansson52de8b02019-01-16 17:25:44 +010051 }
52
53 private:
54 std::map<std::string, std::string>* const target_;
55};
56
57} // namespace
58
59MemoryLogStorage::MemoryLogStorage() {}
60
61MemoryLogStorage::~MemoryLogStorage() {}
62
63std::unique_ptr<LogWriterFactoryInterface> MemoryLogStorage::CreateFactory() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020064 return std::make_unique<MemoryLogWriterFactory>(&logs_);
Sebastian Jansson52de8b02019-01-16 17:25:44 +010065}
66
67// namespace webrtc_impl
68} // namespace webrtc