blob: 7108d9d281beda3b5c1d8f494ed9f94de255dfd2 [file] [log] [blame]
tkchin93411912015-07-22 12:12:17 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/filerotatingstream.h"
tkchin93411912015-07-22 12:12:17 -070012
13#include <algorithm>
Jonas Olsson55378f42018-05-25 10:23:10 +020014#include <cstdio>
tkchin93411912015-07-22 12:12:17 -070015#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/fileutils.h"
19#include "rtc_base/pathutils.h"
Jonas Olsson55378f42018-05-25 10:23:10 +020020#include "rtc_base/strings/string_builder.h"
tkchin93411912015-07-22 12:12:17 -070021
Jonas Olsson55378f42018-05-25 10:23:10 +020022// Note: We use fprintf for logging in the write paths of this stream to avoid
tkchin93411912015-07-22 12:12:17 -070023// infinite loops when logging.
24
25namespace rtc {
26
27FileRotatingStream::FileRotatingStream(const std::string& dir_path,
28 const std::string& file_prefix)
29 : FileRotatingStream(dir_path, file_prefix, 0, 0, kRead) {
30}
31
32FileRotatingStream::FileRotatingStream(const std::string& dir_path,
33 const std::string& file_prefix,
34 size_t max_file_size,
35 size_t num_files)
36 : FileRotatingStream(dir_path,
37 file_prefix,
38 max_file_size,
39 num_files,
40 kWrite) {
kwibergaf476c72016-11-28 15:21:39 -080041 RTC_DCHECK_GT(max_file_size, 0);
42 RTC_DCHECK_GT(num_files, 1);
tkchin93411912015-07-22 12:12:17 -070043}
44
45FileRotatingStream::FileRotatingStream(const std::string& dir_path,
46 const std::string& file_prefix,
47 size_t max_file_size,
48 size_t num_files,
49 Mode mode)
50 : dir_path_(dir_path),
51 file_prefix_(file_prefix),
52 mode_(mode),
53 file_stream_(nullptr),
54 max_file_size_(max_file_size),
55 current_file_index_(0),
56 rotation_index_(0),
57 current_bytes_written_(0),
58 disable_buffering_(false) {
henrikg91d6ede2015-09-17 00:24:34 -070059 RTC_DCHECK(Filesystem::IsFolder(dir_path));
tkchin93411912015-07-22 12:12:17 -070060 switch (mode) {
61 case kWrite: {
62 file_names_.clear();
63 for (size_t i = 0; i < num_files; ++i) {
64 file_names_.push_back(GetFilePath(i, num_files));
65 }
66 rotation_index_ = num_files - 1;
67 break;
68 }
69 case kRead: {
70 file_names_ = GetFilesWithPrefix();
71 std::sort(file_names_.begin(), file_names_.end());
72 if (file_names_.size() > 0) {
73 // |file_names_| is sorted newest first, so read from the end.
74 current_file_index_ = file_names_.size() - 1;
75 }
76 break;
77 }
78 }
79}
80
81FileRotatingStream::~FileRotatingStream() {
82}
83
84StreamState FileRotatingStream::GetState() const {
85 if (mode_ == kRead && current_file_index_ < file_names_.size()) {
86 return SS_OPEN;
87 }
88 if (!file_stream_) {
89 return SS_CLOSED;
90 }
91 return file_stream_->GetState();
92}
93
94StreamResult FileRotatingStream::Read(void* buffer,
95 size_t buffer_len,
96 size_t* read,
97 int* error) {
henrikg91d6ede2015-09-17 00:24:34 -070098 RTC_DCHECK(buffer);
tkchin93411912015-07-22 12:12:17 -070099 if (mode_ != kRead) {
100 return SR_EOS;
101 }
102 if (current_file_index_ >= file_names_.size()) {
103 return SR_EOS;
104 }
105 // We will have no file stream initially, and when we are finished with the
106 // previous file.
107 if (!file_stream_) {
108 if (!OpenCurrentFile()) {
109 return SR_ERROR;
110 }
111 }
112 int local_error = 0;
113 if (!error) {
114 error = &local_error;
115 }
116 StreamResult result = file_stream_->Read(buffer, buffer_len, read, error);
117 if (result == SR_EOS || result == SR_ERROR) {
118 if (result == SR_ERROR) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100119 RTC_LOG(LS_ERROR) << "Failed to read from: "
120 << file_names_[current_file_index_]
121 << "Error: " << error;
tkchin93411912015-07-22 12:12:17 -0700122 }
123 // Reached the end of the file, read next file. If there is an error return
124 // the error status but allow for a next read by reading next file.
125 CloseCurrentFile();
126 if (current_file_index_ == 0) {
127 // Just finished reading the last file, signal EOS by setting index.
128 current_file_index_ = file_names_.size();
129 } else {
130 --current_file_index_;
131 }
132 if (read) {
133 *read = 0;
134 }
135 return result == SR_EOS ? SR_SUCCESS : result;
136 } else if (result == SR_SUCCESS) {
137 // Succeeded, continue reading from this file.
138 return SR_SUCCESS;
139 } else {
140 RTC_NOTREACHED();
141 }
142 return result;
143}
144
145StreamResult FileRotatingStream::Write(const void* data,
146 size_t data_len,
147 size_t* written,
148 int* error) {
149 if (mode_ != kWrite) {
150 return SR_EOS;
151 }
152 if (!file_stream_) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200153 std::fprintf(stderr, "Open() must be called before Write.\n");
tkchin93411912015-07-22 12:12:17 -0700154 return SR_ERROR;
155 }
156 // Write as much as will fit in to the current file.
henrikg91d6ede2015-09-17 00:24:34 -0700157 RTC_DCHECK_LT(current_bytes_written_, max_file_size_);
tkchin93411912015-07-22 12:12:17 -0700158 size_t remaining_bytes = max_file_size_ - current_bytes_written_;
159 size_t write_length = std::min(data_len, remaining_bytes);
160 size_t local_written = 0;
161 if (!written) {
162 written = &local_written;
163 }
164 StreamResult result = file_stream_->Write(data, write_length, written, error);
165 current_bytes_written_ += *written;
166
167 // If we're done with this file, rotate it out.
168 if (current_bytes_written_ >= max_file_size_) {
henrikg91d6ede2015-09-17 00:24:34 -0700169 RTC_DCHECK_EQ(current_bytes_written_, max_file_size_);
tkchin93411912015-07-22 12:12:17 -0700170 RotateFiles();
171 }
172 return result;
173}
174
175bool FileRotatingStream::Flush() {
176 if (!file_stream_) {
177 return false;
178 }
179 return file_stream_->Flush();
180}
181
tkchin28bae022015-07-23 12:27:02 -0700182bool FileRotatingStream::GetSize(size_t* size) const {
183 if (mode_ != kRead) {
184 // Not possible to get accurate size on disk when writing because of
185 // potential buffering.
186 return false;
187 }
henrikg91d6ede2015-09-17 00:24:34 -0700188 RTC_DCHECK(size);
tkchin28bae022015-07-23 12:27:02 -0700189 *size = 0;
190 size_t total_size = 0;
191 for (auto file_name : file_names_) {
192 Pathname pathname(file_name);
193 size_t file_size = 0;
194 if (Filesystem::GetFileSize(file_name, &file_size)) {
195 total_size += file_size;
196 }
197 }
198 *size = total_size;
199 return true;
200}
201
tkchin93411912015-07-22 12:12:17 -0700202void FileRotatingStream::Close() {
203 CloseCurrentFile();
204}
205
206bool FileRotatingStream::Open() {
207 switch (mode_) {
208 case kRead:
209 // Defer opening to when we first read since we want to return read error
210 // if we fail to open next file.
211 return true;
212 case kWrite: {
213 // Delete existing files when opening for write.
214 std::vector<std::string> matching_files = GetFilesWithPrefix();
215 for (auto matching_file : matching_files) {
216 if (!Filesystem::DeleteFile(matching_file)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200217 std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str());
tkchin93411912015-07-22 12:12:17 -0700218 }
219 }
220 return OpenCurrentFile();
221 }
222 }
223 return false;
224}
225
226bool FileRotatingStream::DisableBuffering() {
227 disable_buffering_ = true;
228 if (!file_stream_) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200229 std::fprintf(stderr, "Open() must be called before DisableBuffering().\n");
tkchin93411912015-07-22 12:12:17 -0700230 return false;
231 }
232 return file_stream_->DisableBuffering();
233}
234
235std::string FileRotatingStream::GetFilePath(size_t index) const {
henrikg91d6ede2015-09-17 00:24:34 -0700236 RTC_DCHECK_LT(index, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700237 return file_names_[index];
238}
239
240bool FileRotatingStream::OpenCurrentFile() {
241 CloseCurrentFile();
242
243 // Opens the appropriate file in the appropriate mode.
henrikg91d6ede2015-09-17 00:24:34 -0700244 RTC_DCHECK_LT(current_file_index_, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700245 std::string file_path = file_names_[current_file_index_];
246 file_stream_.reset(new FileStream());
247 const char* mode = nullptr;
248 switch (mode_) {
249 case kWrite:
250 mode = "w+";
251 // We should always we writing to the zero-th file.
kwibergaf476c72016-11-28 15:21:39 -0800252 RTC_DCHECK_EQ(current_file_index_, 0);
tkchin93411912015-07-22 12:12:17 -0700253 break;
254 case kRead:
255 mode = "r";
256 break;
257 }
258 int error = 0;
259 if (!file_stream_->Open(file_path, mode, &error)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200260 std::fprintf(stderr, "Failed to open: %s Error: %i\n", file_path.c_str(),
261 error);
tkchin93411912015-07-22 12:12:17 -0700262 file_stream_.reset();
263 return false;
264 }
265 if (disable_buffering_) {
266 file_stream_->DisableBuffering();
267 }
268 return true;
269}
270
271void FileRotatingStream::CloseCurrentFile() {
272 if (!file_stream_) {
273 return;
274 }
275 current_bytes_written_ = 0;
276 file_stream_.reset();
277}
278
279void FileRotatingStream::RotateFiles() {
henrikg91d6ede2015-09-17 00:24:34 -0700280 RTC_DCHECK_EQ(mode_, kWrite);
tkchin93411912015-07-22 12:12:17 -0700281 CloseCurrentFile();
282 // Rotates the files by deleting the file at |rotation_index_|, which is the
283 // oldest file and then renaming the newer files to have an incremented index.
284 // See header file comments for example.
hayscd02b0fa2015-12-08 13:59:05 -0800285 RTC_DCHECK_LT(rotation_index_, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700286 std::string file_to_delete = file_names_[rotation_index_];
287 if (Filesystem::IsFile(file_to_delete)) {
288 if (!Filesystem::DeleteFile(file_to_delete)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200289 std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str());
tkchin93411912015-07-22 12:12:17 -0700290 }
291 }
292 for (auto i = rotation_index_; i > 0; --i) {
293 std::string rotated_name = file_names_[i];
294 std::string unrotated_name = file_names_[i - 1];
295 if (Filesystem::IsFile(unrotated_name)) {
296 if (!Filesystem::MoveFile(unrotated_name, rotated_name)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200297 std::fprintf(stderr, "Failed to move: %s to %s\n",
298 unrotated_name.c_str(), rotated_name.c_str());
tkchin93411912015-07-22 12:12:17 -0700299 }
300 }
301 }
302 // Create a new file for 0th index.
303 OpenCurrentFile();
304 OnRotation();
305}
306
307std::vector<std::string> FileRotatingStream::GetFilesWithPrefix() const {
308 std::vector<std::string> files;
309 // Iterate over the files in the directory.
310 DirectoryIterator it;
311 Pathname dir_path;
312 dir_path.SetFolder(dir_path_);
313 if (!it.Iterate(dir_path)) {
314 return files;
315 }
316 do {
317 std::string current_name = it.Name();
318 if (current_name.size() && !it.IsDirectory() &&
319 current_name.compare(0, file_prefix_.size(), file_prefix_) == 0) {
320 Pathname path(dir_path_, current_name);
321 files.push_back(path.pathname());
322 }
323 } while (it.Next());
324 return files;
325}
326
327std::string FileRotatingStream::GetFilePath(size_t index,
328 size_t num_files) const {
henrikg91d6ede2015-09-17 00:24:34 -0700329 RTC_DCHECK_LT(index, num_files);
tkchin93411912015-07-22 12:12:17 -0700330
Jonas Olsson671cae22018-06-14 09:57:39 +0200331 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);
tkchin93411912015-07-22 12:12:17 -0700337
Jonas Olsson671cae22018-06-14 09:57:39 +0200338 Pathname file_path(dir_path_, file_prefix_ + file_postfix);
tkchin93411912015-07-22 12:12:17 -0700339 return file_path.pathname();
340}
341
342CallSessionFileRotatingStream::CallSessionFileRotatingStream(
343 const std::string& dir_path)
344 : FileRotatingStream(dir_path, kLogPrefix),
345 max_total_log_size_(0),
346 num_rotations_(0) {
347}
348
349CallSessionFileRotatingStream::CallSessionFileRotatingStream(
350 const std::string& dir_path,
351 size_t max_total_log_size)
352 : FileRotatingStream(dir_path,
353 kLogPrefix,
354 max_total_log_size / 2,
355 GetNumRotatingLogFiles(max_total_log_size) + 1),
356 max_total_log_size_(max_total_log_size),
357 num_rotations_(0) {
kwibergaf476c72016-11-28 15:21:39 -0800358 RTC_DCHECK_GE(max_total_log_size, 4);
tkchin93411912015-07-22 12:12:17 -0700359}
360
361const char* CallSessionFileRotatingStream::kLogPrefix = "webrtc_log";
362const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize =
363 1024 * 1024;
364
365void CallSessionFileRotatingStream::OnRotation() {
366 ++num_rotations_;
367 if (num_rotations_ == 1) {
368 // On the first rotation adjust the max file size so subsequent files after
369 // the first are smaller.
370 SetMaxFileSize(GetRotatingLogSize(max_total_log_size_));
371 } else if (num_rotations_ == (GetNumFiles() - 1)) {
372 // On the next rotation the very first file is going to be deleted. Change
373 // the rotation index so this doesn't happen.
374 SetRotationIndex(GetRotationIndex() - 1);
375 }
376}
377
378size_t CallSessionFileRotatingStream::GetRotatingLogSize(
379 size_t max_total_log_size) {
380 size_t num_rotating_log_files = GetNumRotatingLogFiles(max_total_log_size);
381 size_t rotating_log_size = num_rotating_log_files > 2
382 ? kRotatingLogFileDefaultSize
383 : max_total_log_size / 4;
384 return rotating_log_size;
385}
386
387size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles(
388 size_t max_total_log_size) {
389 // At minimum have two rotating files. Otherwise split the available log size
390 // evenly across 1MB files.
391 return std::max((size_t)2,
392 (max_total_log_size / 2) / kRotatingLogFileDefaultSize);
393}
394
395} // namespace rtc