blob: 2eb1cffb4871e24803a5373cd9c6a575db0701e9 [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;
Björn Terelius1a617392019-10-24 18:19:12 +020033 return buffer_.WriteAll(value.data(), value.size(), &written, &error) ==
Sebastian Jansson52de8b02019-01-16 17:25:44 +010034 rtc::SR_SUCCESS;
Björn Terelius1a617392019-10-24 18:19:12 +020035 RTC_DCHECK_EQ(value.size(), written);
Sebastian Jansson52de8b02019-01-16 17:25:44 +010036 }
37 void Flush() override {}
38
39 private:
40 std::map<std::string, std::string>* const target_;
41 const std::string filename_;
42 rtc::MemoryStream buffer_;
43};
44
45class MemoryLogWriterFactory : public LogWriterFactoryInterface {
46 public:
47 explicit MemoryLogWriterFactory(std::map<std::string, std::string>* target)
48 : target_(target) {}
49 ~MemoryLogWriterFactory() final {}
50 std::unique_ptr<RtcEventLogOutput> Create(std::string filename) override {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020051 return std::make_unique<MemoryLogWriter>(target_, filename);
Sebastian Jansson52de8b02019-01-16 17:25:44 +010052 }
53
54 private:
55 std::map<std::string, std::string>* const target_;
56};
57
58} // namespace
59
60MemoryLogStorage::MemoryLogStorage() {}
61
62MemoryLogStorage::~MemoryLogStorage() {}
63
64std::unique_ptr<LogWriterFactoryInterface> MemoryLogStorage::CreateFactory() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020065 return std::make_unique<MemoryLogWriterFactory>(&logs_);
Sebastian Jansson52de8b02019-01-16 17:25:44 +010066}
67
68// namespace webrtc_impl
69} // namespace webrtc