• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
13 
14 #include "rtc_base/checks.h"
15 #include "rtc_base/logging.h"
16 #include "test/testsupport/file_utils.h"
17 
18 namespace webrtc {
19 namespace webrtc_impl {
20 
FileLogWriter(std::string file_path)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 
~FileLogWriter()27 FileLogWriter::~FileLogWriter() {
28   std::fclose(out_);
29 }
30 
IsActive() const31 bool FileLogWriter::IsActive() const {
32   return true;
33 }
34 
Write(const std::string & value)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 
Flush()43 void FileLogWriter::Flush() {
44   RTC_CHECK_EQ(fflush(out_), 0) << "fflush failed unexpectedly: " << errno;
45 }
46 
47 }  // namespace webrtc_impl
48 
FileLogWriterFactory(std::string base_path)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 
~FileLogWriterFactory()57 FileLogWriterFactory::~FileLogWriterFactory() {}
58 
Create(std::string filename)59 std::unique_ptr<RtcEventLogOutput> FileLogWriterFactory::Create(
60     std::string filename) {
61   return std::make_unique<webrtc_impl::FileLogWriter>(base_path_ + filename);
62 }
63 }  // namespace webrtc
64