blob: 1d3da92eea22f89a939302cef1a8493e2097b8d5 [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#ifndef RTC_BASE_FILEROTATINGSTREAM_H_
12#define RTC_BASE_FILEROTATINGSTREAM_H_
tkchin93411912015-07-22 12:12:17 -070013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <memory>
16#include <string>
17#include <vector>
tkchin93411912015-07-22 12:12:17 -070018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/constructormagic.h"
20#include "rtc_base/stream.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021
22namespace 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.
28class FileRotatingStream : public StreamInterface {
29 public:
30 // Use this constructor for reading a directory previously written to with
31 // this stream.
32 FileRotatingStream(const std::string& dir_path,
33 const std::string& file_prefix);
34
35 // Use this constructor for writing to a directory. Files in the directory
36 // matching the prefix will be deleted on open.
37 FileRotatingStream(const std::string& dir_path,
38 const std::string& file_prefix,
39 size_t max_file_size,
40 size_t num_files);
41
42 ~FileRotatingStream() override;
43
44 // StreamInterface methods.
45 StreamState GetState() const override;
46 StreamResult Read(void* buffer,
47 size_t buffer_len,
48 size_t* read,
49 int* error) override;
50 StreamResult Write(const void* data,
51 size_t data_len,
52 size_t* written,
53 int* error) override;
54 bool Flush() override;
55 // Returns the total file size currently used on disk.
56 bool GetSize(size_t* size) const override;
57 void Close() override;
58
59 // Opens the appropriate file(s). Call this before using the stream.
60 bool Open();
61
62 // Disabling buffering causes writes to block until disk is updated. This is
63 // enabled by default for performance.
64 bool DisableBuffering();
65
66 // Returns the path used for the i-th newest file, where the 0th file is the
67 // newest file. The file may or may not exist, this is just used for
68 // formatting. Index must be less than GetNumFiles().
69 std::string GetFilePath(size_t index) const;
70
71 // Returns the number of files that will used by this stream.
72 size_t GetNumFiles() const { return file_names_.size(); }
73
74 protected:
75 size_t GetMaxFileSize() const { return max_file_size_; }
76
77 void SetMaxFileSize(size_t size) { max_file_size_ = size; }
78
79 size_t GetRotationIndex() const { return rotation_index_; }
80
81 void SetRotationIndex(size_t index) { rotation_index_ = index; }
82
83 virtual void OnRotation() {}
84
85 private:
86 enum Mode { kRead, kWrite };
87
88 FileRotatingStream(const std::string& dir_path,
89 const std::string& file_prefix,
90 size_t max_file_size,
91 size_t num_files,
92 Mode mode);
93
94 bool OpenCurrentFile();
95 void CloseCurrentFile();
96
97 // Rotates the files by creating a new current file, renaming the
98 // existing files, and deleting the oldest one. e.g.
99 // file_0 -> file_1
100 // file_1 -> file_2
101 // file_2 -> delete
102 // create new file_0
103 void RotateFiles();
104
105 // Returns a list of file names in the directory beginning with the prefix.
106 std::vector<std::string> GetFilesWithPrefix() const;
107 // Private version of GetFilePath.
108 std::string GetFilePath(size_t index, size_t num_files) const;
109
110 const std::string dir_path_;
111 const std::string file_prefix_;
112 const Mode mode_;
113
114 // FileStream is used to write to the current file.
115 std::unique_ptr<FileStream> file_stream_;
116 // Convenience storage for file names so we don't generate them over and over.
117 std::vector<std::string> file_names_;
118 size_t max_file_size_;
119 size_t current_file_index_;
120 // The rotation index indicates the index of the file that will be
121 // deleted first on rotation. Indices lower than this index will be rotated.
122 size_t rotation_index_;
123 // Number of bytes written to current file. We need this because with
124 // buffering the file size read from disk might not be accurate.
125 size_t current_bytes_written_;
126 bool disable_buffering_;
127
128 RTC_DISALLOW_COPY_AND_ASSIGN(FileRotatingStream);
129};
130
131// CallSessionFileRotatingStream is meant to be used in situations where we will
132// have limited disk space. Its purpose is to read and write logs up to a
133// maximum size. Once the maximum size is exceeded, logs from the middle are
134// deleted whereas logs from the beginning and end are preserved. The reason for
135// this is because we anticipate that in WebRTC the beginning and end of the
136// logs are most useful for call diagnostics.
137//
138// This implementation simply writes to a single file until
139// |max_total_log_size| / 2 bytes are written to it, and subsequently writes to
140// a set of rotating files. We do this by inheriting FileRotatingStream and
141// setting the appropriate internal variables so that we don't delete the last
142// (earliest) file on rotate, and that that file's size is bigger.
143//
144// Open() must be called before using this stream.
145class CallSessionFileRotatingStream : public FileRotatingStream {
146 public:
147 // Use this constructor for reading a directory previously written to with
148 // this stream.
149 explicit CallSessionFileRotatingStream(const std::string& dir_path);
150 // Use this constructor for writing to a directory. Files in the directory
151 // matching what's used by the stream will be deleted. |max_total_log_size|
152 // must be at least 4.
153 CallSessionFileRotatingStream(const std::string& dir_path,
154 size_t max_total_log_size);
155 ~CallSessionFileRotatingStream() override {}
156
157 protected:
158 void OnRotation() override;
159
160 private:
161 static size_t GetRotatingLogSize(size_t max_total_log_size);
162 static size_t GetNumRotatingLogFiles(size_t max_total_log_size);
163 static const char* kLogPrefix;
164 static const size_t kRotatingLogFileDefaultSize;
165
166 const size_t max_total_log_size_;
167 size_t num_rotations_;
168
169 RTC_DISALLOW_COPY_AND_ASSIGN(CallSessionFileRotatingStream);
170};
171
172} // namespace rtc
tkchin93411912015-07-22 12:12:17 -0700173
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200174#endif // RTC_BASE_FILEROTATINGSTREAM_H_