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