blob: ced81fee306a759ae6f501735f9b52a7b991121e [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
12#include "absl/memory/memory.h"
13#include "rtc_base/checks.h"
14#include "rtc_base/logging.h"
Steve Antonf3802842019-01-24 19:07:40 -080015#include "test/testsupport/file_utils.h"
Sebastian Jansson52de8b02019-01-16 17:25:44 +010016
17namespace webrtc {
18namespace webrtc_impl {
19
20FileLogWriter::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
26FileLogWriter::~FileLogWriter() {
27 std::fclose(out_);
28}
29
30bool FileLogWriter::IsActive() const {
31 return true;
32}
33
34bool 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
42void FileLogWriter::Flush() {
43 RTC_CHECK_EQ(fflush(out_), 0) << "fflush failed unexpectedly: " << errno;
44}
45
46} // namespace webrtc_impl
47
48FileLogWriterFactory::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
56FileLogWriterFactory::~FileLogWriterFactory() {}
57
58std::unique_ptr<RtcEventLogOutput> FileLogWriterFactory::Create(
59 std::string filename) {
60 return absl::make_unique<webrtc_impl::FileLogWriter>(base_path_ + filename);
61}
62} // namespace webrtc