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

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

Issue 1335923002: Add RTC_ prefix to (D)CHECKs and related macros. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebase. Created 5 years, 3 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/event.cc ('k') | webrtc/base/flags.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
(...skipping 19 matching lines...) Expand all
30 30
31 FileRotatingStream::FileRotatingStream(const std::string& dir_path, 31 FileRotatingStream::FileRotatingStream(const std::string& dir_path,
32 const std::string& file_prefix, 32 const std::string& file_prefix,
33 size_t max_file_size, 33 size_t max_file_size,
34 size_t num_files) 34 size_t num_files)
35 : FileRotatingStream(dir_path, 35 : FileRotatingStream(dir_path,
36 file_prefix, 36 file_prefix,
37 max_file_size, 37 max_file_size,
38 num_files, 38 num_files,
39 kWrite) { 39 kWrite) {
40 DCHECK_GT(max_file_size, 0u); 40 RTC_DCHECK_GT(max_file_size, 0u);
41 DCHECK_GT(num_files, 1u); 41 RTC_DCHECK_GT(num_files, 1u);
42 } 42 }
43 43
44 FileRotatingStream::FileRotatingStream(const std::string& dir_path, 44 FileRotatingStream::FileRotatingStream(const std::string& dir_path,
45 const std::string& file_prefix, 45 const std::string& file_prefix,
46 size_t max_file_size, 46 size_t max_file_size,
47 size_t num_files, 47 size_t num_files,
48 Mode mode) 48 Mode mode)
49 : dir_path_(dir_path), 49 : dir_path_(dir_path),
50 file_prefix_(file_prefix), 50 file_prefix_(file_prefix),
51 mode_(mode), 51 mode_(mode),
52 file_stream_(nullptr), 52 file_stream_(nullptr),
53 max_file_size_(max_file_size), 53 max_file_size_(max_file_size),
54 current_file_index_(0), 54 current_file_index_(0),
55 rotation_index_(0), 55 rotation_index_(0),
56 current_bytes_written_(0), 56 current_bytes_written_(0),
57 disable_buffering_(false) { 57 disable_buffering_(false) {
58 DCHECK(Filesystem::IsFolder(dir_path)); 58 RTC_DCHECK(Filesystem::IsFolder(dir_path));
59 switch (mode) { 59 switch (mode) {
60 case kWrite: { 60 case kWrite: {
61 file_names_.clear(); 61 file_names_.clear();
62 for (size_t i = 0; i < num_files; ++i) { 62 for (size_t i = 0; i < num_files; ++i) {
63 file_names_.push_back(GetFilePath(i, num_files)); 63 file_names_.push_back(GetFilePath(i, num_files));
64 } 64 }
65 rotation_index_ = num_files - 1; 65 rotation_index_ = num_files - 1;
66 break; 66 break;
67 } 67 }
68 case kRead: { 68 case kRead: {
(...skipping 18 matching lines...) Expand all
87 if (!file_stream_) { 87 if (!file_stream_) {
88 return SS_CLOSED; 88 return SS_CLOSED;
89 } 89 }
90 return file_stream_->GetState(); 90 return file_stream_->GetState();
91 } 91 }
92 92
93 StreamResult FileRotatingStream::Read(void* buffer, 93 StreamResult FileRotatingStream::Read(void* buffer,
94 size_t buffer_len, 94 size_t buffer_len,
95 size_t* read, 95 size_t* read,
96 int* error) { 96 int* error) {
97 DCHECK(buffer); 97 RTC_DCHECK(buffer);
98 if (mode_ != kRead) { 98 if (mode_ != kRead) {
99 return SR_EOS; 99 return SR_EOS;
100 } 100 }
101 if (current_file_index_ >= file_names_.size()) { 101 if (current_file_index_ >= file_names_.size()) {
102 return SR_EOS; 102 return SR_EOS;
103 } 103 }
104 // We will have no file stream initially, and when we are finished with the 104 // We will have no file stream initially, and when we are finished with the
105 // previous file. 105 // previous file.
106 if (!file_stream_) { 106 if (!file_stream_) {
107 if (!OpenCurrentFile()) { 107 if (!OpenCurrentFile()) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 size_t* written, 145 size_t* written,
146 int* error) { 146 int* error) {
147 if (mode_ != kWrite) { 147 if (mode_ != kWrite) {
148 return SR_EOS; 148 return SR_EOS;
149 } 149 }
150 if (!file_stream_) { 150 if (!file_stream_) {
151 std::cerr << "Open() must be called before Write." << std::endl; 151 std::cerr << "Open() must be called before Write." << std::endl;
152 return SR_ERROR; 152 return SR_ERROR;
153 } 153 }
154 // Write as much as will fit in to the current file. 154 // Write as much as will fit in to the current file.
155 DCHECK_LT(current_bytes_written_, max_file_size_); 155 RTC_DCHECK_LT(current_bytes_written_, max_file_size_);
156 size_t remaining_bytes = max_file_size_ - current_bytes_written_; 156 size_t remaining_bytes = max_file_size_ - current_bytes_written_;
157 size_t write_length = std::min(data_len, remaining_bytes); 157 size_t write_length = std::min(data_len, remaining_bytes);
158 size_t local_written = 0; 158 size_t local_written = 0;
159 if (!written) { 159 if (!written) {
160 written = &local_written; 160 written = &local_written;
161 } 161 }
162 StreamResult result = file_stream_->Write(data, write_length, written, error); 162 StreamResult result = file_stream_->Write(data, write_length, written, error);
163 current_bytes_written_ += *written; 163 current_bytes_written_ += *written;
164 164
165 // If we're done with this file, rotate it out. 165 // If we're done with this file, rotate it out.
166 if (current_bytes_written_ >= max_file_size_) { 166 if (current_bytes_written_ >= max_file_size_) {
167 DCHECK_EQ(current_bytes_written_, max_file_size_); 167 RTC_DCHECK_EQ(current_bytes_written_, max_file_size_);
168 RotateFiles(); 168 RotateFiles();
169 } 169 }
170 return result; 170 return result;
171 } 171 }
172 172
173 bool FileRotatingStream::Flush() { 173 bool FileRotatingStream::Flush() {
174 if (!file_stream_) { 174 if (!file_stream_) {
175 return false; 175 return false;
176 } 176 }
177 return file_stream_->Flush(); 177 return file_stream_->Flush();
178 } 178 }
179 179
180 bool FileRotatingStream::GetSize(size_t* size) const { 180 bool FileRotatingStream::GetSize(size_t* size) const {
181 if (mode_ != kRead) { 181 if (mode_ != kRead) {
182 // Not possible to get accurate size on disk when writing because of 182 // Not possible to get accurate size on disk when writing because of
183 // potential buffering. 183 // potential buffering.
184 return false; 184 return false;
185 } 185 }
186 DCHECK(size); 186 RTC_DCHECK(size);
187 *size = 0; 187 *size = 0;
188 size_t total_size = 0; 188 size_t total_size = 0;
189 for (auto file_name : file_names_) { 189 for (auto file_name : file_names_) {
190 Pathname pathname(file_name); 190 Pathname pathname(file_name);
191 size_t file_size = 0; 191 size_t file_size = 0;
192 if (Filesystem::GetFileSize(file_name, &file_size)) { 192 if (Filesystem::GetFileSize(file_name, &file_size)) {
193 total_size += file_size; 193 total_size += file_size;
194 } 194 }
195 } 195 }
196 *size = total_size; 196 *size = total_size;
(...skipping 28 matching lines...) Expand all
225 disable_buffering_ = true; 225 disable_buffering_ = true;
226 if (!file_stream_) { 226 if (!file_stream_) {
227 std::cerr << "Open() must be called before DisableBuffering()." 227 std::cerr << "Open() must be called before DisableBuffering()."
228 << std::endl; 228 << std::endl;
229 return false; 229 return false;
230 } 230 }
231 return file_stream_->DisableBuffering(); 231 return file_stream_->DisableBuffering();
232 } 232 }
233 233
234 std::string FileRotatingStream::GetFilePath(size_t index) const { 234 std::string FileRotatingStream::GetFilePath(size_t index) const {
235 DCHECK_LT(index, file_names_.size()); 235 RTC_DCHECK_LT(index, file_names_.size());
236 return file_names_[index]; 236 return file_names_[index];
237 } 237 }
238 238
239 bool FileRotatingStream::OpenCurrentFile() { 239 bool FileRotatingStream::OpenCurrentFile() {
240 CloseCurrentFile(); 240 CloseCurrentFile();
241 241
242 // Opens the appropriate file in the appropriate mode. 242 // Opens the appropriate file in the appropriate mode.
243 DCHECK_LT(current_file_index_, file_names_.size()); 243 RTC_DCHECK_LT(current_file_index_, file_names_.size());
244 std::string file_path = file_names_[current_file_index_]; 244 std::string file_path = file_names_[current_file_index_];
245 file_stream_.reset(new FileStream()); 245 file_stream_.reset(new FileStream());
246 const char* mode = nullptr; 246 const char* mode = nullptr;
247 switch (mode_) { 247 switch (mode_) {
248 case kWrite: 248 case kWrite:
249 mode = "w+"; 249 mode = "w+";
250 // We should always we writing to the zero-th file. 250 // We should always we writing to the zero-th file.
251 DCHECK_EQ(current_file_index_, 0u); 251 RTC_DCHECK_EQ(current_file_index_, 0u);
252 break; 252 break;
253 case kRead: 253 case kRead:
254 mode = "r"; 254 mode = "r";
255 break; 255 break;
256 } 256 }
257 int error = 0; 257 int error = 0;
258 if (!file_stream_->Open(file_path, mode, &error)) { 258 if (!file_stream_->Open(file_path, mode, &error)) {
259 std::cerr << "Failed to open: " << file_path << "Error: " << error 259 std::cerr << "Failed to open: " << file_path << "Error: " << error
260 << std::endl; 260 << std::endl;
261 file_stream_.reset(); 261 file_stream_.reset();
262 return false; 262 return false;
263 } 263 }
264 if (disable_buffering_) { 264 if (disable_buffering_) {
265 file_stream_->DisableBuffering(); 265 file_stream_->DisableBuffering();
266 } 266 }
267 return true; 267 return true;
268 } 268 }
269 269
270 void FileRotatingStream::CloseCurrentFile() { 270 void FileRotatingStream::CloseCurrentFile() {
271 if (!file_stream_) { 271 if (!file_stream_) {
272 return; 272 return;
273 } 273 }
274 current_bytes_written_ = 0; 274 current_bytes_written_ = 0;
275 file_stream_.reset(); 275 file_stream_.reset();
276 } 276 }
277 277
278 void FileRotatingStream::RotateFiles() { 278 void FileRotatingStream::RotateFiles() {
279 DCHECK_EQ(mode_, kWrite); 279 RTC_DCHECK_EQ(mode_, kWrite);
280 CloseCurrentFile(); 280 CloseCurrentFile();
281 // Rotates the files by deleting the file at |rotation_index_|, which is the 281 // Rotates the files by deleting the file at |rotation_index_|, which is the
282 // oldest file and then renaming the newer files to have an incremented index. 282 // oldest file and then renaming the newer files to have an incremented index.
283 // See header file comments for example. 283 // See header file comments for example.
284 DCHECK_LE(rotation_index_, file_names_.size()); 284 RTC_DCHECK_LE(rotation_index_, file_names_.size());
285 std::string file_to_delete = file_names_[rotation_index_]; 285 std::string file_to_delete = file_names_[rotation_index_];
286 if (Filesystem::IsFile(file_to_delete)) { 286 if (Filesystem::IsFile(file_to_delete)) {
287 if (!Filesystem::DeleteFile(file_to_delete)) { 287 if (!Filesystem::DeleteFile(file_to_delete)) {
288 std::cerr << "Failed to delete: " << file_to_delete << std::endl; 288 std::cerr << "Failed to delete: " << file_to_delete << std::endl;
289 } 289 }
290 } 290 }
291 for (auto i = rotation_index_; i > 0; --i) { 291 for (auto i = rotation_index_; i > 0; --i) {
292 std::string rotated_name = file_names_[i]; 292 std::string rotated_name = file_names_[i];
293 std::string unrotated_name = file_names_[i - 1]; 293 std::string unrotated_name = file_names_[i - 1];
294 if (Filesystem::IsFile(unrotated_name)) { 294 if (Filesystem::IsFile(unrotated_name)) {
(...skipping 23 matching lines...) Expand all
318 current_name.compare(0, file_prefix_.size(), file_prefix_) == 0) { 318 current_name.compare(0, file_prefix_.size(), file_prefix_) == 0) {
319 Pathname path(dir_path_, current_name); 319 Pathname path(dir_path_, current_name);
320 files.push_back(path.pathname()); 320 files.push_back(path.pathname());
321 } 321 }
322 } while (it.Next()); 322 } while (it.Next());
323 return files; 323 return files;
324 } 324 }
325 325
326 std::string FileRotatingStream::GetFilePath(size_t index, 326 std::string FileRotatingStream::GetFilePath(size_t index,
327 size_t num_files) const { 327 size_t num_files) const {
328 DCHECK_LT(index, num_files); 328 RTC_DCHECK_LT(index, num_files);
329 std::ostringstream file_name; 329 std::ostringstream file_name;
330 // The format will be "_%<num_digits>zu". We want to zero pad the index so 330 // The format will be "_%<num_digits>zu". We want to zero pad the index so
331 // that it will sort nicely. 331 // that it will sort nicely.
332 size_t max_digits = ((num_files - 1) / 10) + 1; 332 size_t max_digits = ((num_files - 1) / 10) + 1;
333 size_t num_digits = (index / 10) + 1; 333 size_t num_digits = (index / 10) + 1;
334 DCHECK_LE(num_digits, max_digits); 334 RTC_DCHECK_LE(num_digits, max_digits);
335 size_t padding = max_digits - num_digits; 335 size_t padding = max_digits - num_digits;
336 336
337 file_name << file_prefix_ << "_"; 337 file_name << file_prefix_ << "_";
338 for (size_t i = 0; i < padding; ++i) { 338 for (size_t i = 0; i < padding; ++i) {
339 file_name << "0"; 339 file_name << "0";
340 } 340 }
341 file_name << index; 341 file_name << index;
342 342
343 Pathname file_path(dir_path_, file_name.str()); 343 Pathname file_path(dir_path_, file_name.str());
344 return file_path.pathname(); 344 return file_path.pathname();
345 } 345 }
346 346
347 CallSessionFileRotatingStream::CallSessionFileRotatingStream( 347 CallSessionFileRotatingStream::CallSessionFileRotatingStream(
348 const std::string& dir_path) 348 const std::string& dir_path)
349 : FileRotatingStream(dir_path, kLogPrefix), 349 : FileRotatingStream(dir_path, kLogPrefix),
350 max_total_log_size_(0), 350 max_total_log_size_(0),
351 num_rotations_(0) { 351 num_rotations_(0) {
352 } 352 }
353 353
354 CallSessionFileRotatingStream::CallSessionFileRotatingStream( 354 CallSessionFileRotatingStream::CallSessionFileRotatingStream(
355 const std::string& dir_path, 355 const std::string& dir_path,
356 size_t max_total_log_size) 356 size_t max_total_log_size)
357 : FileRotatingStream(dir_path, 357 : FileRotatingStream(dir_path,
358 kLogPrefix, 358 kLogPrefix,
359 max_total_log_size / 2, 359 max_total_log_size / 2,
360 GetNumRotatingLogFiles(max_total_log_size) + 1), 360 GetNumRotatingLogFiles(max_total_log_size) + 1),
361 max_total_log_size_(max_total_log_size), 361 max_total_log_size_(max_total_log_size),
362 num_rotations_(0) { 362 num_rotations_(0) {
363 DCHECK_GE(max_total_log_size, 4u); 363 RTC_DCHECK_GE(max_total_log_size, 4u);
364 } 364 }
365 365
366 const char* CallSessionFileRotatingStream::kLogPrefix = "webrtc_log"; 366 const char* CallSessionFileRotatingStream::kLogPrefix = "webrtc_log";
367 const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize = 367 const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize =
368 1024 * 1024; 368 1024 * 1024;
369 369
370 void CallSessionFileRotatingStream::OnRotation() { 370 void CallSessionFileRotatingStream::OnRotation() {
371 ++num_rotations_; 371 ++num_rotations_;
372 if (num_rotations_ == 1) { 372 if (num_rotations_ == 1) {
373 // On the first rotation adjust the max file size so subsequent files after 373 // On the first rotation adjust the max file size so subsequent files after
(...skipping 17 matching lines...) Expand all
391 391
392 size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles( 392 size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles(
393 size_t max_total_log_size) { 393 size_t max_total_log_size) {
394 // At minimum have two rotating files. Otherwise split the available log size 394 // At minimum have two rotating files. Otherwise split the available log size
395 // evenly across 1MB files. 395 // evenly across 1MB files.
396 return std::max((size_t)2, 396 return std::max((size_t)2,
397 (max_total_log_size / 2) / kRotatingLogFileDefaultSize); 397 (max_total_log_size / 2) / kRotatingLogFileDefaultSize);
398 } 398 }
399 399
400 } // namespace rtc 400 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/event.cc ('k') | webrtc/base/flags.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698