Handle FileRotatingStreams with long file names

Bug: webrtc:9392
Change-Id: I7b42b1a6ed1b646c244bc64f1bad92a2f38e5539
Reviewed-on: https://webrtc-review.googlesource.com/83162
Commit-Queue: Jonas Olsson <jonasolsson@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23608}
This commit is contained in:
Jonas Olsson
2018-06-14 09:57:39 +02:00
committed by Commit Bot
parent 1b36894f06
commit 671cae2c7c

View File

@ -327,22 +327,15 @@ std::vector<std::string> FileRotatingStream::GetFilesWithPrefix() const {
std::string FileRotatingStream::GetFilePath(size_t index, std::string FileRotatingStream::GetFilePath(size_t index,
size_t num_files) const { size_t num_files) const {
RTC_DCHECK_LT(index, num_files); RTC_DCHECK_LT(index, num_files);
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;
size_t num_digits = (index / 10) + 1;
RTC_DCHECK_LE(num_digits, max_digits);
size_t padding = max_digits - num_digits;
file_name << file_prefix_ << "_"; const size_t buffer_size = 32;
for (size_t i = 0; i < padding; ++i) { char file_postfix[buffer_size];
file_name << "0"; // We want to zero pad the index so that it will sort nicely.
} const int max_digits = std::snprintf(nullptr, 0, "%zu", num_files - 1);
file_name << index; RTC_DCHECK_LT(1 + max_digits, buffer_size);
std::snprintf(file_postfix, buffer_size, "_%0*zu", max_digits, index);
Pathname file_path(dir_path_, file_name.str()); Pathname file_path(dir_path_, file_prefix_ + file_postfix);
return file_path.pathname(); return file_path.pathname();
} }