blob: c9a663aafff869f3706f1fddde38502b4d840559 [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)
Yves Gerey665174f2018-06-19 15:03:05 +020029 : FileRotatingStream(dir_path, file_prefix, 0, 0, kRead) {}
tkchin93411912015-07-22 12:12:17 -070030
31FileRotatingStream::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) {
kwibergaf476c72016-11-28 15:21:39 -080040 RTC_DCHECK_GT(max_file_size, 0);
41 RTC_DCHECK_GT(num_files, 1);
tkchin93411912015-07-22 12:12:17 -070042}
43
44FileRotatingStream::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) {
henrikg91d6ede2015-09-17 00:24:34 -070058 RTC_DCHECK(Filesystem::IsFolder(dir_path));
tkchin93411912015-07-22 12:12:17 -070059 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
Yves Gerey665174f2018-06-19 15:03:05 +020080FileRotatingStream::~FileRotatingStream() {}
tkchin93411912015-07-22 12:12:17 -070081
82StreamState FileRotatingStream::GetState() const {
83 if (mode_ == kRead && current_file_index_ < file_names_.size()) {
84 return SS_OPEN;
85 }
86 if (!file_stream_) {
87 return SS_CLOSED;
88 }
89 return file_stream_->GetState();
90}
91
92StreamResult FileRotatingStream::Read(void* buffer,
93 size_t buffer_len,
94 size_t* read,
95 int* error) {
henrikg91d6ede2015-09-17 00:24:34 -070096 RTC_DCHECK(buffer);
tkchin93411912015-07-22 12:12:17 -070097 if (mode_ != kRead) {
98 return SR_EOS;
99 }
100 if (current_file_index_ >= file_names_.size()) {
101 return SR_EOS;
102 }
103 // We will have no file stream initially, and when we are finished with the
104 // previous file.
105 if (!file_stream_) {
106 if (!OpenCurrentFile()) {
107 return SR_ERROR;
108 }
109 }
110 int local_error = 0;
111 if (!error) {
112 error = &local_error;
113 }
114 StreamResult result = file_stream_->Read(buffer, buffer_len, read, error);
115 if (result == SR_EOS || result == SR_ERROR) {
116 if (result == SR_ERROR) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100117 RTC_LOG(LS_ERROR) << "Failed to read from: "
118 << file_names_[current_file_index_]
119 << "Error: " << error;
tkchin93411912015-07-22 12:12:17 -0700120 }
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
143StreamResult 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_) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200151 std::fprintf(stderr, "Open() must be called before Write.\n");
tkchin93411912015-07-22 12:12:17 -0700152 return SR_ERROR;
153 }
154 // Write as much as will fit in to the current file.
henrikg91d6ede2015-09-17 00:24:34 -0700155 RTC_DCHECK_LT(current_bytes_written_, max_file_size_);
tkchin93411912015-07-22 12:12:17 -0700156 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_) {
henrikg91d6ede2015-09-17 00:24:34 -0700167 RTC_DCHECK_EQ(current_bytes_written_, max_file_size_);
tkchin93411912015-07-22 12:12:17 -0700168 RotateFiles();
169 }
170 return result;
171}
172
173bool FileRotatingStream::Flush() {
174 if (!file_stream_) {
175 return false;
176 }
177 return file_stream_->Flush();
178}
179
tkchin28bae022015-07-23 12:27:02 -0700180bool FileRotatingStream::GetSize(size_t* size) const {
181 if (mode_ != kRead) {
182 // Not possible to get accurate size on disk when writing because of
183 // potential buffering.
184 return false;
185 }
henrikg91d6ede2015-09-17 00:24:34 -0700186 RTC_DCHECK(size);
tkchin28bae022015-07-23 12:27:02 -0700187 *size = 0;
188 size_t total_size = 0;
189 for (auto file_name : file_names_) {
190 Pathname pathname(file_name);
191 size_t file_size = 0;
192 if (Filesystem::GetFileSize(file_name, &file_size)) {
193 total_size += file_size;
194 }
195 }
196 *size = total_size;
197 return true;
198}
199
tkchin93411912015-07-22 12:12:17 -0700200void FileRotatingStream::Close() {
201 CloseCurrentFile();
202}
203
204bool FileRotatingStream::Open() {
205 switch (mode_) {
206 case kRead:
207 // Defer opening to when we first read since we want to return read error
208 // if we fail to open next file.
209 return true;
210 case kWrite: {
211 // Delete existing files when opening for write.
212 std::vector<std::string> matching_files = GetFilesWithPrefix();
213 for (auto matching_file : matching_files) {
214 if (!Filesystem::DeleteFile(matching_file)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200215 std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str());
tkchin93411912015-07-22 12:12:17 -0700216 }
217 }
218 return OpenCurrentFile();
219 }
220 }
221 return false;
222}
223
224bool FileRotatingStream::DisableBuffering() {
225 disable_buffering_ = true;
226 if (!file_stream_) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200227 std::fprintf(stderr, "Open() must be called before DisableBuffering().\n");
tkchin93411912015-07-22 12:12:17 -0700228 return false;
229 }
230 return file_stream_->DisableBuffering();
231}
232
233std::string FileRotatingStream::GetFilePath(size_t index) const {
henrikg91d6ede2015-09-17 00:24:34 -0700234 RTC_DCHECK_LT(index, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700235 return file_names_[index];
236}
237
238bool FileRotatingStream::OpenCurrentFile() {
239 CloseCurrentFile();
240
241 // Opens the appropriate file in the appropriate mode.
henrikg91d6ede2015-09-17 00:24:34 -0700242 RTC_DCHECK_LT(current_file_index_, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700243 std::string file_path = file_names_[current_file_index_];
244 file_stream_.reset(new FileStream());
245 const char* mode = nullptr;
246 switch (mode_) {
247 case kWrite:
248 mode = "w+";
249 // We should always we writing to the zero-th file.
kwibergaf476c72016-11-28 15:21:39 -0800250 RTC_DCHECK_EQ(current_file_index_, 0);
tkchin93411912015-07-22 12:12:17 -0700251 break;
252 case kRead:
253 mode = "r";
254 break;
255 }
256 int error = 0;
257 if (!file_stream_->Open(file_path, mode, &error)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200258 std::fprintf(stderr, "Failed to open: %s Error: %i\n", file_path.c_str(),
259 error);
tkchin93411912015-07-22 12:12:17 -0700260 file_stream_.reset();
261 return false;
262 }
263 if (disable_buffering_) {
264 file_stream_->DisableBuffering();
265 }
266 return true;
267}
268
269void FileRotatingStream::CloseCurrentFile() {
270 if (!file_stream_) {
271 return;
272 }
273 current_bytes_written_ = 0;
274 file_stream_.reset();
275}
276
277void FileRotatingStream::RotateFiles() {
henrikg91d6ede2015-09-17 00:24:34 -0700278 RTC_DCHECK_EQ(mode_, kWrite);
tkchin93411912015-07-22 12:12:17 -0700279 CloseCurrentFile();
280 // Rotates the files by deleting the file at |rotation_index_|, which is the
281 // oldest file and then renaming the newer files to have an incremented index.
282 // See header file comments for example.
hayscd02b0fa2015-12-08 13:59:05 -0800283 RTC_DCHECK_LT(rotation_index_, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700284 std::string file_to_delete = file_names_[rotation_index_];
285 if (Filesystem::IsFile(file_to_delete)) {
286 if (!Filesystem::DeleteFile(file_to_delete)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200287 std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str());
tkchin93411912015-07-22 12:12:17 -0700288 }
289 }
290 for (auto i = rotation_index_; i > 0; --i) {
291 std::string rotated_name = file_names_[i];
292 std::string unrotated_name = file_names_[i - 1];
293 if (Filesystem::IsFile(unrotated_name)) {
294 if (!Filesystem::MoveFile(unrotated_name, rotated_name)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200295 std::fprintf(stderr, "Failed to move: %s to %s\n",
296 unrotated_name.c_str(), rotated_name.c_str());
tkchin93411912015-07-22 12:12:17 -0700297 }
298 }
299 }
300 // Create a new file for 0th index.
301 OpenCurrentFile();
302 OnRotation();
303}
304
305std::vector<std::string> FileRotatingStream::GetFilesWithPrefix() const {
306 std::vector<std::string> files;
307 // Iterate over the files in the directory.
308 DirectoryIterator it;
309 Pathname dir_path;
310 dir_path.SetFolder(dir_path_);
311 if (!it.Iterate(dir_path)) {
312 return files;
313 }
314 do {
315 std::string current_name = it.Name();
316 if (current_name.size() && !it.IsDirectory() &&
317 current_name.compare(0, file_prefix_.size(), file_prefix_) == 0) {
318 Pathname path(dir_path_, current_name);
319 files.push_back(path.pathname());
320 }
321 } while (it.Next());
322 return files;
323}
324
325std::string FileRotatingStream::GetFilePath(size_t index,
326 size_t num_files) const {
henrikg91d6ede2015-09-17 00:24:34 -0700327 RTC_DCHECK_LT(index, num_files);
tkchin93411912015-07-22 12:12:17 -0700328
Jonas Olsson671cae22018-06-14 09:57:39 +0200329 const size_t buffer_size = 32;
330 char file_postfix[buffer_size];
331 // We want to zero pad the index so that it will sort nicely.
332 const int max_digits = std::snprintf(nullptr, 0, "%zu", num_files - 1);
333 RTC_DCHECK_LT(1 + max_digits, buffer_size);
334 std::snprintf(file_postfix, buffer_size, "_%0*zu", max_digits, index);
tkchin93411912015-07-22 12:12:17 -0700335
Jonas Olsson671cae22018-06-14 09:57:39 +0200336 Pathname file_path(dir_path_, file_prefix_ + file_postfix);
tkchin93411912015-07-22 12:12:17 -0700337 return file_path.pathname();
338}
339
340CallSessionFileRotatingStream::CallSessionFileRotatingStream(
341 const std::string& dir_path)
342 : FileRotatingStream(dir_path, kLogPrefix),
343 max_total_log_size_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200344 num_rotations_(0) {}
tkchin93411912015-07-22 12:12:17 -0700345
346CallSessionFileRotatingStream::CallSessionFileRotatingStream(
347 const std::string& dir_path,
348 size_t max_total_log_size)
349 : FileRotatingStream(dir_path,
350 kLogPrefix,
351 max_total_log_size / 2,
352 GetNumRotatingLogFiles(max_total_log_size) + 1),
353 max_total_log_size_(max_total_log_size),
354 num_rotations_(0) {
kwibergaf476c72016-11-28 15:21:39 -0800355 RTC_DCHECK_GE(max_total_log_size, 4);
tkchin93411912015-07-22 12:12:17 -0700356}
357
358const char* CallSessionFileRotatingStream::kLogPrefix = "webrtc_log";
359const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize =
360 1024 * 1024;
361
362void CallSessionFileRotatingStream::OnRotation() {
363 ++num_rotations_;
364 if (num_rotations_ == 1) {
365 // On the first rotation adjust the max file size so subsequent files after
366 // the first are smaller.
367 SetMaxFileSize(GetRotatingLogSize(max_total_log_size_));
368 } else if (num_rotations_ == (GetNumFiles() - 1)) {
369 // On the next rotation the very first file is going to be deleted. Change
370 // the rotation index so this doesn't happen.
371 SetRotationIndex(GetRotationIndex() - 1);
372 }
373}
374
375size_t CallSessionFileRotatingStream::GetRotatingLogSize(
376 size_t max_total_log_size) {
377 size_t num_rotating_log_files = GetNumRotatingLogFiles(max_total_log_size);
378 size_t rotating_log_size = num_rotating_log_files > 2
379 ? kRotatingLogFileDefaultSize
380 : max_total_log_size / 4;
381 return rotating_log_size;
382}
383
384size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles(
385 size_t max_total_log_size) {
386 // At minimum have two rotating files. Otherwise split the available log size
387 // evenly across 1MB files.
388 return std::max((size_t)2,
389 (max_total_log_size / 2) / kRotatingLogFileDefaultSize);
390}
391
392} // namespace rtc