Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: webrtc/base/filerotatingstream.h

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/file_win.cc ('k') | webrtc/base/filerotatingstream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_FILEROTATINGSTREAM_H_ 11 #ifndef WEBRTC_BASE_FILEROTATINGSTREAM_H_
12 #define WEBRTC_BASE_FILEROTATINGSTREAM_H_ 12 #define WEBRTC_BASE_FILEROTATINGSTREAM_H_
13 13
14 #include <memory>
15 #include <string>
16 #include <vector>
17 14
18 #include "webrtc/base/constructormagic.h" 15 // This header is deprecated and is just left here temporarily during
19 #include "webrtc/base/stream.h" 16 // refactoring. See https://bugs.webrtc.org/7634 for more details.
20 17 #include "webrtc/rtc_base/filerotatingstream.h"
21 namespace rtc {
22
23 // FileRotatingStream writes to a file in the directory specified in the
24 // constructor. It rotates the files once the current file is full. The
25 // individual file size and the number of files used is configurable in the
26 // constructor. Open() must be called before using this stream.
27 class FileRotatingStream : public StreamInterface {
28 public:
29 // Use this constructor for reading a directory previously written to with
30 // this stream.
31 FileRotatingStream(const std::string& dir_path,
32 const std::string& file_prefix);
33
34 // Use this constructor for writing to a directory. Files in the directory
35 // matching the prefix will be deleted on open.
36 FileRotatingStream(const std::string& dir_path,
37 const std::string& file_prefix,
38 size_t max_file_size,
39 size_t num_files);
40
41 ~FileRotatingStream() override;
42
43 // StreamInterface methods.
44 StreamState GetState() const override;
45 StreamResult Read(void* buffer,
46 size_t buffer_len,
47 size_t* read,
48 int* error) override;
49 StreamResult Write(const void* data,
50 size_t data_len,
51 size_t* written,
52 int* error) override;
53 bool Flush() override;
54 // Returns the total file size currently used on disk.
55 bool GetSize(size_t* size) const override;
56 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:
85 enum Mode { kRead, kWrite };
86
87 FileRotatingStream(const std::string& dir_path,
88 const std::string& file_prefix,
89 size_t max_file_size,
90 size_t num_files,
91 Mode mode);
92
93 bool OpenCurrentFile();
94 void CloseCurrentFile();
95
96 // Rotates the files by creating a new current file, renaming the
97 // existing files, and deleting the oldest one. e.g.
98 // file_0 -> file_1
99 // file_1 -> file_2
100 // file_2 -> delete
101 // create new file_0
102 void RotateFiles();
103
104 // Returns a list of file names in the directory beginning with the prefix.
105 std::vector<std::string> GetFilesWithPrefix() const;
106 // Private version of GetFilePath.
107 std::string GetFilePath(size_t index, size_t num_files) const;
108
109 const std::string dir_path_;
110 const std::string file_prefix_;
111 const Mode mode_;
112
113 // FileStream is used to write to the current file.
114 std::unique_ptr<FileStream> file_stream_;
115 // Convenience storage for file names so we don't generate them over and over.
116 std::vector<std::string> file_names_;
117 size_t max_file_size_;
118 size_t current_file_index_;
119 // The rotation index indicates the index of the file that will be
120 // deleted first on rotation. Indices lower than this index will be rotated.
121 size_t rotation_index_;
122 // Number of bytes written to current file. We need this because with
123 // buffering the file size read from disk might not be accurate.
124 size_t current_bytes_written_;
125 bool disable_buffering_;
126
127 RTC_DISALLOW_COPY_AND_ASSIGN(FileRotatingStream);
128 };
129
130 // CallSessionFileRotatingStream is meant to be used in situations where we will
131 // have limited disk space. Its purpose is to read and write logs up to a
132 // maximum size. Once the maximum size is exceeded, logs from the middle are
133 // deleted whereas logs from the beginning and end are preserved. The reason for
134 // this is because we anticipate that in WebRTC the beginning and end of the
135 // logs are most useful for call diagnostics.
136 //
137 // This implementation simply writes to a single file until
138 // |max_total_log_size| / 2 bytes are written to it, and subsequently writes to
139 // a set of rotating files. We do this by inheriting FileRotatingStream and
140 // setting the appropriate internal variables so that we don't delete the last
141 // (earliest) file on rotate, and that that file's size is bigger.
142 //
143 // Open() must be called before using this stream.
144 class CallSessionFileRotatingStream : public FileRotatingStream {
145 public:
146 // Use this constructor for reading a directory previously written to with
147 // this stream.
148 explicit CallSessionFileRotatingStream(const std::string& dir_path);
149 // Use this constructor for writing to a directory. Files in the directory
150 // matching what's used by the stream will be deleted. |max_total_log_size|
151 // must be at least 4.
152 CallSessionFileRotatingStream(const std::string& dir_path,
153 size_t max_total_log_size);
154 ~CallSessionFileRotatingStream() override {}
155
156 protected:
157 void OnRotation() override;
158
159 private:
160 static size_t GetRotatingLogSize(size_t max_total_log_size);
161 static size_t GetNumRotatingLogFiles(size_t max_total_log_size);
162 static const char* kLogPrefix;
163 static const size_t kRotatingLogFileDefaultSize;
164
165 const size_t max_total_log_size_;
166 size_t num_rotations_;
167
168 RTC_DISALLOW_COPY_AND_ASSIGN(CallSessionFileRotatingStream);
169 };
170
171 } // namespace rtc
172 18
173 #endif // WEBRTC_BASE_FILEROTATINGSTREAM_H_ 19 #endif // WEBRTC_BASE_FILEROTATINGSTREAM_H_
OLDNEW
« no previous file with comments | « webrtc/base/file_win.cc ('k') | webrtc/base/filerotatingstream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698