Update daily_rotating_file_sink.h

pull/862/head
Ryan Hsu 7 years ago committed by GitHub
parent 3ec6f7511e
commit ee2a5dbbc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -41,8 +41,19 @@ public:
{ {
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
} }
auto now = log_clock::now(); auto now = log_clock::now();
file_helper_.open(calc_filename(base_filename_, now_tm(now)), truncate_); today_filename_ = calc_filename(base_filename_, now_tm(now));
for (max_files_ = 0; ; ++max_files_)
{
filename_t src = calc_filename(today_filename_, max_files_);
if (!details::file_helper::file_exists(src))
{
break;
}
}
file_helper_.open(today_filename_, truncate_);
rotation_tp_ = next_rotation_tp_(); rotation_tp_ = next_rotation_tp_();
current_size_ = file_helper_.size(); // expensive. called only once current_size_ = file_helper_.size(); // expensive. called only once
} }
@ -107,14 +118,15 @@ private:
{ {
using details::os::filename_to_str; using details::os::filename_to_str;
file_helper_.close(); file_helper_.close();
for (auto i = max_files_; i >= 0; --i) ++max_files_;
for (auto i = max_files_; i > 0; --i)
{ {
filename_t src = calc_filename(today_filename_, i); filename_t src = calc_filename(today_filename_, i - 1);
if (!details::file_helper::file_exists(src)) if (!details::file_helper::file_exists(src))
{ {
continue; continue;
} }
filename_t target = calc_filename(today_filename_, i + 1); filename_t target = calc_filename(today_filename_, i);
if (!rename_file(src, target)) if (!rename_file(src, target))
{ {
@ -130,7 +142,6 @@ private:
} }
} }
} }
++max_files_;
file_helper_.reopen(true); file_helper_.reopen(true);
} }

Loading…
Cancel
Save