blob: beb47c83da49988f243db49dac161e11917ea995 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_FILE_ROTATING_STREAM_H_
12#define RTC_BASE_FILE_ROTATING_STREAM_H_
tkchin93411912015-07-22 12:12:17 -070013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <memory>
17#include <string>
18#include <vector>
tkchin93411912015-07-22 12:12:17 -070019
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/constructor_magic.h"
Niels Möller23213d92019-01-22 11:01:24 +010021#include "rtc_base/system/file_wrapper.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace 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.
Niels Möllera9311b62021-03-25 15:29:02 +010029class FileRotatingStream {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031 // Use this constructor for writing to a directory. Files in the directory
32 // matching the prefix will be deleted on open.
33 FileRotatingStream(const std::string& dir_path,
34 const std::string& file_prefix,
35 size_t max_file_size,
36 size_t num_files);
37
Niels Möllera9311b62021-03-25 15:29:02 +010038 virtual ~FileRotatingStream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039
Niels Möllera9311b62021-03-25 15:29:02 +010040 bool IsOpen() const;
41
42 bool Write(const void* data, size_t data_len);
43 bool Flush();
44 void Close();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045
46 // Opens the appropriate file(s). Call this before using the stream.
47 bool Open();
48
49 // Disabling buffering causes writes to block until disk is updated. This is
50 // enabled by default for performance.
51 bool DisableBuffering();
52
Niels Möllera9311b62021-03-25 15:29:02 +010053 // Below two methods are public for testing only.
54
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 // Returns the path used for the i-th newest file, where the 0th file is the
56 // newest file. The file may or may not exist, this is just used for
57 // formatting. Index must be less than GetNumFiles().
58 std::string GetFilePath(size_t index) const;
59
60 // Returns the number of files that will used by this stream.
61 size_t GetNumFiles() const { return file_names_.size(); }
62
63 protected:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064 void SetMaxFileSize(size_t size) { max_file_size_ = size; }
65
66 size_t GetRotationIndex() const { return rotation_index_; }
67
68 void SetRotationIndex(size_t index) { rotation_index_ = index; }
69
70 virtual void OnRotation() {}
71
72 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 bool OpenCurrentFile();
74 void CloseCurrentFile();
75
76 // Rotates the files by creating a new current file, renaming the
77 // existing files, and deleting the oldest one. e.g.
78 // file_0 -> file_1
79 // file_1 -> file_2
80 // file_2 -> delete
81 // create new file_0
82 void RotateFiles();
83
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 // Private version of GetFilePath.
85 std::string GetFilePath(size_t index, size_t num_files) const;
86
87 const std::string dir_path_;
88 const std::string file_prefix_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089
Niels Möller23213d92019-01-22 11:01:24 +010090 // File we're currently writing to.
91 webrtc::FileWrapper file_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092 // Convenience storage for file names so we don't generate them over and over.
93 std::vector<std::string> file_names_;
94 size_t max_file_size_;
95 size_t current_file_index_;
96 // The rotation index indicates the index of the file that will be
97 // deleted first on rotation. Indices lower than this index will be rotated.
98 size_t rotation_index_;
99 // Number of bytes written to current file. We need this because with
100 // buffering the file size read from disk might not be accurate.
101 size_t current_bytes_written_;
102 bool disable_buffering_;
103
104 RTC_DISALLOW_COPY_AND_ASSIGN(FileRotatingStream);
105};
106
107// CallSessionFileRotatingStream is meant to be used in situations where we will
Niels Möller6ffe62a2019-01-08 13:22:57 +0100108// have limited disk space. Its purpose is to write logs up to a
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109// 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 Titov96e3b992021-07-26 16:03:14 +0200115// `max_total_log_size` / 2 bytes are written to it, and subsequently writes to
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200116// 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öller6ffe62a2019-01-08 13:22:57 +0100121
122// To read the logs produced by this class, one can use the companion class
123// CallSessionFileRotatingStreamReader.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124class CallSessionFileRotatingStream : public FileRotatingStream {
125 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200126 // Use this constructor for writing to a directory. Files in the directory
Artem Titov96e3b992021-07-26 16:03:14 +0200127 // matching what's used by the stream will be deleted. `max_total_log_size`
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128 // must be at least 4.
129 CallSessionFileRotatingStream(const std::string& dir_path,
130 size_t max_total_log_size);
131 ~CallSessionFileRotatingStream() override {}
132
133 protected:
134 void OnRotation() override;
135
136 private:
137 static size_t GetRotatingLogSize(size_t max_total_log_size);
138 static size_t GetNumRotatingLogFiles(size_t max_total_log_size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200139 static const size_t kRotatingLogFileDefaultSize;
140
141 const size_t max_total_log_size_;
142 size_t num_rotations_;
143
144 RTC_DISALLOW_COPY_AND_ASSIGN(CallSessionFileRotatingStream);
145};
146
Niels Möllerd9ac0582019-01-03 14:21:38 +0100147// This is a convenience class, to read all files produced by a
148// FileRotatingStream, all in one go. Typical use calls GetSize and ReadData
149// only once. The list of file names to read is based on the contents of the log
150// directory at construction time.
151class FileRotatingStreamReader {
152 public:
153 FileRotatingStreamReader(const std::string& dir_path,
154 const std::string& file_prefix);
155 ~FileRotatingStreamReader();
156 size_t GetSize() const;
157 size_t ReadAll(void* buffer, size_t size) const;
158
159 private:
160 std::vector<std::string> file_names_;
161};
162
163class CallSessionFileRotatingStreamReader : public FileRotatingStreamReader {
164 public:
165 CallSessionFileRotatingStreamReader(const std::string& dir_path);
166};
167
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200168} // namespace rtc
tkchin93411912015-07-22 12:12:17 -0700169
Steve Anton10542f22019-01-11 09:11:00 -0800170#endif // RTC_BASE_FILE_ROTATING_STREAM_H_