123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- #ifndef RTC_BASE_FILE_ROTATING_STREAM_H_
- #define RTC_BASE_FILE_ROTATING_STREAM_H_
- #include <stddef.h>
- #include <memory>
- #include <string>
- #include <vector>
- #include "rtc_base/constructor_magic.h"
- #include "rtc_base/stream.h"
- #include "rtc_base/system/file_wrapper.h"
- namespace rtc {
- class FileRotatingStream : public StreamInterface {
- public:
-
-
- FileRotatingStream(const std::string& dir_path,
- const std::string& file_prefix);
-
-
- FileRotatingStream(const std::string& dir_path,
- const std::string& file_prefix,
- size_t max_file_size,
- size_t num_files);
- ~FileRotatingStream() override;
-
- StreamState GetState() const override;
- StreamResult Read(void* buffer,
- size_t buffer_len,
- size_t* read,
- int* error) override;
- StreamResult Write(const void* data,
- size_t data_len,
- size_t* written,
- int* error) override;
- bool Flush() override;
- void Close() override;
-
- bool Open();
-
-
- bool DisableBuffering();
-
-
-
- std::string GetFilePath(size_t index) const;
-
- size_t GetNumFiles() const { return file_names_.size(); }
- protected:
- size_t GetMaxFileSize() const { return max_file_size_; }
- void SetMaxFileSize(size_t size) { max_file_size_ = size; }
- size_t GetRotationIndex() const { return rotation_index_; }
- void SetRotationIndex(size_t index) { rotation_index_ = index; }
- virtual void OnRotation() {}
- private:
- bool OpenCurrentFile();
- void CloseCurrentFile();
-
-
-
-
-
-
- void RotateFiles();
-
- std::string GetFilePath(size_t index, size_t num_files) const;
- const std::string dir_path_;
- const std::string file_prefix_;
-
- webrtc::FileWrapper file_;
-
- std::vector<std::string> file_names_;
- size_t max_file_size_;
- size_t current_file_index_;
-
-
- size_t rotation_index_;
-
-
- size_t current_bytes_written_;
- bool disable_buffering_;
- RTC_DISALLOW_COPY_AND_ASSIGN(FileRotatingStream);
- };
- class CallSessionFileRotatingStream : public FileRotatingStream {
- public:
-
-
-
- CallSessionFileRotatingStream(const std::string& dir_path,
- size_t max_total_log_size);
- ~CallSessionFileRotatingStream() override {}
- protected:
- void OnRotation() override;
- private:
- static size_t GetRotatingLogSize(size_t max_total_log_size);
- static size_t GetNumRotatingLogFiles(size_t max_total_log_size);
- static const size_t kRotatingLogFileDefaultSize;
- const size_t max_total_log_size_;
- size_t num_rotations_;
- RTC_DISALLOW_COPY_AND_ASSIGN(CallSessionFileRotatingStream);
- };
- class FileRotatingStreamReader {
- public:
- FileRotatingStreamReader(const std::string& dir_path,
- const std::string& file_prefix);
- ~FileRotatingStreamReader();
- size_t GetSize() const;
- size_t ReadAll(void* buffer, size_t size) const;
- private:
- std::vector<std::string> file_names_;
- };
- class CallSessionFileRotatingStreamReader : public FileRotatingStreamReader {
- public:
- CallSessionFileRotatingStreamReader(const std::string& dir_path);
- };
- }
- #endif
|