Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include <limits> |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 12 | #include <utility> |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 13 | |
Niels Möller | d8b9ed7 | 2019-05-08 13:53:51 +0200 | [diff] [blame] | 14 | #include "api/rtc_event_log_output_file.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 15 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/logging.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | FileWrapper FileWrapperFromPlatformFile(rtc::PlatformFile platform_file) { |
| 24 | if (platform_file == rtc::kInvalidPlatformFileValue) |
| 25 | return FileWrapper(); |
| 26 | |
| 27 | return FileWrapper(rtc::FdopenPlatformFileForWriting(platform_file)); |
| 28 | } |
| 29 | |
| 30 | } // namespace |
| 31 | |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 32 | // Together with the assumption of no single Write() would ever be called on |
| 33 | // an input with length greater-than-or-equal-to (max(size_t) / 2), this |
| 34 | // guarantees no overflow of the check for remaining file capacity in Write(). |
| 35 | // This does *not* apply to files with unlimited size. |
| 36 | const size_t RtcEventLogOutputFile::kMaxReasonableFileSize = |
| 37 | std::numeric_limits<size_t>::max() / 2; |
| 38 | |
| 39 | RtcEventLogOutputFile::RtcEventLogOutputFile(const std::string& file_name) |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 40 | : RtcEventLogOutputFile(FileWrapper::OpenWriteOnly(file_name), |
| 41 | RtcEventLog::kUnlimitedOutput) {} |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 42 | |
| 43 | RtcEventLogOutputFile::RtcEventLogOutputFile(const std::string& file_name, |
| 44 | size_t max_size_bytes) |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 45 | |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 46 | // Unlike plain fopen, FileWrapper takes care of filename utf8 -> |
| 47 | // wchar conversion on Windows. |
| 48 | : RtcEventLogOutputFile(FileWrapper::OpenWriteOnly(file_name), |
Niels Möller | d4d3990 | 2017-12-07 09:35:23 +0100 | [diff] [blame] | 49 | max_size_bytes) {} |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 50 | |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 51 | RtcEventLogOutputFile::RtcEventLogOutputFile(FILE* file, size_t max_size_bytes) |
| 52 | : RtcEventLogOutputFile(FileWrapper(file), max_size_bytes) {} |
| 53 | |
| 54 | RtcEventLogOutputFile::RtcEventLogOutputFile(FileWrapper file, |
| 55 | size_t max_size_bytes) |
| 56 | : max_size_bytes_(max_size_bytes), file_(std::move(file)) { |
| 57 | RTC_CHECK_LE(max_size_bytes_, kMaxReasonableFileSize); |
| 58 | if (!file_.is_open()) { |
| 59 | RTC_LOG(LS_ERROR) << "Invalid file. WebRTC event log not started."; |
| 60 | } |
| 61 | } |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 62 | |
Niels Möller | d4d3990 | 2017-12-07 09:35:23 +0100 | [diff] [blame] | 63 | RtcEventLogOutputFile::RtcEventLogOutputFile(rtc::PlatformFile platform_file, |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 64 | size_t max_size_bytes) |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 65 | : RtcEventLogOutputFile(FileWrapperFromPlatformFile(platform_file), |
| 66 | max_size_bytes) {} |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 67 | |
| 68 | bool RtcEventLogOutputFile::IsActive() const { |
| 69 | return IsActiveInternal(); |
| 70 | } |
| 71 | |
| 72 | bool RtcEventLogOutputFile::Write(const std::string& output) { |
| 73 | RTC_DCHECK(IsActiveInternal()); |
| 74 | // No single write may be so big, that it would risk overflowing the |
| 75 | // calculation of (written_bytes_ + output.length()). |
| 76 | RTC_DCHECK_LT(output.length(), kMaxReasonableFileSize); |
| 77 | |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 78 | if (max_size_bytes_ == RtcEventLog::kUnlimitedOutput || |
| 79 | written_bytes_ + output.length() <= max_size_bytes_) { |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 80 | if (file_.Write(output.c_str(), output.size())) { |
Niels Möller | d4d3990 | 2017-12-07 09:35:23 +0100 | [diff] [blame] | 81 | written_bytes_ += output.size(); |
| 82 | return true; |
| 83 | } else { |
| 84 | RTC_LOG(LS_ERROR) << "Write to WebRtcEventLog file failed."; |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 85 | } |
| 86 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 87 | RTC_LOG(LS_VERBOSE) << "Max file size reached."; |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Niels Möller | d4d3990 | 2017-12-07 09:35:23 +0100 | [diff] [blame] | 90 | // Failed, for one of above reasons. Close output file. |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 91 | file_.Close(); |
Niels Möller | d4d3990 | 2017-12-07 09:35:23 +0100 | [diff] [blame] | 92 | return false; |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Niels Möller | d4d3990 | 2017-12-07 09:35:23 +0100 | [diff] [blame] | 95 | // Internal non-virtual method. |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 96 | bool RtcEventLogOutputFile::IsActiveInternal() const { |
Niels Möller | e396276 | 2019-05-06 09:14:31 +0200 | [diff] [blame] | 97 | return file_.is_open(); |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace webrtc |