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

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

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

Powered by Google App Engine
This is Rietveld 408576698