1 /*
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
12 #include <memory>
13
14 #include "rtc_base/checks.h"
15 #include "rtc_base/logging.h"
16
17 namespace webrtc {
18 namespace {
19 class MemoryLogWriter final : public RtcEventLogOutput {
20 public:
MemoryLogWriter(std::map<std::string,std::string> * target,std::string filename)21 explicit MemoryLogWriter(std::map<std::string, std::string>* target,
22 std::string filename)
23 : target_(target), filename_(filename) {}
~MemoryLogWriter()24 ~MemoryLogWriter() final {
25 size_t size;
26 buffer_.GetSize(&size);
27 target_->insert({filename_, std::string(buffer_.GetBuffer(), size)});
28 }
IsActive() const29 bool IsActive() const override { return true; }
Write(const std::string & value)30 bool Write(const std::string& value) override {
31 size_t written;
32 int error;
33 return buffer_.WriteAll(value.data(), value.size(), &written, &error) ==
34 rtc::SR_SUCCESS;
35 RTC_DCHECK_EQ(value.size(), written);
36 }
Flush()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
45 class MemoryLogWriterFactory : public LogWriterFactoryInterface {
46 public:
MemoryLogWriterFactory(std::map<std::string,std::string> * target)47 explicit MemoryLogWriterFactory(std::map<std::string, std::string>* target)
48 : target_(target) {}
~MemoryLogWriterFactory()49 ~MemoryLogWriterFactory() final {}
Create(std::string filename)50 std::unique_ptr<RtcEventLogOutput> Create(std::string filename) override {
51 return std::make_unique<MemoryLogWriter>(target_, filename);
52 }
53
54 private:
55 std::map<std::string, std::string>* const target_;
56 };
57
58 } // namespace
59
MemoryLogStorage()60 MemoryLogStorage::MemoryLogStorage() {}
61
~MemoryLogStorage()62 MemoryLogStorage::~MemoryLogStorage() {}
63
CreateFactory()64 std::unique_ptr<LogWriterFactoryInterface> MemoryLogStorage::CreateFactory() {
65 return std::make_unique<MemoryLogWriterFactory>(&logs_);
66 }
67
68 // namespace webrtc_impl
69 } // namespace webrtc
70