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 #ifndef RTC_BASE_FILE_ROTATING_STREAM_H_ 12 #define RTC_BASE_FILE_ROTATING_STREAM_H_ 13 14 #include <stddef.h> 15 16 #include <memory> 17 #include <string> 18 #include <vector> 19 20 #include "absl/strings/string_view.h" 21 #include "rtc_base/system/file_wrapper.h" 22 23 namespace rtc { 24 25 // FileRotatingStream writes to a file in the directory specified in the 26 // constructor. It rotates the files once the current file is full. The 27 // individual file size and the number of files used is configurable in the 28 // constructor. Open() must be called before using this stream. 29 class FileRotatingStream { 30 public: 31 // Use this constructor for writing to a directory. Files in the directory 32 // matching the prefix will be deleted on open. 33 FileRotatingStream(absl::string_view dir_path, 34 absl::string_view file_prefix, 35 size_t max_file_size, 36 size_t num_files); 37 38 virtual ~FileRotatingStream(); 39 40 FileRotatingStream(const FileRotatingStream&) = delete; 41 FileRotatingStream& operator=(const FileRotatingStream&) = delete; 42 43 bool IsOpen() const; 44 45 bool Write(const void* data, size_t data_len); 46 bool Flush(); 47 void Close(); 48 49 // Opens the appropriate file(s). Call this before using the stream. 50 bool Open(); 51 52 // Disabling buffering causes writes to block until disk is updated. This is 53 // enabled by default for performance. 54 bool DisableBuffering(); 55 56 // Below two methods are public for testing only. 57 58 // Returns the path used for the i-th newest file, where the 0th file is the 59 // newest file. The file may or may not exist, this is just used for 60 // formatting. Index must be less than GetNumFiles(). 61 std::string GetFilePath(size_t index) const; 62 63 // Returns the number of files that will used by this stream. GetNumFiles()64 size_t GetNumFiles() const { return file_names_.size(); } 65 66 protected: SetMaxFileSize(size_t size)67 void SetMaxFileSize(size_t size) { max_file_size_ = size; } 68 GetRotationIndex()69 size_t GetRotationIndex() const { return rotation_index_; } 70 SetRotationIndex(size_t index)71 void SetRotationIndex(size_t index) { rotation_index_ = index; } 72 OnRotation()73 virtual void OnRotation() {} 74 75 private: 76 bool OpenCurrentFile(); 77 void CloseCurrentFile(); 78 79 // Rotates the files by creating a new current file, renaming the 80 // existing files, and deleting the oldest one. e.g. 81 // file_0 -> file_1 82 // file_1 -> file_2 83 // file_2 -> delete 84 // create new file_0 85 void RotateFiles(); 86 87 // Private version of GetFilePath. 88 std::string GetFilePath(size_t index, size_t num_files) const; 89 90 const std::string dir_path_; 91 const std::string file_prefix_; 92 93 // File we're currently writing to. 94 webrtc::FileWrapper file_; 95 // Convenience storage for file names so we don't generate them over and over. 96 std::vector<std::string> file_names_; 97 size_t max_file_size_; 98 size_t current_file_index_; 99 // The rotation index indicates the index of the file that will be 100 // deleted first on rotation. Indices lower than this index will be rotated. 101 size_t rotation_index_; 102 // Number of bytes written to current file. We need this because with 103 // buffering the file size read from disk might not be accurate. 104 size_t current_bytes_written_; 105 bool disable_buffering_; 106 }; 107 108 // CallSessionFileRotatingStream is meant to be used in situations where we will 109 // have limited disk space. Its purpose is to write logs up to a 110 // maximum size. Once the maximum size is exceeded, logs from the middle are 111 // deleted whereas logs from the beginning and end are preserved. The reason for 112 // this is because we anticipate that in WebRTC the beginning and end of the 113 // logs are most useful for call diagnostics. 114 // 115 // This implementation simply writes to a single file until 116 // `max_total_log_size` / 2 bytes are written to it, and subsequently writes to 117 // a set of rotating files. We do this by inheriting FileRotatingStream and 118 // setting the appropriate internal variables so that we don't delete the last 119 // (earliest) file on rotate, and that that file's size is bigger. 120 // 121 // Open() must be called before using this stream. 122 123 // To read the logs produced by this class, one can use the companion class 124 // CallSessionFileRotatingStreamReader. 125 class CallSessionFileRotatingStream : public FileRotatingStream { 126 public: 127 // Use this constructor for writing to a directory. Files in the directory 128 // matching what's used by the stream will be deleted. `max_total_log_size` 129 // must be at least 4. 130 CallSessionFileRotatingStream(absl::string_view dir_path, 131 size_t max_total_log_size); ~CallSessionFileRotatingStream()132 ~CallSessionFileRotatingStream() override {} 133 134 CallSessionFileRotatingStream(const CallSessionFileRotatingStream&) = delete; 135 CallSessionFileRotatingStream& operator=( 136 const CallSessionFileRotatingStream&) = delete; 137 138 protected: 139 void OnRotation() override; 140 141 private: 142 static size_t GetRotatingLogSize(size_t max_total_log_size); 143 static size_t GetNumRotatingLogFiles(size_t max_total_log_size); 144 static const size_t kRotatingLogFileDefaultSize; 145 146 const size_t max_total_log_size_; 147 size_t num_rotations_; 148 }; 149 150 // This is a convenience class, to read all files produced by a 151 // FileRotatingStream, all in one go. Typical use calls GetSize and ReadData 152 // only once. The list of file names to read is based on the contents of the log 153 // directory at construction time. 154 class FileRotatingStreamReader { 155 public: 156 FileRotatingStreamReader(absl::string_view dir_path, 157 absl::string_view file_prefix); 158 ~FileRotatingStreamReader(); 159 size_t GetSize() const; 160 size_t ReadAll(void* buffer, size_t size) const; 161 162 private: 163 std::vector<std::string> file_names_; 164 }; 165 166 class CallSessionFileRotatingStreamReader : public FileRotatingStreamReader { 167 public: 168 CallSessionFileRotatingStreamReader(absl::string_view dir_path); 169 }; 170 171 } // namespace rtc 172 173 #endif // RTC_BASE_FILE_ROTATING_STREAM_H_ 174