blob: 2e31c2df66ac9857a68bea6ca041f4449f4278ea [file] [log] [blame]
Elad Alon83ccca12017-10-04 13:18:26 +02001/*
2 * Copyright (c) 2017 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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "api/rtc_event_log_output_file.h"
12
Elad Alon83ccca12017-10-04 13:18:26 +020013#include <limits>
Niels Möllere3962762019-05-06 09:14:31 +020014#include <utility>
Elad Alon83ccca12017-10-04 13:18:26 +020015
Danil Chapovalovb32f2c72019-05-22 13:39:25 +020016#include "api/rtc_event_log/rtc_event_log.h"
Elad Alon83ccca12017-10-04 13:18:26 +020017#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
Elad Alon83ccca12017-10-04 13:18:26 +020019
20namespace webrtc {
21
22// Together with the assumption of no single Write() would ever be called on
23// an input with length greater-than-or-equal-to (max(size_t) / 2), this
24// guarantees no overflow of the check for remaining file capacity in Write().
25// This does *not* apply to files with unlimited size.
26const size_t RtcEventLogOutputFile::kMaxReasonableFileSize =
27 std::numeric_limits<size_t>::max() / 2;
28
29RtcEventLogOutputFile::RtcEventLogOutputFile(const std::string& file_name)
Niels Möllere3962762019-05-06 09:14:31 +020030 : RtcEventLogOutputFile(FileWrapper::OpenWriteOnly(file_name),
31 RtcEventLog::kUnlimitedOutput) {}
Elad Alon83ccca12017-10-04 13:18:26 +020032
33RtcEventLogOutputFile::RtcEventLogOutputFile(const std::string& file_name,
34 size_t max_size_bytes)
Elad Alon83ccca12017-10-04 13:18:26 +020035
Niels Möllere3962762019-05-06 09:14:31 +020036 // Unlike plain fopen, FileWrapper takes care of filename utf8 ->
37 // wchar conversion on Windows.
38 : RtcEventLogOutputFile(FileWrapper::OpenWriteOnly(file_name),
Niels Möllerd4d39902017-12-07 09:35:23 +010039 max_size_bytes) {}
Elad Alon83ccca12017-10-04 13:18:26 +020040
Niels Möllere3962762019-05-06 09:14:31 +020041RtcEventLogOutputFile::RtcEventLogOutputFile(FILE* file, size_t max_size_bytes)
42 : RtcEventLogOutputFile(FileWrapper(file), max_size_bytes) {}
43
44RtcEventLogOutputFile::RtcEventLogOutputFile(FileWrapper file,
45 size_t max_size_bytes)
46 : max_size_bytes_(max_size_bytes), file_(std::move(file)) {
47 RTC_CHECK_LE(max_size_bytes_, kMaxReasonableFileSize);
48 if (!file_.is_open()) {
49 RTC_LOG(LS_ERROR) << "Invalid file. WebRTC event log not started.";
50 }
51}
Elad Alon83ccca12017-10-04 13:18:26 +020052
Elad Alon83ccca12017-10-04 13:18:26 +020053bool RtcEventLogOutputFile::IsActive() const {
54 return IsActiveInternal();
55}
56
57bool RtcEventLogOutputFile::Write(const std::string& output) {
58 RTC_DCHECK(IsActiveInternal());
59 // No single write may be so big, that it would risk overflowing the
60 // calculation of (written_bytes_ + output.length()).
61 RTC_DCHECK_LT(output.length(), kMaxReasonableFileSize);
62
Elad Alon83ccca12017-10-04 13:18:26 +020063 if (max_size_bytes_ == RtcEventLog::kUnlimitedOutput ||
64 written_bytes_ + output.length() <= max_size_bytes_) {
Niels Möllere3962762019-05-06 09:14:31 +020065 if (file_.Write(output.c_str(), output.size())) {
Niels Möllerd4d39902017-12-07 09:35:23 +010066 written_bytes_ += output.size();
67 return true;
68 } else {
69 RTC_LOG(LS_ERROR) << "Write to WebRtcEventLog file failed.";
Elad Alon83ccca12017-10-04 13:18:26 +020070 }
71 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +010072 RTC_LOG(LS_VERBOSE) << "Max file size reached.";
Elad Alon83ccca12017-10-04 13:18:26 +020073 }
74
Niels Möllerd4d39902017-12-07 09:35:23 +010075 // Failed, for one of above reasons. Close output file.
Niels Möllere3962762019-05-06 09:14:31 +020076 file_.Close();
Niels Möllerd4d39902017-12-07 09:35:23 +010077 return false;
Elad Alon83ccca12017-10-04 13:18:26 +020078}
79
Niels Möllerd4d39902017-12-07 09:35:23 +010080// Internal non-virtual method.
Elad Alon83ccca12017-10-04 13:18:26 +020081bool RtcEventLogOutputFile::IsActiveInternal() const {
Niels Möllere3962762019-05-06 09:14:31 +020082 return file_.is_open();
Elad Alon83ccca12017-10-04 13:18:26 +020083}
84
85} // namespace webrtc