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