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 | |
| 39 | std::string AddTrailingPathDelimiterIfNeeded(std::string directory); |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 40 | |
| 41 | // |dir| must have a trailing delimiter. |prefix| must not include wild card |
| 42 | // characters. |
| 43 | std::vector<std::string> GetFilesWithPrefix(const std::string& directory, |
| 44 | const std::string& prefix); |
| 45 | bool DeleteFile(const std::string& file); |
| 46 | bool MoveFile(const std::string& old_file, const std::string& new_file); |
| 47 | bool IsFile(const std::string& file); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 48 | bool IsFolder(const std::string& file); |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 49 | absl::optional<size_t> GetFileSize(const std::string& file); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 50 | |
| 51 | #if defined(WEBRTC_WIN) |
| 52 | |
| 53 | std::string AddTrailingPathDelimiterIfNeeded(std::string directory) { |
| 54 | if (absl::EndsWith(directory, "\\")) { |
| 55 | return directory; |
| 56 | } |
| 57 | return directory + "\\"; |
| 58 | } |
| 59 | |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 60 | std::vector<std::string> GetFilesWithPrefix(const std::string& directory, |
| 61 | const std::string& prefix) { |
| 62 | RTC_DCHECK(absl::EndsWith(directory, "\\")); |
| 63 | WIN32_FIND_DATA data; |
| 64 | HANDLE handle; |
| 65 | handle = ::FindFirstFile(ToUtf16(directory + prefix + '*').c_str(), &data); |
| 66 | if (handle == INVALID_HANDLE_VALUE) |
| 67 | return {}; |
| 68 | |
| 69 | std::vector<std::string> file_list; |
| 70 | do { |
| 71 | file_list.emplace_back(directory + ToUtf8(data.cFileName)); |
| 72 | } while (::FindNextFile(handle, &data) == TRUE); |
| 73 | |
| 74 | ::FindClose(handle); |
| 75 | return file_list; |
| 76 | } |
| 77 | |
| 78 | bool DeleteFile(const std::string& file) { |
| 79 | return ::DeleteFile(ToUtf16(file).c_str()) != 0; |
| 80 | } |
| 81 | |
| 82 | bool MoveFile(const std::string& old_file, const std::string& new_file) { |
| 83 | return ::MoveFile(ToUtf16(old_file).c_str(), ToUtf16(new_file).c_str()) != 0; |
| 84 | } |
| 85 | |
| 86 | bool IsFile(const std::string& file) { |
| 87 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 88 | if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard, |
| 89 | &data)) |
| 90 | return false; |
| 91 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; |
| 92 | } |
| 93 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 94 | bool IsFolder(const std::string& file) { |
| 95 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 96 | if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard, |
| 97 | &data)) |
| 98 | return false; |
| 99 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == |
| 100 | FILE_ATTRIBUTE_DIRECTORY; |
| 101 | } |
| 102 | |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 103 | absl::optional<size_t> GetFileSize(const std::string& file) { |
| 104 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 105 | if (::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard, |
| 106 | &data) == 0) |
| 107 | return absl::nullopt; |
| 108 | return data.nFileSizeLow; |
| 109 | } |
| 110 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 111 | #else // defined(WEBRTC_WIN) |
| 112 | |
| 113 | std::string AddTrailingPathDelimiterIfNeeded(std::string directory) { |
| 114 | if (absl::EndsWith(directory, "/")) { |
| 115 | return directory; |
| 116 | } |
| 117 | return directory + "/"; |
| 118 | } |
| 119 | |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 120 | std::vector<std::string> GetFilesWithPrefix(const std::string& directory, |
| 121 | const std::string& prefix) { |
| 122 | RTC_DCHECK(absl::EndsWith(directory, "/")); |
| 123 | DIR* dir = ::opendir(directory.c_str()); |
| 124 | if (dir == nullptr) |
| 125 | return {}; |
| 126 | std::vector<std::string> file_list; |
| 127 | for (struct dirent* dirent = ::readdir(dir); dirent; |
| 128 | dirent = ::readdir(dir)) { |
| 129 | std::string name = dirent->d_name; |
| 130 | if (name.compare(0, prefix.size(), prefix) == 0) { |
| 131 | file_list.emplace_back(directory + name); |
| 132 | } |
| 133 | } |
| 134 | ::closedir(dir); |
| 135 | return file_list; |
| 136 | } |
| 137 | |
| 138 | bool DeleteFile(const std::string& file) { |
| 139 | return ::unlink(file.c_str()) == 0; |
| 140 | } |
| 141 | |
| 142 | bool MoveFile(const std::string& old_file, const std::string& new_file) { |
| 143 | return ::rename(old_file.c_str(), new_file.c_str()) == 0; |
| 144 | } |
| 145 | |
| 146 | bool IsFile(const std::string& file) { |
| 147 | struct stat st; |
| 148 | int res = ::stat(file.c_str(), &st); |
| 149 | // Treat symlinks, named pipes, etc. all as files. |
| 150 | return res == 0 && !S_ISDIR(st.st_mode); |
| 151 | } |
| 152 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 153 | bool IsFolder(const std::string& file) { |
| 154 | struct stat st; |
| 155 | int res = ::stat(file.c_str(), &st); |
| 156 | return res == 0 && S_ISDIR(st.st_mode); |
| 157 | } |
| 158 | |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 159 | absl::optional<size_t> GetFileSize(const std::string& file) { |
| 160 | struct stat st; |
| 161 | if (::stat(file.c_str(), &st) != 0) |
| 162 | return absl::nullopt; |
| 163 | return st.st_size; |
| 164 | } |
| 165 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 166 | #endif |
| 167 | |
| 168 | } // namespace |
| 169 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 170 | FileRotatingStream::FileRotatingStream(const std::string& dir_path, |
| 171 | const std::string& file_prefix) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 172 | : FileRotatingStream(dir_path, file_prefix, 0, 0, kRead) {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 173 | |
| 174 | FileRotatingStream::FileRotatingStream(const std::string& dir_path, |
| 175 | const std::string& file_prefix, |
| 176 | size_t max_file_size, |
| 177 | size_t num_files) |
| 178 | : FileRotatingStream(dir_path, |
| 179 | file_prefix, |
| 180 | max_file_size, |
| 181 | num_files, |
| 182 | kWrite) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 183 | RTC_DCHECK_GT(max_file_size, 0); |
| 184 | RTC_DCHECK_GT(num_files, 1); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | FileRotatingStream::FileRotatingStream(const std::string& dir_path, |
| 188 | const std::string& file_prefix, |
| 189 | size_t max_file_size, |
| 190 | size_t num_files, |
| 191 | Mode mode) |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 192 | : dir_path_(AddTrailingPathDelimiterIfNeeded(dir_path)), |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 193 | file_prefix_(file_prefix), |
| 194 | mode_(mode), |
| 195 | file_stream_(nullptr), |
| 196 | max_file_size_(max_file_size), |
| 197 | current_file_index_(0), |
| 198 | rotation_index_(0), |
| 199 | current_bytes_written_(0), |
| 200 | disable_buffering_(false) { |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 201 | RTC_DCHECK(IsFolder(dir_path)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 202 | switch (mode) { |
| 203 | case kWrite: { |
| 204 | file_names_.clear(); |
| 205 | for (size_t i = 0; i < num_files; ++i) { |
| 206 | file_names_.push_back(GetFilePath(i, num_files)); |
| 207 | } |
| 208 | rotation_index_ = num_files - 1; |
| 209 | break; |
| 210 | } |
| 211 | case kRead: { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 212 | file_names_ = GetFilesWithPrefix(dir_path_, file_prefix_); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 213 | std::sort(file_names_.begin(), file_names_.end()); |
| 214 | if (file_names_.size() > 0) { |
| 215 | // |file_names_| is sorted newest first, so read from the end. |
| 216 | current_file_index_ = file_names_.size() - 1; |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 223 | FileRotatingStream::~FileRotatingStream() {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 224 | |
| 225 | StreamState FileRotatingStream::GetState() const { |
| 226 | if (mode_ == kRead && current_file_index_ < file_names_.size()) { |
| 227 | return SS_OPEN; |
| 228 | } |
| 229 | if (!file_stream_) { |
| 230 | return SS_CLOSED; |
| 231 | } |
| 232 | return file_stream_->GetState(); |
| 233 | } |
| 234 | |
| 235 | StreamResult FileRotatingStream::Read(void* buffer, |
| 236 | size_t buffer_len, |
| 237 | size_t* read, |
| 238 | int* error) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 239 | RTC_DCHECK(buffer); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 240 | if (mode_ != kRead) { |
| 241 | return SR_EOS; |
| 242 | } |
| 243 | if (current_file_index_ >= file_names_.size()) { |
| 244 | return SR_EOS; |
| 245 | } |
| 246 | // We will have no file stream initially, and when we are finished with the |
| 247 | // previous file. |
| 248 | if (!file_stream_) { |
| 249 | if (!OpenCurrentFile()) { |
| 250 | return SR_ERROR; |
| 251 | } |
| 252 | } |
| 253 | int local_error = 0; |
| 254 | if (!error) { |
| 255 | error = &local_error; |
| 256 | } |
| 257 | StreamResult result = file_stream_->Read(buffer, buffer_len, read, error); |
| 258 | if (result == SR_EOS || result == SR_ERROR) { |
| 259 | if (result == SR_ERROR) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 260 | RTC_LOG(LS_ERROR) << "Failed to read from: " |
| 261 | << file_names_[current_file_index_] |
| 262 | << "Error: " << error; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 263 | } |
| 264 | // Reached the end of the file, read next file. If there is an error return |
| 265 | // the error status but allow for a next read by reading next file. |
| 266 | CloseCurrentFile(); |
| 267 | if (current_file_index_ == 0) { |
| 268 | // Just finished reading the last file, signal EOS by setting index. |
| 269 | current_file_index_ = file_names_.size(); |
| 270 | } else { |
| 271 | --current_file_index_; |
| 272 | } |
| 273 | if (read) { |
| 274 | *read = 0; |
| 275 | } |
| 276 | return result == SR_EOS ? SR_SUCCESS : result; |
| 277 | } else if (result == SR_SUCCESS) { |
| 278 | // Succeeded, continue reading from this file. |
| 279 | return SR_SUCCESS; |
| 280 | } else { |
| 281 | RTC_NOTREACHED(); |
| 282 | } |
| 283 | return result; |
| 284 | } |
| 285 | |
| 286 | StreamResult FileRotatingStream::Write(const void* data, |
| 287 | size_t data_len, |
| 288 | size_t* written, |
| 289 | int* error) { |
| 290 | if (mode_ != kWrite) { |
| 291 | return SR_EOS; |
| 292 | } |
| 293 | if (!file_stream_) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 294 | std::fprintf(stderr, "Open() must be called before Write.\n"); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 295 | return SR_ERROR; |
| 296 | } |
| 297 | // Write as much as will fit in to the current file. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 298 | RTC_DCHECK_LT(current_bytes_written_, max_file_size_); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 299 | size_t remaining_bytes = max_file_size_ - current_bytes_written_; |
| 300 | size_t write_length = std::min(data_len, remaining_bytes); |
| 301 | size_t local_written = 0; |
| 302 | if (!written) { |
| 303 | written = &local_written; |
| 304 | } |
| 305 | StreamResult result = file_stream_->Write(data, write_length, written, error); |
| 306 | current_bytes_written_ += *written; |
| 307 | |
| 308 | // If we're done with this file, rotate it out. |
| 309 | if (current_bytes_written_ >= max_file_size_) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 310 | RTC_DCHECK_EQ(current_bytes_written_, max_file_size_); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 311 | RotateFiles(); |
| 312 | } |
| 313 | return result; |
| 314 | } |
| 315 | |
| 316 | bool FileRotatingStream::Flush() { |
| 317 | if (!file_stream_) { |
| 318 | return false; |
| 319 | } |
| 320 | return file_stream_->Flush(); |
| 321 | } |
| 322 | |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 323 | bool FileRotatingStream::GetSize(size_t* size) const { |
| 324 | if (mode_ != kRead) { |
| 325 | // Not possible to get accurate size on disk when writing because of |
| 326 | // potential buffering. |
| 327 | return false; |
| 328 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 329 | RTC_DCHECK(size); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 330 | *size = 0; |
| 331 | size_t total_size = 0; |
| 332 | for (auto file_name : file_names_) { |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 333 | total_size += GetFileSize(file_name).value_or(0); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 334 | } |
| 335 | *size = total_size; |
| 336 | return true; |
| 337 | } |
| 338 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 339 | void FileRotatingStream::Close() { |
| 340 | CloseCurrentFile(); |
| 341 | } |
| 342 | |
| 343 | bool FileRotatingStream::Open() { |
| 344 | switch (mode_) { |
| 345 | case kRead: |
| 346 | // Defer opening to when we first read since we want to return read error |
| 347 | // if we fail to open next file. |
| 348 | return true; |
| 349 | case kWrite: { |
| 350 | // Delete existing files when opening for write. |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 351 | std::vector<std::string> matching_files = |
| 352 | GetFilesWithPrefix(dir_path_, file_prefix_); |
| 353 | for (const auto& matching_file : matching_files) { |
| 354 | if (!DeleteFile(matching_file)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 355 | std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | return OpenCurrentFile(); |
| 359 | } |
| 360 | } |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | bool FileRotatingStream::DisableBuffering() { |
| 365 | disable_buffering_ = true; |
| 366 | if (!file_stream_) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 367 | std::fprintf(stderr, "Open() must be called before DisableBuffering().\n"); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 368 | return false; |
| 369 | } |
| 370 | return file_stream_->DisableBuffering(); |
| 371 | } |
| 372 | |
| 373 | std::string FileRotatingStream::GetFilePath(size_t index) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 374 | RTC_DCHECK_LT(index, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 375 | return file_names_[index]; |
| 376 | } |
| 377 | |
| 378 | bool FileRotatingStream::OpenCurrentFile() { |
| 379 | CloseCurrentFile(); |
| 380 | |
| 381 | // Opens the appropriate file in the appropriate mode. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 382 | RTC_DCHECK_LT(current_file_index_, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 383 | std::string file_path = file_names_[current_file_index_]; |
| 384 | file_stream_.reset(new FileStream()); |
| 385 | const char* mode = nullptr; |
| 386 | switch (mode_) { |
| 387 | case kWrite: |
| 388 | mode = "w+"; |
| 389 | // We should always we writing to the zero-th file. |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 390 | RTC_DCHECK_EQ(current_file_index_, 0); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 391 | break; |
| 392 | case kRead: |
| 393 | mode = "r"; |
| 394 | break; |
| 395 | } |
| 396 | int error = 0; |
| 397 | if (!file_stream_->Open(file_path, mode, &error)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 398 | std::fprintf(stderr, "Failed to open: %s Error: %i\n", file_path.c_str(), |
| 399 | error); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 400 | file_stream_.reset(); |
| 401 | return false; |
| 402 | } |
| 403 | if (disable_buffering_) { |
| 404 | file_stream_->DisableBuffering(); |
| 405 | } |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | void FileRotatingStream::CloseCurrentFile() { |
| 410 | if (!file_stream_) { |
| 411 | return; |
| 412 | } |
| 413 | current_bytes_written_ = 0; |
| 414 | file_stream_.reset(); |
| 415 | } |
| 416 | |
| 417 | void FileRotatingStream::RotateFiles() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 418 | RTC_DCHECK_EQ(mode_, kWrite); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 419 | CloseCurrentFile(); |
| 420 | // Rotates the files by deleting the file at |rotation_index_|, which is the |
| 421 | // oldest file and then renaming the newer files to have an incremented index. |
| 422 | // See header file comments for example. |
haysc | d02b0fa | 2015-12-08 13:59:05 -0800 | [diff] [blame] | 423 | RTC_DCHECK_LT(rotation_index_, file_names_.size()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 424 | std::string file_to_delete = file_names_[rotation_index_]; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 425 | if (IsFile(file_to_delete)) { |
| 426 | if (!DeleteFile(file_to_delete)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 427 | std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | for (auto i = rotation_index_; i > 0; --i) { |
| 431 | std::string rotated_name = file_names_[i]; |
| 432 | std::string unrotated_name = file_names_[i - 1]; |
Niels Möller | 260770c | 2018-11-07 15:08:18 +0100 | [diff] [blame] | 433 | if (IsFile(unrotated_name)) { |
| 434 | if (!MoveFile(unrotated_name, rotated_name)) { |
Jonas Olsson | 55378f4 | 2018-05-25 10:23:10 +0200 | [diff] [blame] | 435 | std::fprintf(stderr, "Failed to move: %s to %s\n", |
| 436 | unrotated_name.c_str(), rotated_name.c_str()); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | // Create a new file for 0th index. |
| 441 | OpenCurrentFile(); |
| 442 | OnRotation(); |
| 443 | } |
| 444 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 445 | std::string FileRotatingStream::GetFilePath(size_t index, |
| 446 | size_t num_files) const { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 447 | RTC_DCHECK_LT(index, num_files); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 448 | |
Jonas Olsson | 671cae2 | 2018-06-14 09:57:39 +0200 | [diff] [blame] | 449 | const size_t buffer_size = 32; |
| 450 | char file_postfix[buffer_size]; |
| 451 | // We want to zero pad the index so that it will sort nicely. |
| 452 | const int max_digits = std::snprintf(nullptr, 0, "%zu", num_files - 1); |
| 453 | RTC_DCHECK_LT(1 + max_digits, buffer_size); |
| 454 | std::snprintf(file_postfix, buffer_size, "_%0*zu", max_digits, index); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 455 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame] | 456 | return dir_path_ + file_prefix_ + file_postfix; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | CallSessionFileRotatingStream::CallSessionFileRotatingStream( |
| 460 | const std::string& dir_path) |
| 461 | : FileRotatingStream(dir_path, kLogPrefix), |
| 462 | max_total_log_size_(0), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 463 | num_rotations_(0) {} |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 464 | |
| 465 | CallSessionFileRotatingStream::CallSessionFileRotatingStream( |
| 466 | const std::string& dir_path, |
| 467 | size_t max_total_log_size) |
| 468 | : FileRotatingStream(dir_path, |
| 469 | kLogPrefix, |
| 470 | max_total_log_size / 2, |
| 471 | GetNumRotatingLogFiles(max_total_log_size) + 1), |
| 472 | max_total_log_size_(max_total_log_size), |
| 473 | num_rotations_(0) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 474 | RTC_DCHECK_GE(max_total_log_size, 4); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | const char* CallSessionFileRotatingStream::kLogPrefix = "webrtc_log"; |
| 478 | const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize = |
| 479 | 1024 * 1024; |
| 480 | |
| 481 | void CallSessionFileRotatingStream::OnRotation() { |
| 482 | ++num_rotations_; |
| 483 | if (num_rotations_ == 1) { |
| 484 | // On the first rotation adjust the max file size so subsequent files after |
| 485 | // the first are smaller. |
| 486 | SetMaxFileSize(GetRotatingLogSize(max_total_log_size_)); |
| 487 | } else if (num_rotations_ == (GetNumFiles() - 1)) { |
| 488 | // On the next rotation the very first file is going to be deleted. Change |
| 489 | // the rotation index so this doesn't happen. |
| 490 | SetRotationIndex(GetRotationIndex() - 1); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | size_t CallSessionFileRotatingStream::GetRotatingLogSize( |
| 495 | size_t max_total_log_size) { |
| 496 | size_t num_rotating_log_files = GetNumRotatingLogFiles(max_total_log_size); |
| 497 | size_t rotating_log_size = num_rotating_log_files > 2 |
| 498 | ? kRotatingLogFileDefaultSize |
| 499 | : max_total_log_size / 4; |
| 500 | return rotating_log_size; |
| 501 | } |
| 502 | |
| 503 | size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles( |
| 504 | size_t max_total_log_size) { |
| 505 | // At minimum have two rotating files. Otherwise split the available log size |
| 506 | // evenly across 1MB files. |
| 507 | return std::max((size_t)2, |
| 508 | (max_total_log_size / 2) / kRotatingLogFileDefaultSize); |
| 509 | } |
| 510 | |
| 511 | } // namespace rtc |