OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "webrtc/base/filerotatingstream.h" |
| 12 |
| 13 #include <algorithm> |
| 14 #include <iostream> |
| 15 #include <string> |
| 16 |
| 17 #include "webrtc/base/checks.h" |
| 18 #include "webrtc/base/fileutils.h" |
| 19 #include "webrtc/base/pathutils.h" |
| 20 |
| 21 // Note: We use std::cerr for logging in the write paths of this stream to avoid |
| 22 // infinite loops when logging. |
| 23 |
| 24 namespace rtc { |
| 25 |
| 26 FileRotatingStream::FileRotatingStream(const std::string& dir_path, |
| 27 const std::string& file_prefix) |
| 28 : FileRotatingStream(dir_path, file_prefix, 0, 0, kRead) { |
| 29 } |
| 30 |
| 31 FileRotatingStream::FileRotatingStream(const std::string& dir_path, |
| 32 const std::string& file_prefix, |
| 33 size_t max_file_size, |
| 34 size_t num_files) |
| 35 : FileRotatingStream(dir_path, |
| 36 file_prefix, |
| 37 max_file_size, |
| 38 num_files, |
| 39 kWrite) { |
| 40 DCHECK_GT(max_file_size, 0u); |
| 41 DCHECK_GT(num_files, 1u); |
| 42 } |
| 43 |
| 44 FileRotatingStream::FileRotatingStream(const std::string& dir_path, |
| 45 const std::string& file_prefix, |
| 46 size_t max_file_size, |
| 47 size_t num_files, |
| 48 Mode mode) |
| 49 : dir_path_(dir_path), |
| 50 file_prefix_(file_prefix), |
| 51 mode_(mode), |
| 52 file_stream_(nullptr), |
| 53 max_file_size_(max_file_size), |
| 54 current_file_index_(0), |
| 55 rotation_index_(0), |
| 56 current_bytes_written_(0), |
| 57 disable_buffering_(false) { |
| 58 DCHECK(Filesystem::IsFolder(dir_path)); |
| 59 switch (mode) { |
| 60 case kWrite: { |
| 61 file_names_.clear(); |
| 62 for (size_t i = 0; i < num_files; ++i) { |
| 63 file_names_.push_back(GetFilePath(i, num_files)); |
| 64 } |
| 65 rotation_index_ = num_files - 1; |
| 66 break; |
| 67 } |
| 68 case kRead: { |
| 69 file_names_ = GetFilesWithPrefix(); |
| 70 std::sort(file_names_.begin(), file_names_.end()); |
| 71 if (file_names_.size() > 0) { |
| 72 // |file_names_| is sorted newest first, so read from the end. |
| 73 current_file_index_ = file_names_.size() - 1; |
| 74 } |
| 75 break; |
| 76 } |
| 77 } |
| 78 } |
| 79 |
| 80 FileRotatingStream::~FileRotatingStream() { |
| 81 } |
| 82 |
| 83 StreamState FileRotatingStream::GetState() const { |
| 84 if (mode_ == kRead && current_file_index_ < file_names_.size()) { |
| 85 return SS_OPEN; |
| 86 } |
| 87 if (!file_stream_) { |
| 88 return SS_CLOSED; |
| 89 } |
| 90 return file_stream_->GetState(); |
| 91 } |
| 92 |
| 93 StreamResult FileRotatingStream::Read(void* buffer, |
| 94 size_t buffer_len, |
| 95 size_t* read, |
| 96 int* error) { |
| 97 DCHECK(buffer); |
| 98 if (mode_ != kRead) { |
| 99 return SR_EOS; |
| 100 } |
| 101 if (current_file_index_ >= file_names_.size()) { |
| 102 return SR_EOS; |
| 103 } |
| 104 // We will have no file stream initially, and when we are finished with the |
| 105 // previous file. |
| 106 if (!file_stream_) { |
| 107 if (!OpenCurrentFile()) { |
| 108 return SR_ERROR; |
| 109 } |
| 110 } |
| 111 int local_error = 0; |
| 112 if (!error) { |
| 113 error = &local_error; |
| 114 } |
| 115 StreamResult result = file_stream_->Read(buffer, buffer_len, read, error); |
| 116 if (result == SR_EOS || result == SR_ERROR) { |
| 117 if (result == SR_ERROR) { |
| 118 LOG(LS_ERROR) << "Failed to read from: " |
| 119 << file_names_[current_file_index_] << "Error: " << error; |
| 120 } |
| 121 // Reached the end of the file, read next file. If there is an error return |
| 122 // the error status but allow for a next read by reading next file. |
| 123 CloseCurrentFile(); |
| 124 if (current_file_index_ == 0) { |
| 125 // Just finished reading the last file, signal EOS by setting index. |
| 126 current_file_index_ = file_names_.size(); |
| 127 } else { |
| 128 --current_file_index_; |
| 129 } |
| 130 if (read) { |
| 131 *read = 0; |
| 132 } |
| 133 return result == SR_EOS ? SR_SUCCESS : result; |
| 134 } else if (result == SR_SUCCESS) { |
| 135 // Succeeded, continue reading from this file. |
| 136 return SR_SUCCESS; |
| 137 } else { |
| 138 RTC_NOTREACHED(); |
| 139 } |
| 140 return result; |
| 141 } |
| 142 |
| 143 StreamResult FileRotatingStream::Write(const void* data, |
| 144 size_t data_len, |
| 145 size_t* written, |
| 146 int* error) { |
| 147 if (mode_ != kWrite) { |
| 148 return SR_EOS; |
| 149 } |
| 150 if (!file_stream_) { |
| 151 std::cerr << "Open() must be called before Write." << std::endl; |
| 152 return SR_ERROR; |
| 153 } |
| 154 // Write as much as will fit in to the current file. |
| 155 DCHECK_LT(current_bytes_written_, max_file_size_); |
| 156 size_t remaining_bytes = max_file_size_ - current_bytes_written_; |
| 157 size_t write_length = std::min(data_len, remaining_bytes); |
| 158 size_t local_written = 0; |
| 159 if (!written) { |
| 160 written = &local_written; |
| 161 } |
| 162 StreamResult result = file_stream_->Write(data, write_length, written, error); |
| 163 current_bytes_written_ += *written; |
| 164 |
| 165 // If we're done with this file, rotate it out. |
| 166 if (current_bytes_written_ >= max_file_size_) { |
| 167 DCHECK_EQ(current_bytes_written_, max_file_size_); |
| 168 RotateFiles(); |
| 169 } |
| 170 return result; |
| 171 } |
| 172 |
| 173 bool FileRotatingStream::Flush() { |
| 174 if (!file_stream_) { |
| 175 return false; |
| 176 } |
| 177 return file_stream_->Flush(); |
| 178 } |
| 179 |
| 180 void FileRotatingStream::Close() { |
| 181 CloseCurrentFile(); |
| 182 } |
| 183 |
| 184 bool FileRotatingStream::Open() { |
| 185 switch (mode_) { |
| 186 case kRead: |
| 187 // Defer opening to when we first read since we want to return read error |
| 188 // if we fail to open next file. |
| 189 return true; |
| 190 case kWrite: { |
| 191 // Delete existing files when opening for write. |
| 192 std::vector<std::string> matching_files = GetFilesWithPrefix(); |
| 193 for (auto matching_file : matching_files) { |
| 194 if (!Filesystem::DeleteFile(matching_file)) { |
| 195 std::cerr << "Failed to delete: " << matching_file << std::endl; |
| 196 } |
| 197 } |
| 198 return OpenCurrentFile(); |
| 199 } |
| 200 } |
| 201 return false; |
| 202 } |
| 203 |
| 204 bool FileRotatingStream::DisableBuffering() { |
| 205 disable_buffering_ = true; |
| 206 if (!file_stream_) { |
| 207 std::cerr << "Open() must be called before DisableBuffering()." |
| 208 << std::endl; |
| 209 return false; |
| 210 } |
| 211 return file_stream_->DisableBuffering(); |
| 212 } |
| 213 |
| 214 std::string FileRotatingStream::GetFilePath(size_t index) const { |
| 215 DCHECK_LT(index, file_names_.size()); |
| 216 return file_names_[index]; |
| 217 } |
| 218 |
| 219 bool FileRotatingStream::OpenCurrentFile() { |
| 220 CloseCurrentFile(); |
| 221 |
| 222 // Opens the appropriate file in the appropriate mode. |
| 223 DCHECK_LT(current_file_index_, file_names_.size()); |
| 224 std::string file_path = file_names_[current_file_index_]; |
| 225 file_stream_.reset(new FileStream()); |
| 226 const char* mode = nullptr; |
| 227 switch (mode_) { |
| 228 case kWrite: |
| 229 mode = "w+"; |
| 230 // We should always we writing to the zero-th file. |
| 231 DCHECK_EQ(current_file_index_, 0u); |
| 232 break; |
| 233 case kRead: |
| 234 mode = "r"; |
| 235 break; |
| 236 } |
| 237 int error = 0; |
| 238 if (!file_stream_->Open(file_path, mode, &error)) { |
| 239 std::cerr << "Failed to open: " << file_path << "Error: " << error |
| 240 << std::endl; |
| 241 file_stream_.reset(); |
| 242 return false; |
| 243 } |
| 244 if (disable_buffering_) { |
| 245 file_stream_->DisableBuffering(); |
| 246 } |
| 247 return true; |
| 248 } |
| 249 |
| 250 void FileRotatingStream::CloseCurrentFile() { |
| 251 if (!file_stream_) { |
| 252 return; |
| 253 } |
| 254 current_bytes_written_ = 0; |
| 255 file_stream_.reset(); |
| 256 } |
| 257 |
| 258 void FileRotatingStream::RotateFiles() { |
| 259 DCHECK_EQ(mode_, kWrite); |
| 260 CloseCurrentFile(); |
| 261 // Rotates the files by deleting the file at |rotation_index_|, which is the |
| 262 // oldest file and then renaming the newer files to have an incremented index. |
| 263 // See header file comments for example. |
| 264 DCHECK_LE(rotation_index_, file_names_.size()); |
| 265 std::string file_to_delete = file_names_[rotation_index_]; |
| 266 if (Filesystem::IsFile(file_to_delete)) { |
| 267 if (!Filesystem::DeleteFile(file_to_delete)) { |
| 268 std::cerr << "Failed to delete: " << file_to_delete << std::endl; |
| 269 } |
| 270 } |
| 271 for (auto i = rotation_index_; i > 0; --i) { |
| 272 std::string rotated_name = file_names_[i]; |
| 273 std::string unrotated_name = file_names_[i - 1]; |
| 274 if (Filesystem::IsFile(unrotated_name)) { |
| 275 if (!Filesystem::MoveFile(unrotated_name, rotated_name)) { |
| 276 std::cerr << "Failed to move: " << unrotated_name << " to " |
| 277 << rotated_name << std::endl; |
| 278 } |
| 279 } |
| 280 } |
| 281 // Create a new file for 0th index. |
| 282 OpenCurrentFile(); |
| 283 OnRotation(); |
| 284 } |
| 285 |
| 286 std::vector<std::string> FileRotatingStream::GetFilesWithPrefix() const { |
| 287 std::vector<std::string> files; |
| 288 // Iterate over the files in the directory. |
| 289 DirectoryIterator it; |
| 290 Pathname dir_path; |
| 291 dir_path.SetFolder(dir_path_); |
| 292 if (!it.Iterate(dir_path)) { |
| 293 return files; |
| 294 } |
| 295 do { |
| 296 std::string current_name = it.Name(); |
| 297 if (current_name.size() && !it.IsDirectory() && |
| 298 current_name.compare(0, file_prefix_.size(), file_prefix_) == 0) { |
| 299 Pathname path(dir_path_, current_name); |
| 300 files.push_back(path.pathname()); |
| 301 } |
| 302 } while (it.Next()); |
| 303 return files; |
| 304 } |
| 305 |
| 306 std::string FileRotatingStream::GetFilePath(size_t index, |
| 307 size_t num_files) const { |
| 308 DCHECK_LT(index, num_files); |
| 309 std::ostringstream file_name; |
| 310 // The format will be "_%<num_digits>zu". We want to zero pad the index so |
| 311 // that it will sort nicely. |
| 312 size_t max_digits = ((num_files - 1) / 10) + 1; |
| 313 size_t num_digits = (index / 10) + 1; |
| 314 DCHECK_LE(num_digits, max_digits); |
| 315 size_t padding = max_digits - num_digits; |
| 316 |
| 317 file_name << file_prefix_ << "_"; |
| 318 for (size_t i = 0; i < padding; ++i) { |
| 319 file_name << "0"; |
| 320 } |
| 321 file_name << index; |
| 322 |
| 323 Pathname file_path(dir_path_, file_name.str()); |
| 324 return file_path.pathname(); |
| 325 } |
| 326 |
| 327 CallSessionFileRotatingStream::CallSessionFileRotatingStream( |
| 328 const std::string& dir_path) |
| 329 : FileRotatingStream(dir_path, kLogPrefix), |
| 330 max_total_log_size_(0), |
| 331 num_rotations_(0) { |
| 332 } |
| 333 |
| 334 CallSessionFileRotatingStream::CallSessionFileRotatingStream( |
| 335 const std::string& dir_path, |
| 336 size_t max_total_log_size) |
| 337 : FileRotatingStream(dir_path, |
| 338 kLogPrefix, |
| 339 max_total_log_size / 2, |
| 340 GetNumRotatingLogFiles(max_total_log_size) + 1), |
| 341 max_total_log_size_(max_total_log_size), |
| 342 num_rotations_(0) { |
| 343 DCHECK_GE(max_total_log_size, 4u); |
| 344 } |
| 345 |
| 346 const char* CallSessionFileRotatingStream::kLogPrefix = "webrtc_log"; |
| 347 const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize = |
| 348 1024 * 1024; |
| 349 |
| 350 void CallSessionFileRotatingStream::OnRotation() { |
| 351 ++num_rotations_; |
| 352 if (num_rotations_ == 1) { |
| 353 // On the first rotation adjust the max file size so subsequent files after |
| 354 // the first are smaller. |
| 355 SetMaxFileSize(GetRotatingLogSize(max_total_log_size_)); |
| 356 } else if (num_rotations_ == (GetNumFiles() - 1)) { |
| 357 // On the next rotation the very first file is going to be deleted. Change |
| 358 // the rotation index so this doesn't happen. |
| 359 SetRotationIndex(GetRotationIndex() - 1); |
| 360 } |
| 361 } |
| 362 |
| 363 size_t CallSessionFileRotatingStream::GetRotatingLogSize( |
| 364 size_t max_total_log_size) { |
| 365 size_t num_rotating_log_files = GetNumRotatingLogFiles(max_total_log_size); |
| 366 size_t rotating_log_size = num_rotating_log_files > 2 |
| 367 ? kRotatingLogFileDefaultSize |
| 368 : max_total_log_size / 4; |
| 369 return rotating_log_size; |
| 370 } |
| 371 |
| 372 size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles( |
| 373 size_t max_total_log_size) { |
| 374 // At minimum have two rotating files. Otherwise split the available log size |
| 375 // evenly across 1MB files. |
| 376 return std::max((size_t)2, |
| 377 (max_total_log_size / 2) / kRotatingLogFileDefaultSize); |
| 378 } |
| 379 |
| 380 } // namespace rtc |
OLD | NEW |