blob: 78e2983445f11024a7e0c3db08484ef7d7221a5d [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"
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.
29class FileRotatingStream : public StreamInterface {
30 public:
31 // Use this constructor for reading a directory previously written to with
32 // this stream.
33 FileRotatingStream(const std::string& dir_path,
34 const std::string& file_prefix);
35
36 // Use this constructor for writing to a directory. Files in the directory
37 // matching the prefix will be deleted on open.
38 FileRotatingStream(const std::string& dir_path,
39 const std::string& file_prefix,
40 size_t max_file_size,
41 size_t num_files);
42
43 ~FileRotatingStream() override;
44
45 // StreamInterface methods.
46 StreamState GetState() const override;
47 StreamResult Read(void* buffer,
48 size_t buffer_len,
49 size_t* read,
50 int* error) override;
51 StreamResult Write(const void* data,
52 size_t data_len,
53 size_t* written,
54 int* error) override;
55 bool Flush() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056 void Close() override;
57
58 // Opens the appropriate file(s). Call this before using the stream.
59 bool Open();
60
61 // Disabling buffering causes writes to block until disk is updated. This is
62 // enabled by default for performance.
63 bool DisableBuffering();
64
65 // Returns the path used for the i-th newest file, where the 0th file is the
66 // newest file. The file may or may not exist, this is just used for
67 // formatting. Index must be less than GetNumFiles().
68 std::string GetFilePath(size_t index) const;
69
70 // Returns the number of files that will used by this stream.
71 size_t GetNumFiles() const { return file_names_.size(); }
72
73 protected:
74 size_t GetMaxFileSize() const { return max_file_size_; }
75
76 void SetMaxFileSize(size_t size) { max_file_size_ = size; }
77
78 size_t GetRotationIndex() const { return rotation_index_; }
79
80 void SetRotationIndex(size_t index) { rotation_index_ = index; }
81
82 virtual void OnRotation() {}
83
84 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085 bool OpenCurrentFile();
86 void CloseCurrentFile();
87
88 // Rotates the files by creating a new current file, renaming the
89 // existing files, and deleting the oldest one. e.g.
90 // file_0 -> file_1
91 // file_1 -> file_2
92 // file_2 -> delete
93 // create new file_0
94 void RotateFiles();
95
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096 // Private version of GetFilePath.
97 std::string GetFilePath(size_t index, size_t num_files) const;
98
99 const std::string dir_path_;
100 const std::string file_prefix_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101
Niels Möller23213d92019-01-22 11:01:24 +0100102 // File we're currently writing to.
103 webrtc::FileWrapper file_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104 // Convenience storage for file names so we don't generate them over and over.
105 std::vector<std::string> file_names_;
106 size_t max_file_size_;
107 size_t current_file_index_;
108 // The rotation index indicates the index of the file that will be
109 // deleted first on rotation. Indices lower than this index will be rotated.
110 size_t rotation_index_;
111 // Number of bytes written to current file. We need this because with
112 // buffering the file size read from disk might not be accurate.
113 size_t current_bytes_written_;
114 bool disable_buffering_;
115
116 RTC_DISALLOW_COPY_AND_ASSIGN(FileRotatingStream);
117};
118
119// CallSessionFileRotatingStream is meant to be used in situations where we will
Niels Möller6ffe62a2019-01-08 13:22:57 +0100120// have limited disk space. Its purpose is to write logs up to a
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121// maximum size. Once the maximum size is exceeded, logs from the middle are
122// deleted whereas logs from the beginning and end are preserved. The reason for
123// this is because we anticipate that in WebRTC the beginning and end of the
124// logs are most useful for call diagnostics.
125//
126// This implementation simply writes to a single file until
127// |max_total_log_size| / 2 bytes are written to it, and subsequently writes to
128// a set of rotating files. We do this by inheriting FileRotatingStream and
129// setting the appropriate internal variables so that we don't delete the last
130// (earliest) file on rotate, and that that file's size is bigger.
131//
132// Open() must be called before using this stream.
Niels Möller6ffe62a2019-01-08 13:22:57 +0100133
134// To read the logs produced by this class, one can use the companion class
135// CallSessionFileRotatingStreamReader.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136class CallSessionFileRotatingStream : public FileRotatingStream {
137 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200138 // Use this constructor for writing to a directory. Files in the directory
139 // matching what's used by the stream will be deleted. |max_total_log_size|
140 // must be at least 4.
141 CallSessionFileRotatingStream(const std::string& dir_path,
142 size_t max_total_log_size);
143 ~CallSessionFileRotatingStream() override {}
144
145 protected:
146 void OnRotation() override;
147
148 private:
149 static size_t GetRotatingLogSize(size_t max_total_log_size);
150 static size_t GetNumRotatingLogFiles(size_t max_total_log_size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151 static const size_t kRotatingLogFileDefaultSize;
152
153 const size_t max_total_log_size_;
154 size_t num_rotations_;
155
156 RTC_DISALLOW_COPY_AND_ASSIGN(CallSessionFileRotatingStream);
157};
158
Niels Möllerd9ac0582019-01-03 14:21:38 +0100159// This is a convenience class, to read all files produced by a
160// FileRotatingStream, all in one go. Typical use calls GetSize and ReadData
161// only once. The list of file names to read is based on the contents of the log
162// directory at construction time.
163class FileRotatingStreamReader {
164 public:
165 FileRotatingStreamReader(const std::string& dir_path,
166 const std::string& file_prefix);
167 ~FileRotatingStreamReader();
168 size_t GetSize() const;
169 size_t ReadAll(void* buffer, size_t size) const;
170
171 private:
172 std::vector<std::string> file_names_;
173};
174
175class CallSessionFileRotatingStreamReader : public FileRotatingStreamReader {
176 public:
177 CallSessionFileRotatingStreamReader(const std::string& dir_path);
178};
179
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200180} // namespace rtc
tkchin93411912015-07-22 12:12:17 -0700181
Steve Anton10542f22019-01-11 09:11:00 -0800182#endif // RTC_BASE_FILE_ROTATING_STREAM_H_