xref: /aosp_15_r20/external/webrtc/test/logging/memory_log_writer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "absl/strings/string_view.h"
15 #include "rtc_base/checks.h"
16 #include "rtc_base/logging.h"
17 
18 namespace webrtc {
19 namespace {
20 class MemoryLogWriter final : public RtcEventLogOutput {
21  public:
MemoryLogWriter(std::map<std::string,std::string> * target,absl::string_view filename)22   explicit MemoryLogWriter(std::map<std::string, std::string>* target,
23                            absl::string_view filename)
24       : target_(target), filename_(filename) {}
~MemoryLogWriter()25   ~MemoryLogWriter() final { target_->insert({filename_, std::move(buffer_)}); }
IsActive() const26   bool IsActive() const override { return true; }
Write(absl::string_view value)27   bool Write(absl::string_view value) override {
28     buffer_.append(value.data(), value.size());
29     return true;
30   }
Flush()31   void Flush() override {}
32 
33  private:
34   std::map<std::string, std::string>* const target_;
35   const std::string filename_;
36   std::string buffer_;
37 };
38 
39 class MemoryLogWriterFactory final : public LogWriterFactoryInterface {
40  public:
MemoryLogWriterFactory(std::map<std::string,std::string> * target)41   explicit MemoryLogWriterFactory(std::map<std::string, std::string>* target)
42       : target_(target) {}
~MemoryLogWriterFactory()43   ~MemoryLogWriterFactory() override {}
Create(absl::string_view filename)44   std::unique_ptr<RtcEventLogOutput> Create(
45       absl::string_view filename) override {
46     return std::make_unique<MemoryLogWriter>(target_, filename);
47   }
48 
49  private:
50   std::map<std::string, std::string>* const target_;
51 };
52 
53 }  // namespace
54 
MemoryLogStorage()55 MemoryLogStorage::MemoryLogStorage() {}
56 
~MemoryLogStorage()57 MemoryLogStorage::~MemoryLogStorage() {}
58 
CreateFactory()59 std::unique_ptr<LogWriterFactoryInterface> MemoryLogStorage::CreateFactory() {
60   return std::make_unique<MemoryLogWriterFactory>(&logs_);
61 }
62 
63 // namespace webrtc_impl
64 }  // namespace webrtc
65