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/file_log_writer.h" |
| 11 | |
| 12 | #include "absl/memory/memory.h" |
| 13 | #include "rtc_base/checks.h" |
| 14 | #include "rtc_base/logging.h" |
Steve Anton | f380284 | 2019-01-24 19:07:40 -0800 | [diff] [blame] | 15 | #include "test/testsupport/file_utils.h" |
Sebastian Jansson | 52de8b0 | 2019-01-16 17:25:44 +0100 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace webrtc_impl { |
| 19 | |
| 20 | FileLogWriter::FileLogWriter(std::string file_path) |
| 21 | : out_(std::fopen(file_path.c_str(), "wb")) { |
| 22 | RTC_CHECK(out_ != nullptr) |
| 23 | << "Failed to open file: '" << file_path << "' for writing."; |
| 24 | } |
| 25 | |
| 26 | FileLogWriter::~FileLogWriter() { |
| 27 | std::fclose(out_); |
| 28 | } |
| 29 | |
| 30 | bool FileLogWriter::IsActive() const { |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | bool FileLogWriter::Write(const std::string& value) { |
| 35 | // We don't expect the write to fail. If it does, we don't want to risk |
| 36 | // silently ignoring it. |
| 37 | RTC_CHECK_EQ(std::fwrite(value.data(), 1, value.size(), out_), value.size()) |
| 38 | << "fwrite failed unexpectedly: " << errno; |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | void FileLogWriter::Flush() { |
| 43 | RTC_CHECK_EQ(fflush(out_), 0) << "fflush failed unexpectedly: " << errno; |
| 44 | } |
| 45 | |
| 46 | } // namespace webrtc_impl |
| 47 | |
| 48 | FileLogWriterFactory::FileLogWriterFactory(std::string base_path) |
| 49 | : base_path_(base_path) { |
| 50 | for (size_t i = 0; i < base_path.size(); ++i) { |
| 51 | if (base_path[i] == '/') |
| 52 | test::CreateDir(base_path.substr(0, i)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | FileLogWriterFactory::~FileLogWriterFactory() {} |
| 57 | |
| 58 | std::unique_ptr<RtcEventLogOutput> FileLogWriterFactory::Create( |
| 59 | std::string filename) { |
| 60 | return absl::make_unique<webrtc_impl::FileLogWriter>(base_path_ + filename); |
| 61 | } |
| 62 | } // namespace webrtc |