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