|
|
|
@ -23,7 +23,7 @@ namespace sinks {
|
|
|
|
|
//
|
|
|
|
|
// Rotating file sink based on size
|
|
|
|
|
//
|
|
|
|
|
template<typename Mutex>
|
|
|
|
|
template<typename Mutex, typename FileNameCalc = details::filename_calculator>
|
|
|
|
|
class rotating_file_sink final : public base_sink<Mutex>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
@ -32,28 +32,10 @@ public:
|
|
|
|
|
, max_size_(max_size)
|
|
|
|
|
, max_files_(max_files)
|
|
|
|
|
{
|
|
|
|
|
file_helper_.open(calc_filename(base_filename_, 0));
|
|
|
|
|
file_helper_.open(FileNameCalc::calc_filename(base_filename_, 0));
|
|
|
|
|
current_size_ = file_helper_.size(); // expensive. called only once
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// calc filename according to index and file extension if exists.
|
|
|
|
|
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
|
|
|
|
|
static filename_t calc_filename(const filename_t &filename, std::size_t index)
|
|
|
|
|
{
|
|
|
|
|
typename std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w;
|
|
|
|
|
if (index != 0u)
|
|
|
|
|
{
|
|
|
|
|
filename_t basename, ext;
|
|
|
|
|
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
|
|
|
|
|
fmt::format_to(w, SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fmt::format_to(w, SPDLOG_FILENAME_T("{}"), filename);
|
|
|
|
|
}
|
|
|
|
|
return fmt::to_string(w);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void sink_it_(const details::log_msg &msg) override
|
|
|
|
|
{
|
|
|
|
@ -85,20 +67,20 @@ private:
|
|
|
|
|
file_helper_.close();
|
|
|
|
|
for (auto i = max_files_; i > 0; --i)
|
|
|
|
|
{
|
|
|
|
|
filename_t src = calc_filename(base_filename_, i - 1);
|
|
|
|
|
filename_t src = FileNameCalc::calc_filename(base_filename_, i - 1);
|
|
|
|
|
if (!details::file_helper::file_exists(src))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
filename_t target = calc_filename(base_filename_, i);
|
|
|
|
|
filename_t target = FileNameCalc::calc_filename(base_filename_, i);
|
|
|
|
|
|
|
|
|
|
if (!rename_file(src, target))
|
|
|
|
|
if (!FileNameCalc::rename_file(src, target))
|
|
|
|
|
{
|
|
|
|
|
// if failed try again after a small delay.
|
|
|
|
|
// this is a workaround to a windows issue, where very high rotation
|
|
|
|
|
// rates can cause the rename to fail with permission denied (because of antivirus?).
|
|
|
|
|
details::os::sleep_for_millis(100);
|
|
|
|
|
if (!rename_file(src, target))
|
|
|
|
|
if (!FileNameCalc::rename_file(src, target))
|
|
|
|
|
{
|
|
|
|
|
file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
|
|
|
|
|
throw spdlog_ex(
|
|
|
|
@ -109,15 +91,6 @@ 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_;
|
|
|
|
|