blob: 150f17344d199771a4cab88b6d10e7fa2bc5d210 [file] [log] [blame]
Sebastian Jansson52de8b02019-01-16 17:25:44 +01001/*
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 Bonadei317a1f02019-09-17 17:06:18 +020012#include <memory>
13
Sebastian Jansson52de8b02019-01-16 17:25:44 +010014#include "rtc_base/checks.h"
15#include "rtc_base/logging.h"
Steve Antonf3802842019-01-24 19:07:40 -080016#include "test/testsupport/file_utils.h"
Sebastian Jansson52de8b02019-01-16 17:25:44 +010017
18namespace webrtc {
19namespace webrtc_impl {
20
21FileLogWriter::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
27FileLogWriter::~FileLogWriter() {
28 std::fclose(out_);
29}
30
31bool FileLogWriter::IsActive() const {
32 return true;
33}
34
35bool 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
43void FileLogWriter::Flush() {
44 RTC_CHECK_EQ(fflush(out_), 0) << "fflush failed unexpectedly: " << errno;
45}
46
47} // namespace webrtc_impl
48
49FileLogWriterFactory::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
57FileLogWriterFactory::~FileLogWriterFactory() {}
58
59std::unique_ptr<RtcEventLogOutput> FileLogWriterFactory::Create(
60 std::string filename) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020061 return std::make_unique<webrtc_impl::FileLogWriter>(base_path_ + filename);
Sebastian Jansson52de8b02019-01-16 17:25:44 +010062}
63} // namespace webrtc