blob: bda1ee16baef84beba86175576525aa4ad774ae5 [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>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <memory>
16#include <string>
17#include <vector>
tkchin93411912015-07-22 12:12:17 -070018
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#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;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 void Close() override;
56
57 // Opens the appropriate file(s). Call this before using the stream.
58 bool Open();
59
60 // Disabling buffering causes writes to block until disk is updated. This is
61 // enabled by default for performance.
62 bool DisableBuffering();
63
64 // Returns the path used for the i-th newest file, where the 0th file is the
65 // newest file. The file may or may not exist, this is just used for
66 // formatting. Index must be less than GetNumFiles().
67 std::string GetFilePath(size_t index) const;
68
69 // Returns the number of files that will used by this stream.
70 size_t GetNumFiles() const { return file_names_.size(); }
71
72 protected:
73 size_t GetMaxFileSize() const { return max_file_size_; }
74
75 void SetMaxFileSize(size_t size) { max_file_size_ = size; }
76
77 size_t GetRotationIndex() const { return rotation_index_; }
78
79 void SetRotationIndex(size_t index) { rotation_index_ = index; }
80
81 virtual void OnRotation() {}
82
83 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 bool OpenCurrentFile();
85 void CloseCurrentFile();
86
87 // Rotates the files by creating a new current file, renaming the
88 // existing files, and deleting the oldest one. e.g.
89 // file_0 -> file_1
90 // file_1 -> file_2
91 // file_2 -> delete
92 // create new file_0
93 void RotateFiles();
94
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095 // Private version of GetFilePath.
96 std::string GetFilePath(size_t index, size_t num_files) const;
97
98 const std::string dir_path_;
99 const std::string file_prefix_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100
101 // FileStream is used to write to the current file.
102 std::unique_ptr<FileStream> file_stream_;
103 // Convenience storage for file names so we don't generate them over and over.
104 std::vector<std::string> file_names_;
105 size_t max_file_size_;
106 size_t current_file_index_;
107 // The rotation index indicates the index of the file that will be
108 // deleted first on rotation. Indices lower than this index will be rotated.
109 size_t rotation_index_;
110 // Number of bytes written to current file. We need this because with
111 // buffering the file size read from disk might not be accurate.
112 size_t current_bytes_written_;
113 bool disable_buffering_;
114
115 RTC_DISALLOW_COPY_AND_ASSIGN(FileRotatingStream);
116};
117
118// CallSessionFileRotatingStream is meant to be used in situations where we will
Niels Möller6ffe62a2019-01-08 13:22:57 +0100119// have limited disk space. Its purpose is to write logs up to a
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120// maximum size. Once the maximum size is exceeded, logs from the middle are
121// deleted whereas logs from the beginning and end are preserved. The reason for
122// this is because we anticipate that in WebRTC the beginning and end of the
123// logs are most useful for call diagnostics.
124//
125// This implementation simply writes to a single file until
126// |max_total_log_size| / 2 bytes are written to it, and subsequently writes to
127// a set of rotating files. We do this by inheriting FileRotatingStream and
128// setting the appropriate internal variables so that we don't delete the last
129// (earliest) file on rotate, and that that file's size is bigger.
130//
131// Open() must be called before using this stream.
Niels Möller6ffe62a2019-01-08 13:22:57 +0100132
133// To read the logs produced by this class, one can use the companion class
134// CallSessionFileRotatingStreamReader.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135class CallSessionFileRotatingStream : public FileRotatingStream {
136 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200137 // Use this constructor for writing to a directory. Files in the directory
138 // matching what's used by the stream will be deleted. |max_total_log_size|
139 // must be at least 4.
140 CallSessionFileRotatingStream(const std::string& dir_path,
141 size_t max_total_log_size);
142 ~CallSessionFileRotatingStream() override {}
143
144 protected:
145 void OnRotation() override;
146
147 private:
148 static size_t GetRotatingLogSize(size_t max_total_log_size);
149 static size_t GetNumRotatingLogFiles(size_t max_total_log_size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200150 static const size_t kRotatingLogFileDefaultSize;
151
152 const size_t max_total_log_size_;
153 size_t num_rotations_;
154
155 RTC_DISALLOW_COPY_AND_ASSIGN(CallSessionFileRotatingStream);
156};
157
Niels Möllerd9ac0582019-01-03 14:21:38 +0100158// This is a convenience class, to read all files produced by a
159// FileRotatingStream, all in one go. Typical use calls GetSize and ReadData
160// only once. The list of file names to read is based on the contents of the log
161// directory at construction time.
162class FileRotatingStreamReader {
163 public:
164 FileRotatingStreamReader(const std::string& dir_path,
165 const std::string& file_prefix);
166 ~FileRotatingStreamReader();
167 size_t GetSize() const;
168 size_t ReadAll(void* buffer, size_t size) const;
169
170 private:
171 std::vector<std::string> file_names_;
172};
173
174class CallSessionFileRotatingStreamReader : public FileRotatingStreamReader {
175 public:
176 CallSessionFileRotatingStreamReader(const std::string& dir_path);
177};
178
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200179} // namespace rtc
tkchin93411912015-07-22 12:12:17 -0700180
Steve Anton10542f22019-01-11 09:11:00 -0800181#endif // RTC_BASE_FILE_ROTATING_STREAM_H_