Commit 2fa7410c authored by gabime's avatar gabime

Improve rotating sink error handling

parent 3771d129
......@@ -86,24 +86,21 @@ private:
for (auto i = max_files_; i > 0; --i)
{
filename_t src = calc_filename(base_filename_, i - 1);
filename_t target = calc_filename(base_filename_, i);
if (details::file_helper::file_exists(target))
{
if (details::os::remove(target) != 0)
if (!details::file_helper::file_exists(src))
{
throw spdlog_ex("rotating_file_sink: failed removing " + filename_to_str(target), errno);
continue;
}
}
if (details::file_helper::file_exists(src) && details::os::rename(src, target) != 0)
filename_t target = calc_filename(base_filename_, i);
if (!rename_file(src, target))
{
// if failed try again after small delay.
// if failed try again after a small delay.
// this is a workaround to a windows issue, where very high rotation
// rates sometimes fail (because of antivirus?).
details::os::sleep_for_millis(20);
details::os::remove(target);
if (details::os::rename(src, target) != 0)
// rates can cause the rename to fail with permission denied (because of antivirus?).
details::os::sleep_for_millis(100);
if (!rename_file(src, target))
{
file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
throw spdlog_ex(
"rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
}
......@@ -112,6 +109,15 @@ private:
file_helper_.reopen(true);
}
// delete the target if exists, and rename the src file to target
// return true on success, false otherwise.
bool rename_file(const filename_t &src_filename, const filename_t &target_filename)
{
// try to delete the target file in case it already exists.
(void)details::os::remove(target_filename);
return details::os::rename(src_filename, target_filename) == 0;
}
filename_t base_filename_;
std::size_t max_size_;
std::size_t max_files_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment