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; |
| 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 | |
| 44 | class 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 Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 50 | return std::make_unique<MemoryLogWriter>(target_, filename); |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | private: |
| 54 | std::map<std::string, std::string>* const target_; |
| 55 | }; |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | MemoryLogStorage::MemoryLogStorage() {} |
| 60 | |
| 61 | MemoryLogStorage::~MemoryLogStorage() {} |
| 62 | |
| 63 | std::unique_ptr<LogWriterFactoryInterface> MemoryLogStorage::CreateFactory() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 64 | return std::make_unique<MemoryLogWriterFactory>(&logs_); |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // namespace webrtc_impl |
| 68 | } // namespace webrtc |