Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 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 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 12 | #include <memory> |
| 13 | |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/logging.h" |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 16 | |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 17 | namespace webrtc { |
| 18 | namespace { |
| 19 | class 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 Terelius | 1a61739 | 2019-10-24 18:19:12 +0200 | [diff] [blame] | 33 | return buffer_.WriteAll(value.data(), value.size(), &written, &error) == |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 34 | rtc::SR_SUCCESS; |
Björn Terelius | 1a61739 | 2019-10-24 18:19:12 +0200 | [diff] [blame] | 35 | RTC_DCHECK_EQ(value.size(), written); |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 36 | } |
| 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: |
| 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 Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 51 | return std::make_unique<MemoryLogWriter>(target_, filename); |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private: |
| 55 | std::map<std::string, std::string>* const target_; |
| 56 | }; |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | MemoryLogStorage::MemoryLogStorage() {} |
| 61 | |
| 62 | MemoryLogStorage::~MemoryLogStorage() {} |
| 63 | |
| 64 | std::unique_ptr<LogWriterFactoryInterface> MemoryLogStorage::CreateFactory() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 65 | return std::make_unique<MemoryLogWriterFactory>(&logs_); |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // namespace webrtc_impl |
| 69 | } // namespace webrtc |