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

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

Issue 1230823009: Add rotating log file stream. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Cleanup 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
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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 400 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 411
412 protected: 412 protected:
413 virtual void DoClose(); 413 virtual void DoClose();
414 414
415 FILE* file_; 415 FILE* file_;
416 416
417 private: 417 private:
418 DISALLOW_COPY_AND_ASSIGN(FileStream); 418 DISALLOW_COPY_AND_ASSIGN(FileStream);
419 }; 419 };
420 420
421 // FileRotatingStream writes to a file in the directory specified in the
jiayl2 2015/07/20 23:27:06 I think the declaration and impl are big enough to
tkchin_webrtc 2015/07/21 20:39:13 Done.
422 // constructor. It rotates the files once the current file is full. The
423 // individual file size and the number of files used is configurable in the
424 // constructor. Open must be called before using this stream.
425 class FileRotatingStream : public StreamInterface {
426 public:
427 // Use this constructor for reading a directory previously written to with
428 // this stream.
429 FileRotatingStream(const std::string& dir_path,
430 const std::string& file_prefix);
431
432 // Use this constructor for writing to a directory. Files in the directory
433 // matching the prefix will be deleted on open.
434 FileRotatingStream(const std::string& dir_path,
435 const std::string& file_prefix,
436 size_t max_file_size,
437 size_t num_files);
438
439 ~FileRotatingStream() override;
440
441 // StreamInterface methods.
442 StreamState GetState() const override;
443 StreamResult Read(void* buffer,
444 size_t buffer_len,
445 size_t* read,
446 int* error) override;
447 StreamResult Write(const void* data,
448 size_t data_len,
449 size_t* written,
450 int* error) override;
451 bool Flush() override;
452 void Close() override;
453
454 // Opens the appropriate file(s). Call this before using the stream.
455 bool Open();
456
457 // Disabling buffering causes writes to block until disk is updated. This is
458 // enabled by default for performance.
459 bool DisableBuffering();
460
461 // Returns the path used for the file at the i-th index. The file may or may
462 // not exist, this is just used for formatting. Index must be less than
463 // GetNumFiles().
464 std::string GetFilePath(size_t index) const;
465
466 // Returns the number of files that will used by this stream.
467 size_t GetNumFiles() { return file_names_.size(); }
468
469 protected:
470 size_t GetMaxFileSize() const { return max_file_size_; }
471
472 void SetMaxFileSize(size_t size) { max_file_size_ = size; }
473
474 size_t GetLastFileIndex() const { return last_file_index_; }
475
476 void SetLastFileIndex(size_t index) { last_file_index_ = index; }
477
478 virtual void OnRotation() {}
479
480 private:
481 enum Mode { kRead, kWrite };
482
483 FileRotatingStream(const std::string& dir_path,
484 const std::string& file_prefix,
485 size_t max_file_size,
486 size_t num_files,
487 Mode mode);
488
489 // Opens the file at the current file index. Set |delete_existing| to true
490 // to also delete files that begin with the prefix.
491 bool Open(bool delete_existing);
492
493 // Rotates the files by creating a new current file, renaming the
494 // existing files, and deleting the oldest one. e.g.
495 // file_0 -> file_1
496 // file_1 -> file_2
497 // file_2 -> delete
498 // create new file_0
499 void RotateFiles();
500
501 // Returns a list of file names in the directory beginning with the prefix.
502 std::vector<std::string> GetFilesWithPrefix() const;
503 // Private version of GetFilePath.
504 std::string GetFilePath(size_t index, size_t num_files) const;
505
506 const std::string dir_path_;
507 const std::string file_prefix_;
508 const Mode mode_;
509
510 // FileStream is used to write to the current file.
511 scoped_ptr<FileStream> file_stream_;
512 // Convenience storage for file names so we don't generate them over and over.
513 std::vector<std::string> file_names_;
514 size_t max_file_size_;
515 size_t current_file_index_;
516 // The last file index indicates the index of the file that will be deleted
517 // first on rotation.
518 size_t last_file_index_;
519 // Number of bytes written to current file. We need this because with
520 // buffering the file size read from disk might not be accurate.
521 size_t current_bytes_written_;
522 bool disable_buffering_;
523
524 DISALLOW_COPY_AND_ASSIGN(FileRotatingStream);
525 };
526
527 // MobileDeviceLogStream is meant to be used on mobile devices where we will
528 // have limited disk space. Its purpose is to read and write logs up to a
529 // maximum size. Once the maximum size is exceeded, logs from the middle are
530 // deleted whereas logs from the beginning and end are preserved. The reason for
531 // this is because we anticipate that in WebRTC the beginning and end of the
532 // logs are most useful for call diagnostics.
533 //
534 // This implementation simply writes to a single file until
535 // |max_total_log_size| / 2 bytes are written to it, and subsequently writes to
536 // a set of rotating files. We do this by inheriting FileRotatingStream and
537 // setting the appropriate internal variables so that we don't delete the last
538 // (earliest) file on rotate, and that that file's size is bigger.
539 //
540 // Open() must be called before using this stream.
541 class MobileDeviceLogStream : public FileRotatingStream {
jiayl2 2015/07/20 23:27:06 nit: this is useful not only for mobile devices, b
tkchin_webrtc 2015/07/21 20:39:13 Done.
542 public:
543 // Use this constructor for reading a directory previously written to with
544 // this stream.
545 MobileDeviceLogStream(const std::string& dir_path);
546 // Use this constructor for writing to a directory. Files in the directory
547 // matching what's used by the stream will be deleted.
548 MobileDeviceLogStream(const std::string& dir_path, size_t max_total_log_size);
549 ~MobileDeviceLogStream() override {}
550
551 protected:
552 void OnRotation() override;
553
554 private:
555 static size_t GetRotatingLogSize(size_t max_total_log_size);
556 static size_t GetNumRotatingLogFiles(size_t max_total_log_size);
557 static const char* kLogPrefix;
558 static const size_t kRotatingLogFileDefaultSize;
559
560 const size_t max_total_log_size_;
561 size_t num_rotations_;
562
563 DISALLOW_COPY_AND_ASSIGN(MobileDeviceLogStream);
564 };
565
421 // A stream that caps the output at a certain size, dropping content from the 566 // A stream that caps the output at a certain size, dropping content from the
422 // middle of the logical stream and maintaining equal parts of the start/end of 567 // middle of the logical stream and maintaining equal parts of the start/end of
423 // the logical stream. 568 // the logical stream.
424 class CircularFileStream : public FileStream { 569 class CircularFileStream : public FileStream {
425 public: 570 public:
426 explicit CircularFileStream(size_t max_size); 571 explicit CircularFileStream(size_t max_size);
427 572
428 bool Open(const std::string& filename, const char* mode, int* error) override; 573 bool Open(const std::string& filename, const char* mode, int* error) override;
429 StreamResult Read(void* buffer, 574 StreamResult Read(void* buffer,
430 size_t buffer_len, 575 size_t buffer_len,
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 // as a pass in parameter, it indicates data in buffer that should move to sink 872 // as a pass in parameter, it indicates data in buffer that should move to sink
728 StreamResult Flow(StreamInterface* source, 873 StreamResult Flow(StreamInterface* source,
729 char* buffer, size_t buffer_len, 874 char* buffer, size_t buffer_len,
730 StreamInterface* sink, size_t* data_len = NULL); 875 StreamInterface* sink, size_t* data_len = NULL);
731 876
732 /////////////////////////////////////////////////////////////////////////////// 877 ///////////////////////////////////////////////////////////////////////////////
733 878
734 } // namespace rtc 879 } // namespace rtc
735 880
736 #endif // WEBRTC_BASE_STREAM_H_ 881 #endif // WEBRTC_BASE_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698