tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/file_rotating_stream.h" |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 12 | |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 13 | #include <cstdio> |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 14 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 15 | #include <utility> |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 16 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 17 | #include "absl/strings/string_view.h" |
| 18 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 19 | #if defined(WEBRTC_WIN) |
| 20 | #include <windows.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 21 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/string_utils.h" |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 23 | #else |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 24 | #include <dirent.h> |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 25 | #include <sys/stat.h> |
Niels Möller | b739666 | 2018-11-13 11:55:19 +0100 | [diff] [blame] | 26 | #include <unistd.h> |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 27 | #endif // WEBRTC_WIN |
| 28 | |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 29 | #include "absl/algorithm/container.h" |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 30 | #include "absl/strings/match.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 31 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "rtc_base/checks.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 33 | #include "rtc_base/logging.h" |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 34 | #include "rtc_base/strings/string_builder.h" |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 35 | |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 36 | // Note: We use fprintf for logging in the write paths of this stream to avoid |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 37 | // infinite loops when logging. |
| 38 | |
| 39 | namespace rtc { |
| 40 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 41 | namespace { |
| 42 | |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame] | 43 | const char kCallSessionLogPrefix[] = "webrtc_log"; |
| 44 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 45 | std::string AddTrailingPathDelimiterIfNeeded(absl::string_view directory); |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 46 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 47 | // `dir` must have a trailing delimiter. `prefix` must not include wild card |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 48 | // characters. |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 49 | std::vector<std::string> GetFilesWithPrefix(absl::string_view directory, |
| 50 | absl::string_view prefix); |
| 51 | bool DeleteFile(absl::string_view file); |
| 52 | bool MoveFile(absl::string_view old_file, absl::string_view new_file); |
| 53 | bool IsFile(absl::string_view file); |
| 54 | bool IsFolder(absl::string_view file); |
| 55 | absl::optional<size_t> GetFileSize(absl::string_view file); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 56 | |
| 57 | #if defined(WEBRTC_WIN) |
| 58 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 59 | std::string AddTrailingPathDelimiterIfNeeded(absl::string_view directory) { |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 60 | if (absl::EndsWith(directory, "\\")) { |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 61 | return std::string(directory); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 62 | } |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 63 | return std::string(directory) + "\\"; |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 66 | std::vector<std::string> GetFilesWithPrefix(absl::string_view directory, |
| 67 | absl::string_view prefix) { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 68 | RTC_DCHECK(absl::EndsWith(directory, "\\")); |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame] | 69 | WIN32_FIND_DATAW data; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 70 | HANDLE handle; |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 71 | StringBuilder pattern_builder{directory}; |
| 72 | pattern_builder << prefix << "*"; |
| 73 | handle = ::FindFirstFileW(ToUtf16(pattern_builder.str()).c_str(), &data); |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 74 | if (handle == INVALID_HANDLE_VALUE) |
| 75 | return {}; |
| 76 | |
| 77 | std::vector<std::string> file_list; |
| 78 | do { |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 79 | StringBuilder file_builder{directory}; |
| 80 | file_builder << ToUtf8(data.cFileName); |
| 81 | file_list.emplace_back(file_builder.Release()); |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame] | 82 | } while (::FindNextFileW(handle, &data) == TRUE); |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 83 | |
| 84 | ::FindClose(handle); |
| 85 | return file_list; |
| 86 | } |
| 87 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 88 | bool DeleteFile(absl::string_view file) { |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame] | 89 | return ::DeleteFileW(ToUtf16(file).c_str()) != 0; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 92 | bool MoveFile(absl::string_view old_file, absl::string_view new_file) { |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame] | 93 | return ::MoveFileW(ToUtf16(old_file).c_str(), ToUtf16(new_file).c_str()) != 0; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 96 | bool IsFile(absl::string_view file) { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 97 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame] | 98 | if (0 == ::GetFileAttributesExW(ToUtf16(file).c_str(), GetFileExInfoStandard, |
| 99 | &data)) |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 100 | return false; |
| 101 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; |
| 102 | } |
| 103 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 104 | bool IsFolder(absl::string_view file) { |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 105 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame] | 106 | if (0 == ::GetFileAttributesExW(ToUtf16(file).c_str(), GetFileExInfoStandard, |
| 107 | &data)) |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 108 | return false; |
| 109 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == |
| 110 | FILE_ATTRIBUTE_DIRECTORY; |
| 111 | } |
| 112 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 113 | absl::optional<size_t> GetFileSize(absl::string_view file) { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 114 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
Mirko Bonadei | 673f7e5 | 2019-03-25 09:01:02 +0100 | [diff] [blame] | 115 | if (::GetFileAttributesExW(ToUtf16(file).c_str(), GetFileExInfoStandard, |
| 116 | &data) == 0) |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 117 | return absl::nullopt; |
| 118 | return data.nFileSizeLow; |
| 119 | } |
| 120 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 121 | #else // defined(WEBRTC_WIN) |
| 122 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 123 | std::string AddTrailingPathDelimiterIfNeeded(absl::string_view directory) { |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 124 | if (absl::EndsWith(directory, "/")) { |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 125 | return std::string(directory); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 126 | } |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 127 | return std::string(directory) + "/"; |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 130 | std::vector<std::string> GetFilesWithPrefix(absl::string_view directory, |
| 131 | absl::string_view prefix) { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 132 | RTC_DCHECK(absl::EndsWith(directory, "/")); |
Ali Tofigh | 98bfd99 | 2022-07-22 22:10:49 +0200 | [diff] [blame] | 133 | std::string directory_str(directory); |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 134 | DIR* dir = ::opendir(directory_str.c_str()); |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 135 | if (dir == nullptr) |
| 136 | return {}; |
| 137 | std::vector<std::string> file_list; |
| 138 | for (struct dirent* dirent = ::readdir(dir); dirent; |
| 139 | dirent = ::readdir(dir)) { |
| 140 | std::string name = dirent->d_name; |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 141 | if (name.compare(0, prefix.size(), prefix.data(), prefix.size()) == 0) { |
| 142 | file_list.emplace_back(directory_str + name); |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | ::closedir(dir); |
| 146 | return file_list; |
| 147 | } |
| 148 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 149 | bool DeleteFile(absl::string_view file) { |
| 150 | return ::unlink(std::string(file).c_str()) == 0; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 153 | bool MoveFile(absl::string_view old_file, absl::string_view new_file) { |
| 154 | return ::rename(std::string(old_file).c_str(), |
| 155 | std::string(new_file).c_str()) == 0; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 158 | bool IsFile(absl::string_view file) { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 159 | struct stat st; |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 160 | int res = ::stat(std::string(file).c_str(), &st); |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 161 | // Treat symlinks, named pipes, etc. all as files. |
| 162 | return res == 0 && !S_ISDIR(st.st_mode); |
| 163 | } |
| 164 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 165 | bool IsFolder(absl::string_view file) { |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 166 | struct stat st; |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 167 | int res = ::stat(std::string(file).c_str(), &st); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 168 | return res == 0 && S_ISDIR(st.st_mode); |
| 169 | } |
| 170 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 171 | absl::optional<size_t> GetFileSize(absl::string_view file) { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 172 | struct stat st; |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 173 | if (::stat(std::string(file).c_str(), &st) != 0) |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 174 | return absl::nullopt; |
| 175 | return st.st_size; |
| 176 | } |
| 177 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 178 | #endif |
| 179 | |
| 180 | } // namespace |
| 181 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 182 | FileRotatingStream::FileRotatingStream(absl::string_view dir_path, |
| 183 | absl::string_view file_prefix, |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 184 | size_t max_file_size, |
| 185 | size_t num_files) |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 186 | : dir_path_(AddTrailingPathDelimiterIfNeeded(dir_path)), |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 187 | file_prefix_(file_prefix), |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 188 | max_file_size_(max_file_size), |
| 189 | current_file_index_(0), |
| 190 | rotation_index_(0), |
| 191 | current_bytes_written_(0), |
| 192 | disable_buffering_(false) { |
Niels Möller | 6ffe62a | 2019-01-08 13:22:57 +0100 | [diff] [blame] | 193 | RTC_DCHECK_GT(max_file_size, 0); |
| 194 | RTC_DCHECK_GT(num_files, 1); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 195 | RTC_DCHECK(IsFolder(dir_path)); |
Niels Möller | 6ffe62a | 2019-01-08 13:22:57 +0100 | [diff] [blame] | 196 | file_names_.clear(); |
| 197 | for (size_t i = 0; i < num_files; ++i) { |
| 198 | file_names_.push_back(GetFilePath(i, num_files)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 199 | } |
Niels Möller | 6ffe62a | 2019-01-08 13:22:57 +0100 | [diff] [blame] | 200 | rotation_index_ = num_files - 1; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 203 | FileRotatingStream::~FileRotatingStream() {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 204 | |
Niels Möller | a9311b6 | 2021-03-25 15:29:02 +0100 | [diff] [blame] | 205 | bool FileRotatingStream::IsOpen() const { |
| 206 | return file_.is_open(); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Niels Möller | a9311b6 | 2021-03-25 15:29:02 +0100 | [diff] [blame] | 209 | bool FileRotatingStream::Write(const void* data, size_t data_len) { |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 210 | if (!file_.is_open()) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 211 | std::fprintf(stderr, "Open() must be called before Write.\n"); |
Niels Möller | a9311b6 | 2021-03-25 15:29:02 +0100 | [diff] [blame] | 212 | return false; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 213 | } |
Niels Möller | a9311b6 | 2021-03-25 15:29:02 +0100 | [diff] [blame] | 214 | while (data_len > 0) { |
| 215 | // Write as much as will fit in to the current file. |
| 216 | RTC_DCHECK_LT(current_bytes_written_, max_file_size_); |
| 217 | size_t remaining_bytes = max_file_size_ - current_bytes_written_; |
| 218 | size_t write_length = std::min(data_len, remaining_bytes); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 219 | |
Niels Möller | a9311b6 | 2021-03-25 15:29:02 +0100 | [diff] [blame] | 220 | if (!file_.Write(data, write_length)) { |
| 221 | return false; |
| 222 | } |
| 223 | if (disable_buffering_ && !file_.Flush()) { |
| 224 | return false; |
| 225 | } |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 226 | |
Niels Möller | a9311b6 | 2021-03-25 15:29:02 +0100 | [diff] [blame] | 227 | current_bytes_written_ += write_length; |
| 228 | |
| 229 | // If we're done with this file, rotate it out. |
| 230 | if (current_bytes_written_ >= max_file_size_) { |
| 231 | RTC_DCHECK_EQ(current_bytes_written_, max_file_size_); |
| 232 | RotateFiles(); |
| 233 | } |
| 234 | data_len -= write_length; |
| 235 | data = |
| 236 | static_cast<const void*>(static_cast<const char*>(data) + write_length); |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 237 | } |
Niels Möller | a9311b6 | 2021-03-25 15:29:02 +0100 | [diff] [blame] | 238 | return true; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | bool FileRotatingStream::Flush() { |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 242 | if (!file_.is_open()) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 245 | return file_.Flush(); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 246 | } |
| 247 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 248 | void FileRotatingStream::Close() { |
| 249 | CloseCurrentFile(); |
| 250 | } |
| 251 | |
| 252 | bool FileRotatingStream::Open() { |
Niels Möller | 6ffe62a | 2019-01-08 13:22:57 +0100 | [diff] [blame] | 253 | // Delete existing files when opening for write. |
| 254 | std::vector<std::string> matching_files = |
| 255 | GetFilesWithPrefix(dir_path_, file_prefix_); |
| 256 | for (const auto& matching_file : matching_files) { |
| 257 | if (!DeleteFile(matching_file)) { |
| 258 | std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
Niels Möller | 6ffe62a | 2019-01-08 13:22:57 +0100 | [diff] [blame] | 261 | return OpenCurrentFile(); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | bool FileRotatingStream::DisableBuffering() { |
| 265 | disable_buffering_ = true; |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 266 | return true; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | std::string FileRotatingStream::GetFilePath(size_t index) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 270 | RTC_DCHECK_LT(index, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 271 | return file_names_[index]; |
| 272 | } |
| 273 | |
| 274 | bool FileRotatingStream::OpenCurrentFile() { |
| 275 | CloseCurrentFile(); |
| 276 | |
| 277 | // Opens the appropriate file in the appropriate mode. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 278 | RTC_DCHECK_LT(current_file_index_, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 279 | std::string file_path = file_names_[current_file_index_]; |
Niels Möller | 6ffe62a | 2019-01-08 13:22:57 +0100 | [diff] [blame] | 280 | |
| 281 | // We should always be writing to the zero-th file. |
| 282 | RTC_DCHECK_EQ(current_file_index_, 0); |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 283 | int error; |
| 284 | file_ = webrtc::FileWrapper::OpenWriteOnly(file_path, &error); |
| 285 | if (!file_.is_open()) { |
| 286 | std::fprintf(stderr, "Failed to open: %s Error: %d\n", file_path.c_str(), |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 287 | error); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 288 | return false; |
| 289 | } |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
| 293 | void FileRotatingStream::CloseCurrentFile() { |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 294 | if (!file_.is_open()) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | current_bytes_written_ = 0; |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 298 | file_.Close(); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void FileRotatingStream::RotateFiles() { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 302 | CloseCurrentFile(); |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 303 | // Rotates the files by deleting the file at `rotation_index_`, which is the |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 304 | // oldest file and then renaming the newer files to have an incremented index. |
| 305 | // See header file comments for example. |
haysc | d02b0fa | 2015-12-08 13:59:05 -0800 | [diff] [blame] | 306 | RTC_DCHECK_LT(rotation_index_, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 307 | std::string file_to_delete = file_names_[rotation_index_]; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 308 | if (IsFile(file_to_delete)) { |
| 309 | if (!DeleteFile(file_to_delete)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 310 | std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | for (auto i = rotation_index_; i > 0; --i) { |
| 314 | std::string rotated_name = file_names_[i]; |
| 315 | std::string unrotated_name = file_names_[i - 1]; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 316 | if (IsFile(unrotated_name)) { |
| 317 | if (!MoveFile(unrotated_name, rotated_name)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 318 | std::fprintf(stderr, "Failed to move: %s to %s\n", |
| 319 | unrotated_name.c_str(), rotated_name.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |
| 323 | // Create a new file for 0th index. |
| 324 | OpenCurrentFile(); |
| 325 | OnRotation(); |
| 326 | } |
| 327 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 328 | std::string FileRotatingStream::GetFilePath(size_t index, |
| 329 | size_t num_files) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 330 | RTC_DCHECK_LT(index, num_files); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 331 | |
Jonas Olsson | 671cae2 | 2018-06-14 09:57:39 +0200 | [diff] [blame] | 332 | const size_t buffer_size = 32; |
| 333 | char file_postfix[buffer_size]; |
| 334 | // We want to zero pad the index so that it will sort nicely. |
| 335 | const int max_digits = std::snprintf(nullptr, 0, "%zu", num_files - 1); |
| 336 | RTC_DCHECK_LT(1 + max_digits, buffer_size); |
| 337 | std::snprintf(file_postfix, buffer_size, "_%0*zu", max_digits, index); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 338 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 339 | return dir_path_ + file_prefix_ + file_postfix; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | CallSessionFileRotatingStream::CallSessionFileRotatingStream( |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 343 | absl::string_view dir_path, |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 344 | size_t max_total_log_size) |
| 345 | : FileRotatingStream(dir_path, |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame] | 346 | kCallSessionLogPrefix, |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 347 | max_total_log_size / 2, |
| 348 | GetNumRotatingLogFiles(max_total_log_size) + 1), |
| 349 | max_total_log_size_(max_total_log_size), |
| 350 | num_rotations_(0) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 351 | RTC_DCHECK_GE(max_total_log_size, 4); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 352 | } |
| 353 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 354 | const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize = |
| 355 | 1024 * 1024; |
| 356 | |
| 357 | void CallSessionFileRotatingStream::OnRotation() { |
| 358 | ++num_rotations_; |
| 359 | if (num_rotations_ == 1) { |
| 360 | // On the first rotation adjust the max file size so subsequent files after |
| 361 | // the first are smaller. |
| 362 | SetMaxFileSize(GetRotatingLogSize(max_total_log_size_)); |
| 363 | } else if (num_rotations_ == (GetNumFiles() - 1)) { |
| 364 | // On the next rotation the very first file is going to be deleted. Change |
| 365 | // the rotation index so this doesn't happen. |
| 366 | SetRotationIndex(GetRotationIndex() - 1); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | size_t CallSessionFileRotatingStream::GetRotatingLogSize( |
| 371 | size_t max_total_log_size) { |
| 372 | size_t num_rotating_log_files = GetNumRotatingLogFiles(max_total_log_size); |
| 373 | size_t rotating_log_size = num_rotating_log_files > 2 |
| 374 | ? kRotatingLogFileDefaultSize |
| 375 | : max_total_log_size / 4; |
| 376 | return rotating_log_size; |
| 377 | } |
| 378 | |
| 379 | size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles( |
| 380 | size_t max_total_log_size) { |
| 381 | // At minimum have two rotating files. Otherwise split the available log size |
| 382 | // evenly across 1MB files. |
| 383 | return std::max((size_t)2, |
| 384 | (max_total_log_size / 2) / kRotatingLogFileDefaultSize); |
| 385 | } |
| 386 | |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame] | 387 | FileRotatingStreamReader::FileRotatingStreamReader( |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 388 | absl::string_view dir_path, |
| 389 | absl::string_view file_prefix) { |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame] | 390 | file_names_ = GetFilesWithPrefix(AddTrailingPathDelimiterIfNeeded(dir_path), |
| 391 | file_prefix); |
| 392 | |
| 393 | // Plain sort of the file names would sort by age, i.e., oldest last. Using |
| 394 | // std::greater gives us the desired chronological older, oldest first. |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 395 | absl::c_sort(file_names_, std::greater<std::string>()); |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | FileRotatingStreamReader::~FileRotatingStreamReader() = default; |
| 399 | |
| 400 | size_t FileRotatingStreamReader::GetSize() const { |
| 401 | size_t total_size = 0; |
| 402 | for (const auto& file_name : file_names_) { |
| 403 | total_size += GetFileSize(file_name).value_or(0); |
| 404 | } |
| 405 | return total_size; |
| 406 | } |
| 407 | |
| 408 | size_t FileRotatingStreamReader::ReadAll(void* buffer, size_t size) const { |
| 409 | size_t done = 0; |
| 410 | for (const auto& file_name : file_names_) { |
| 411 | if (done < size) { |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 412 | webrtc::FileWrapper f = webrtc::FileWrapper::OpenReadOnly(file_name); |
| 413 | if (!f.is_open()) { |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame] | 414 | break; |
| 415 | } |
Niels Möller | 23213d9 | 2019-01-22 11:01:24 +0100 | [diff] [blame] | 416 | done += f.Read(static_cast<char*>(buffer) + done, size - done); |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame] | 417 | } else { |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | return done; |
| 422 | } |
| 423 | |
| 424 | CallSessionFileRotatingStreamReader::CallSessionFileRotatingStreamReader( |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 425 | absl::string_view dir_path) |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame] | 426 | : FileRotatingStreamReader(dir_path, kCallSessionLogPrefix) {} |
| 427 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 428 | } // namespace rtc |