diff --git a/include/spdlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h index 847af960..5dc3e6d9 100644 --- a/include/spdlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -72,13 +72,13 @@ class rotating_file_sink : public base_sink public: rotating_file_sink(const std::string &base_filename, const std::string &extension, std::size_t max_size, std::size_t max_files, - std::size_t flush_inverval=0): + std::size_t flush_interval=0): _base_filename(base_filename), _extension(extension), _max_size(max_size), _max_files(max_files), _current_size(0), - _file_helper(flush_inverval) + _file_helper(flush_interval) { _file_helper.open(calc_filename(_base_filename, 0, _extension)); } @@ -111,36 +111,36 @@ private: // Rotate files: // log.txt -> log.1.txt - // log.1.txt -> log2.txt - // log.2.txt -> log3.txt + // log.1.txt -> log.2.txt + // log.2.txt -> log.3.txt // log.3.txt -> delete void _rotate() { _file_helper.close(); - for (auto i = _max_files; i > 0; --i) + for (auto i = _max_files - 1; i > 0; --i) { std::string src = calc_filename(_base_filename, i - 1, _extension); std::string target = calc_filename(_base_filename, i, _extension); if (details::file_helper::file_exists(target)) - { - if (std::remove(target.c_str()) != 0) - { - throw spdlog_ex("rotating_file_sink: failed removing " + target); - } - } + { + if (std::remove(target.c_str()) != 0) + { + throw spdlog_ex("rotating_file_sink: failed removing " + target); + } + } if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str())) { throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target); } } auto cur_name = _file_helper.filename(); - if (std::remove(cur_name.c_str()) != 0) - { - throw spdlog_ex("rotating_file_sink: failed removing " + cur_name); - } + if (details::file_helper::file_exists(cur_name)) + { + throw spdlog_ex("rotating_file_sink: " + cur_name + " still exists after rotating"); + } _file_helper.open(cur_name); } std::string _base_filename;