|
|
|
@ -11,14 +11,15 @@
|
|
|
|
|
#include "rtc_base/filerotatingstream.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/fileutils.h"
|
|
|
|
|
#include "rtc_base/pathutils.h"
|
|
|
|
|
#include "rtc_base/strings/string_builder.h"
|
|
|
|
|
|
|
|
|
|
// Note: We use std::cerr for logging in the write paths of this stream to avoid
|
|
|
|
|
// Note: We use fprintf for logging in the write paths of this stream to avoid
|
|
|
|
|
// infinite loops when logging.
|
|
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
@ -149,7 +150,7 @@ StreamResult FileRotatingStream::Write(const void* data,
|
|
|
|
|
return SR_EOS;
|
|
|
|
|
}
|
|
|
|
|
if (!file_stream_) {
|
|
|
|
|
std::cerr << "Open() must be called before Write." << std::endl;
|
|
|
|
|
std::fprintf(stderr, "Open() must be called before Write.\n");
|
|
|
|
|
return SR_ERROR;
|
|
|
|
|
}
|
|
|
|
|
// Write as much as will fit in to the current file.
|
|
|
|
@ -213,7 +214,7 @@ bool FileRotatingStream::Open() {
|
|
|
|
|
std::vector<std::string> matching_files = GetFilesWithPrefix();
|
|
|
|
|
for (auto matching_file : matching_files) {
|
|
|
|
|
if (!Filesystem::DeleteFile(matching_file)) {
|
|
|
|
|
std::cerr << "Failed to delete: " << matching_file << std::endl;
|
|
|
|
|
std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return OpenCurrentFile();
|
|
|
|
@ -225,8 +226,7 @@ bool FileRotatingStream::Open() {
|
|
|
|
|
bool FileRotatingStream::DisableBuffering() {
|
|
|
|
|
disable_buffering_ = true;
|
|
|
|
|
if (!file_stream_) {
|
|
|
|
|
std::cerr << "Open() must be called before DisableBuffering()."
|
|
|
|
|
<< std::endl;
|
|
|
|
|
std::fprintf(stderr, "Open() must be called before DisableBuffering().\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return file_stream_->DisableBuffering();
|
|
|
|
@ -257,8 +257,8 @@ bool FileRotatingStream::OpenCurrentFile() {
|
|
|
|
|
}
|
|
|
|
|
int error = 0;
|
|
|
|
|
if (!file_stream_->Open(file_path, mode, &error)) {
|
|
|
|
|
std::cerr << "Failed to open: " << file_path << "Error: " << error
|
|
|
|
|
<< std::endl;
|
|
|
|
|
std::fprintf(stderr, "Failed to open: %s Error: %i\n", file_path.c_str(),
|
|
|
|
|
error);
|
|
|
|
|
file_stream_.reset();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -286,7 +286,7 @@ void FileRotatingStream::RotateFiles() {
|
|
|
|
|
std::string file_to_delete = file_names_[rotation_index_];
|
|
|
|
|
if (Filesystem::IsFile(file_to_delete)) {
|
|
|
|
|
if (!Filesystem::DeleteFile(file_to_delete)) {
|
|
|
|
|
std::cerr << "Failed to delete: " << file_to_delete << std::endl;
|
|
|
|
|
std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (auto i = rotation_index_; i > 0; --i) {
|
|
|
|
@ -294,8 +294,8 @@ void FileRotatingStream::RotateFiles() {
|
|
|
|
|
std::string unrotated_name = file_names_[i - 1];
|
|
|
|
|
if (Filesystem::IsFile(unrotated_name)) {
|
|
|
|
|
if (!Filesystem::MoveFile(unrotated_name, rotated_name)) {
|
|
|
|
|
std::cerr << "Failed to move: " << unrotated_name << " to "
|
|
|
|
|
<< rotated_name << std::endl;
|
|
|
|
|
std::fprintf(stderr, "Failed to move: %s to %s\n",
|
|
|
|
|
unrotated_name.c_str(), rotated_name.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -327,7 +327,8 @@ std::vector<std::string> FileRotatingStream::GetFilesWithPrefix() const {
|
|
|
|
|
std::string FileRotatingStream::GetFilePath(size_t index,
|
|
|
|
|
size_t num_files) const {
|
|
|
|
|
RTC_DCHECK_LT(index, num_files);
|
|
|
|
|
std::ostringstream file_name;
|
|
|
|
|
char buf[1024];
|
|
|
|
|
rtc::SimpleStringBuilder file_name(buf);
|
|
|
|
|
// The format will be "_%<num_digits>zu". We want to zero pad the index so
|
|
|
|
|
// that it will sort nicely.
|
|
|
|
|
size_t max_digits = ((num_files - 1) / 10) + 1;
|
|
|
|
|