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