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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/filerotatingstream.h" |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 14 | #include <cstdio> |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 15 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | #include <utility> |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 17 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 18 | #if defined(WEBRTC_WIN) |
| 19 | #include <windows.h> |
| 20 | #include "rtc_base/stringutils.h" |
| 21 | #else |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 22 | #include <dirent.h> |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 23 | #include <sys/stat.h> |
Niels Möller | b739666 | 2018-11-13 11:55:19 +0100 | [diff] [blame] | 24 | #include <unistd.h> |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 25 | #endif // WEBRTC_WIN |
| 26 | |
| 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, |
| 173 | const std::string& file_prefix) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 174 | : FileRotatingStream(dir_path, file_prefix, 0, 0, kRead) {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 175 | |
| 176 | FileRotatingStream::FileRotatingStream(const std::string& dir_path, |
| 177 | const std::string& file_prefix, |
| 178 | size_t max_file_size, |
| 179 | size_t num_files) |
| 180 | : FileRotatingStream(dir_path, |
| 181 | file_prefix, |
| 182 | max_file_size, |
| 183 | num_files, |
| 184 | kWrite) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 185 | RTC_DCHECK_GT(max_file_size, 0); |
| 186 | RTC_DCHECK_GT(num_files, 1); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | FileRotatingStream::FileRotatingStream(const std::string& dir_path, |
| 190 | const std::string& file_prefix, |
| 191 | size_t max_file_size, |
| 192 | size_t num_files, |
| 193 | Mode mode) |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 194 | : dir_path_(AddTrailingPathDelimiterIfNeeded(dir_path)), |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 195 | file_prefix_(file_prefix), |
| 196 | mode_(mode), |
| 197 | file_stream_(nullptr), |
| 198 | max_file_size_(max_file_size), |
| 199 | current_file_index_(0), |
| 200 | rotation_index_(0), |
| 201 | current_bytes_written_(0), |
| 202 | disable_buffering_(false) { |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 203 | RTC_DCHECK(IsFolder(dir_path)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 204 | switch (mode) { |
| 205 | case kWrite: { |
| 206 | file_names_.clear(); |
| 207 | for (size_t i = 0; i < num_files; ++i) { |
| 208 | file_names_.push_back(GetFilePath(i, num_files)); |
| 209 | } |
| 210 | rotation_index_ = num_files - 1; |
| 211 | break; |
| 212 | } |
| 213 | case kRead: { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 214 | file_names_ = GetFilesWithPrefix(dir_path_, file_prefix_); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 215 | std::sort(file_names_.begin(), file_names_.end()); |
| 216 | if (file_names_.size() > 0) { |
| 217 | // |file_names_| is sorted newest first, so read from the end. |
| 218 | current_file_index_ = file_names_.size() - 1; |
| 219 | } |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 225 | FileRotatingStream::~FileRotatingStream() {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 226 | |
| 227 | StreamState FileRotatingStream::GetState() const { |
| 228 | if (mode_ == kRead && current_file_index_ < file_names_.size()) { |
| 229 | return SS_OPEN; |
| 230 | } |
| 231 | if (!file_stream_) { |
| 232 | return SS_CLOSED; |
| 233 | } |
| 234 | return file_stream_->GetState(); |
| 235 | } |
| 236 | |
| 237 | StreamResult FileRotatingStream::Read(void* buffer, |
| 238 | size_t buffer_len, |
| 239 | size_t* read, |
| 240 | int* error) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 241 | RTC_DCHECK(buffer); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 242 | if (mode_ != kRead) { |
| 243 | return SR_EOS; |
| 244 | } |
| 245 | if (current_file_index_ >= file_names_.size()) { |
| 246 | return SR_EOS; |
| 247 | } |
| 248 | // We will have no file stream initially, and when we are finished with the |
| 249 | // previous file. |
| 250 | if (!file_stream_) { |
| 251 | if (!OpenCurrentFile()) { |
| 252 | return SR_ERROR; |
| 253 | } |
| 254 | } |
| 255 | int local_error = 0; |
| 256 | if (!error) { |
| 257 | error = &local_error; |
| 258 | } |
| 259 | StreamResult result = file_stream_->Read(buffer, buffer_len, read, error); |
| 260 | if (result == SR_EOS || result == SR_ERROR) { |
| 261 | if (result == SR_ERROR) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 262 | RTC_LOG(LS_ERROR) << "Failed to read from: " |
| 263 | << file_names_[current_file_index_] |
| 264 | << "Error: " << error; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 265 | } |
| 266 | // Reached the end of the file, read next file. If there is an error return |
| 267 | // the error status but allow for a next read by reading next file. |
| 268 | CloseCurrentFile(); |
| 269 | if (current_file_index_ == 0) { |
| 270 | // Just finished reading the last file, signal EOS by setting index. |
| 271 | current_file_index_ = file_names_.size(); |
| 272 | } else { |
| 273 | --current_file_index_; |
| 274 | } |
| 275 | if (read) { |
| 276 | *read = 0; |
| 277 | } |
| 278 | return result == SR_EOS ? SR_SUCCESS : result; |
| 279 | } else if (result == SR_SUCCESS) { |
| 280 | // Succeeded, continue reading from this file. |
| 281 | return SR_SUCCESS; |
| 282 | } else { |
| 283 | RTC_NOTREACHED(); |
| 284 | } |
| 285 | return result; |
| 286 | } |
| 287 | |
| 288 | StreamResult FileRotatingStream::Write(const void* data, |
| 289 | size_t data_len, |
| 290 | size_t* written, |
| 291 | int* error) { |
| 292 | if (mode_ != kWrite) { |
| 293 | return SR_EOS; |
| 294 | } |
| 295 | if (!file_stream_) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 296 | std::fprintf(stderr, "Open() must be called before Write.\n"); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 297 | return SR_ERROR; |
| 298 | } |
| 299 | // Write as much as will fit in to the current file. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 300 | RTC_DCHECK_LT(current_bytes_written_, max_file_size_); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 301 | size_t remaining_bytes = max_file_size_ - current_bytes_written_; |
| 302 | size_t write_length = std::min(data_len, remaining_bytes); |
| 303 | size_t local_written = 0; |
| 304 | if (!written) { |
| 305 | written = &local_written; |
| 306 | } |
| 307 | StreamResult result = file_stream_->Write(data, write_length, written, error); |
| 308 | current_bytes_written_ += *written; |
| 309 | |
| 310 | // If we're done with this file, rotate it out. |
| 311 | if (current_bytes_written_ >= max_file_size_) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 312 | RTC_DCHECK_EQ(current_bytes_written_, max_file_size_); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 313 | RotateFiles(); |
| 314 | } |
| 315 | return result; |
| 316 | } |
| 317 | |
| 318 | bool FileRotatingStream::Flush() { |
| 319 | if (!file_stream_) { |
| 320 | return false; |
| 321 | } |
| 322 | return file_stream_->Flush(); |
| 323 | } |
| 324 | |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 325 | bool FileRotatingStream::GetSize(size_t* size) const { |
| 326 | if (mode_ != kRead) { |
| 327 | // Not possible to get accurate size on disk when writing because of |
| 328 | // potential buffering. |
| 329 | return false; |
| 330 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 331 | RTC_DCHECK(size); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 332 | *size = 0; |
| 333 | size_t total_size = 0; |
| 334 | for (auto file_name : file_names_) { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 335 | total_size += GetFileSize(file_name).value_or(0); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 336 | } |
| 337 | *size = total_size; |
| 338 | return true; |
| 339 | } |
| 340 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 341 | void FileRotatingStream::Close() { |
| 342 | CloseCurrentFile(); |
| 343 | } |
| 344 | |
| 345 | bool FileRotatingStream::Open() { |
| 346 | switch (mode_) { |
| 347 | case kRead: |
| 348 | // Defer opening to when we first read since we want to return read error |
| 349 | // if we fail to open next file. |
| 350 | return true; |
| 351 | case kWrite: { |
| 352 | // Delete existing files when opening for write. |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 353 | std::vector<std::string> matching_files = |
| 354 | GetFilesWithPrefix(dir_path_, file_prefix_); |
| 355 | for (const auto& matching_file : matching_files) { |
| 356 | if (!DeleteFile(matching_file)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 357 | std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | return OpenCurrentFile(); |
| 361 | } |
| 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | bool FileRotatingStream::DisableBuffering() { |
| 367 | disable_buffering_ = true; |
| 368 | if (!file_stream_) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 369 | std::fprintf(stderr, "Open() must be called before DisableBuffering().\n"); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | return file_stream_->DisableBuffering(); |
| 373 | } |
| 374 | |
| 375 | std::string FileRotatingStream::GetFilePath(size_t index) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 376 | RTC_DCHECK_LT(index, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 377 | return file_names_[index]; |
| 378 | } |
| 379 | |
| 380 | bool FileRotatingStream::OpenCurrentFile() { |
| 381 | CloseCurrentFile(); |
| 382 | |
| 383 | // Opens the appropriate file in the appropriate mode. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 384 | RTC_DCHECK_LT(current_file_index_, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 385 | std::string file_path = file_names_[current_file_index_]; |
| 386 | file_stream_.reset(new FileStream()); |
| 387 | const char* mode = nullptr; |
| 388 | switch (mode_) { |
| 389 | case kWrite: |
| 390 | mode = "w+"; |
| 391 | // We should always we writing to the zero-th file. |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 392 | RTC_DCHECK_EQ(current_file_index_, 0); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 393 | break; |
| 394 | case kRead: |
| 395 | mode = "r"; |
| 396 | break; |
| 397 | } |
| 398 | int error = 0; |
| 399 | if (!file_stream_->Open(file_path, mode, &error)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 400 | std::fprintf(stderr, "Failed to open: %s Error: %i\n", file_path.c_str(), |
| 401 | error); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 402 | file_stream_.reset(); |
| 403 | return false; |
| 404 | } |
| 405 | if (disable_buffering_) { |
| 406 | file_stream_->DisableBuffering(); |
| 407 | } |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | void FileRotatingStream::CloseCurrentFile() { |
| 412 | if (!file_stream_) { |
| 413 | return; |
| 414 | } |
| 415 | current_bytes_written_ = 0; |
| 416 | file_stream_.reset(); |
| 417 | } |
| 418 | |
| 419 | void FileRotatingStream::RotateFiles() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 420 | RTC_DCHECK_EQ(mode_, kWrite); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 421 | CloseCurrentFile(); |
| 422 | // Rotates the files by deleting the file at |rotation_index_|, which is the |
| 423 | // oldest file and then renaming the newer files to have an incremented index. |
| 424 | // See header file comments for example. |
haysc | d02b0fa | 2015-12-08 13:59:05 -0800 | [diff] [blame] | 425 | RTC_DCHECK_LT(rotation_index_, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 426 | std::string file_to_delete = file_names_[rotation_index_]; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 427 | if (IsFile(file_to_delete)) { |
| 428 | if (!DeleteFile(file_to_delete)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 429 | std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | for (auto i = rotation_index_; i > 0; --i) { |
| 433 | std::string rotated_name = file_names_[i]; |
| 434 | std::string unrotated_name = file_names_[i - 1]; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 435 | if (IsFile(unrotated_name)) { |
| 436 | if (!MoveFile(unrotated_name, rotated_name)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 437 | std::fprintf(stderr, "Failed to move: %s to %s\n", |
| 438 | unrotated_name.c_str(), rotated_name.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | } |
| 442 | // Create a new file for 0th index. |
| 443 | OpenCurrentFile(); |
| 444 | OnRotation(); |
| 445 | } |
| 446 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 447 | std::string FileRotatingStream::GetFilePath(size_t index, |
| 448 | size_t num_files) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 449 | RTC_DCHECK_LT(index, num_files); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 450 | |
Jonas Olsson | 671cae2 | 2018-06-14 09:57:39 +0200 | [diff] [blame] | 451 | const size_t buffer_size = 32; |
| 452 | char file_postfix[buffer_size]; |
| 453 | // We want to zero pad the index so that it will sort nicely. |
| 454 | const int max_digits = std::snprintf(nullptr, 0, "%zu", num_files - 1); |
| 455 | RTC_DCHECK_LT(1 + max_digits, buffer_size); |
| 456 | std::snprintf(file_postfix, buffer_size, "_%0*zu", max_digits, index); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 457 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 458 | return dir_path_ + file_prefix_ + file_postfix; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | CallSessionFileRotatingStream::CallSessionFileRotatingStream( |
| 462 | const std::string& dir_path) |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame^] | 463 | : FileRotatingStream(dir_path, kCallSessionLogPrefix), |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 464 | max_total_log_size_(0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 465 | num_rotations_(0) {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 466 | |
| 467 | CallSessionFileRotatingStream::CallSessionFileRotatingStream( |
| 468 | const std::string& dir_path, |
| 469 | size_t max_total_log_size) |
| 470 | : FileRotatingStream(dir_path, |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame^] | 471 | kCallSessionLogPrefix, |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 472 | max_total_log_size / 2, |
| 473 | GetNumRotatingLogFiles(max_total_log_size) + 1), |
| 474 | max_total_log_size_(max_total_log_size), |
| 475 | num_rotations_(0) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 476 | RTC_DCHECK_GE(max_total_log_size, 4); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 477 | } |
| 478 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 479 | const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize = |
| 480 | 1024 * 1024; |
| 481 | |
| 482 | void CallSessionFileRotatingStream::OnRotation() { |
| 483 | ++num_rotations_; |
| 484 | if (num_rotations_ == 1) { |
| 485 | // On the first rotation adjust the max file size so subsequent files after |
| 486 | // the first are smaller. |
| 487 | SetMaxFileSize(GetRotatingLogSize(max_total_log_size_)); |
| 488 | } else if (num_rotations_ == (GetNumFiles() - 1)) { |
| 489 | // On the next rotation the very first file is going to be deleted. Change |
| 490 | // the rotation index so this doesn't happen. |
| 491 | SetRotationIndex(GetRotationIndex() - 1); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | size_t CallSessionFileRotatingStream::GetRotatingLogSize( |
| 496 | size_t max_total_log_size) { |
| 497 | size_t num_rotating_log_files = GetNumRotatingLogFiles(max_total_log_size); |
| 498 | size_t rotating_log_size = num_rotating_log_files > 2 |
| 499 | ? kRotatingLogFileDefaultSize |
| 500 | : max_total_log_size / 4; |
| 501 | return rotating_log_size; |
| 502 | } |
| 503 | |
| 504 | size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles( |
| 505 | size_t max_total_log_size) { |
| 506 | // At minimum have two rotating files. Otherwise split the available log size |
| 507 | // evenly across 1MB files. |
| 508 | return std::max((size_t)2, |
| 509 | (max_total_log_size / 2) / kRotatingLogFileDefaultSize); |
| 510 | } |
| 511 | |
Niels Möller | d9ac058 | 2019-01-03 14:21:38 +0100 | [diff] [blame^] | 512 | FileRotatingStreamReader::FileRotatingStreamReader( |
| 513 | const std::string& dir_path, |
| 514 | const std::string& file_prefix) { |
| 515 | file_names_ = GetFilesWithPrefix(AddTrailingPathDelimiterIfNeeded(dir_path), |
| 516 | file_prefix); |
| 517 | |
| 518 | // Plain sort of the file names would sort by age, i.e., oldest last. Using |
| 519 | // std::greater gives us the desired chronological older, oldest first. |
| 520 | std::sort(file_names_.begin(), file_names_.end(), |
| 521 | std::greater<std::string>()); |
| 522 | } |
| 523 | |
| 524 | FileRotatingStreamReader::~FileRotatingStreamReader() = default; |
| 525 | |
| 526 | size_t FileRotatingStreamReader::GetSize() const { |
| 527 | size_t total_size = 0; |
| 528 | for (const auto& file_name : file_names_) { |
| 529 | total_size += GetFileSize(file_name).value_or(0); |
| 530 | } |
| 531 | return total_size; |
| 532 | } |
| 533 | |
| 534 | size_t FileRotatingStreamReader::ReadAll(void* buffer, size_t size) const { |
| 535 | size_t done = 0; |
| 536 | for (const auto& file_name : file_names_) { |
| 537 | if (done < size) { |
| 538 | FILE* f = fopen(file_name.c_str(), "rb"); |
| 539 | if (!f) { |
| 540 | break; |
| 541 | } |
| 542 | done += fread(static_cast<char*>(buffer) + done, 1, size - done, f); |
| 543 | fclose(f); |
| 544 | } else { |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | return done; |
| 549 | } |
| 550 | |
| 551 | CallSessionFileRotatingStreamReader::CallSessionFileRotatingStreamReader( |
| 552 | const std::string& dir_path) |
| 553 | : FileRotatingStreamReader(dir_path, kCallSessionLogPrefix) {} |
| 554 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 555 | } // namespace rtc |