Chromium Code Reviews| Index: webrtc/base/stream.h |
| diff --git a/webrtc/base/stream.h b/webrtc/base/stream.h |
| index f67c704aa5155f8ed64cbc2cd2a32e4c6b376efd..030f831bcdd33fb6f29a44dcafd3539d2c742843 100644 |
| --- a/webrtc/base/stream.h |
| +++ b/webrtc/base/stream.h |
| @@ -418,6 +418,151 @@ class FileStream : public StreamInterface { |
| DISALLOW_COPY_AND_ASSIGN(FileStream); |
| }; |
| +// FileRotatingStream writes to a file in the directory specified in the |
|
jiayl2
2015/07/20 23:27:06
I think the declaration and impl are big enough to
tkchin_webrtc
2015/07/21 20:39:13
Done.
|
| +// constructor. It rotates the files once the current file is full. The |
| +// individual file size and the number of files used is configurable in the |
| +// constructor. Open must be called before using this stream. |
| +class FileRotatingStream : public StreamInterface { |
| + public: |
| + // Use this constructor for reading a directory previously written to with |
| + // this stream. |
| + FileRotatingStream(const std::string& dir_path, |
| + const std::string& file_prefix); |
| + |
| + // Use this constructor for writing to a directory. Files in the directory |
| + // matching the prefix will be deleted on open. |
| + FileRotatingStream(const std::string& dir_path, |
| + const std::string& file_prefix, |
| + size_t max_file_size, |
| + size_t num_files); |
| + |
| + ~FileRotatingStream() override; |
| + |
| + // StreamInterface methods. |
| + StreamState GetState() const override; |
| + StreamResult Read(void* buffer, |
| + size_t buffer_len, |
| + size_t* read, |
| + int* error) override; |
| + StreamResult Write(const void* data, |
| + size_t data_len, |
| + size_t* written, |
| + int* error) override; |
| + bool Flush() override; |
| + void Close() override; |
| + |
| + // Opens the appropriate file(s). Call this before using the stream. |
| + bool Open(); |
| + |
| + // Disabling buffering causes writes to block until disk is updated. This is |
| + // enabled by default for performance. |
| + bool DisableBuffering(); |
| + |
| + // Returns the path used for the file at the i-th index. The file may or may |
| + // not exist, this is just used for formatting. Index must be less than |
| + // GetNumFiles(). |
| + std::string GetFilePath(size_t index) const; |
| + |
| + // Returns the number of files that will used by this stream. |
| + size_t GetNumFiles() { return file_names_.size(); } |
| + |
| + protected: |
| + size_t GetMaxFileSize() const { return max_file_size_; } |
| + |
| + void SetMaxFileSize(size_t size) { max_file_size_ = size; } |
| + |
| + size_t GetLastFileIndex() const { return last_file_index_; } |
| + |
| + void SetLastFileIndex(size_t index) { last_file_index_ = index; } |
| + |
| + virtual void OnRotation() {} |
| + |
| + private: |
| + enum Mode { kRead, kWrite }; |
| + |
| + FileRotatingStream(const std::string& dir_path, |
| + const std::string& file_prefix, |
| + size_t max_file_size, |
| + size_t num_files, |
| + Mode mode); |
| + |
| + // Opens the file at the current file index. Set |delete_existing| to true |
| + // to also delete files that begin with the prefix. |
| + bool Open(bool delete_existing); |
| + |
| + // Rotates the files by creating a new current file, renaming the |
| + // existing files, and deleting the oldest one. e.g. |
| + // file_0 -> file_1 |
| + // file_1 -> file_2 |
| + // file_2 -> delete |
| + // create new file_0 |
| + void RotateFiles(); |
| + |
| + // Returns a list of file names in the directory beginning with the prefix. |
| + std::vector<std::string> GetFilesWithPrefix() const; |
| + // Private version of GetFilePath. |
| + std::string GetFilePath(size_t index, size_t num_files) const; |
| + |
| + const std::string dir_path_; |
| + const std::string file_prefix_; |
| + const Mode mode_; |
| + |
| + // FileStream is used to write to the current file. |
| + scoped_ptr<FileStream> file_stream_; |
| + // Convenience storage for file names so we don't generate them over and over. |
| + std::vector<std::string> file_names_; |
| + size_t max_file_size_; |
| + size_t current_file_index_; |
| + // The last file index indicates the index of the file that will be deleted |
| + // first on rotation. |
| + size_t last_file_index_; |
| + // Number of bytes written to current file. We need this because with |
| + // buffering the file size read from disk might not be accurate. |
| + size_t current_bytes_written_; |
| + bool disable_buffering_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FileRotatingStream); |
| +}; |
| + |
| +// MobileDeviceLogStream is meant to be used on mobile devices where we will |
| +// have limited disk space. Its purpose is to read and write logs up to a |
| +// maximum size. Once the maximum size is exceeded, logs from the middle are |
| +// deleted whereas logs from the beginning and end are preserved. The reason for |
| +// this is because we anticipate that in WebRTC the beginning and end of the |
| +// logs are most useful for call diagnostics. |
| +// |
| +// This implementation simply writes to a single file until |
| +// |max_total_log_size| / 2 bytes are written to it, and subsequently writes to |
| +// a set of rotating files. We do this by inheriting FileRotatingStream and |
| +// setting the appropriate internal variables so that we don't delete the last |
| +// (earliest) file on rotate, and that that file's size is bigger. |
| +// |
| +// Open() must be called before using this stream. |
| +class MobileDeviceLogStream : public FileRotatingStream { |
|
jiayl2
2015/07/20 23:27:06
nit: this is useful not only for mobile devices, b
tkchin_webrtc
2015/07/21 20:39:13
Done.
|
| + public: |
| + // Use this constructor for reading a directory previously written to with |
| + // this stream. |
| + MobileDeviceLogStream(const std::string& dir_path); |
| + // Use this constructor for writing to a directory. Files in the directory |
| + // matching what's used by the stream will be deleted. |
| + MobileDeviceLogStream(const std::string& dir_path, size_t max_total_log_size); |
| + ~MobileDeviceLogStream() override {} |
| + |
| + protected: |
| + void OnRotation() override; |
| + |
| + private: |
| + static size_t GetRotatingLogSize(size_t max_total_log_size); |
| + static size_t GetNumRotatingLogFiles(size_t max_total_log_size); |
| + static const char* kLogPrefix; |
| + static const size_t kRotatingLogFileDefaultSize; |
| + |
| + const size_t max_total_log_size_; |
| + size_t num_rotations_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MobileDeviceLogStream); |
| +}; |
| + |
| // A stream that caps the output at a certain size, dropping content from the |
| // middle of the logical stream and maintaining equal parts of the start/end of |
| // the logical stream. |