pull/13/merge
mintyc 11 years ago
commit b093f9a7fa

@ -72,13 +72,13 @@ class rotating_file_sink : public base_sink<Mutex>
public: public:
rotating_file_sink(const std::string &base_filename, const std::string &extension, 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 max_size, std::size_t max_files,
std::size_t flush_inverval=0): std::size_t flush_interval=0):
_base_filename(base_filename), _base_filename(base_filename),
_extension(extension), _extension(extension),
_max_size(max_size), _max_size(max_size),
_max_files(max_files), _max_files(max_files),
_current_size(0), _current_size(0),
_file_helper(flush_inverval) _file_helper(flush_interval)
{ {
_file_helper.open(calc_filename(_base_filename, 0, _extension)); _file_helper.open(calc_filename(_base_filename, 0, _extension));
} }
@ -111,36 +111,36 @@ private:
// Rotate files: // Rotate files:
// log.txt -> log.1.txt // log.txt -> log.1.txt
// log.1.txt -> log2.txt // log.1.txt -> log.2.txt
// log.2.txt -> log3.txt // log.2.txt -> log.3.txt
// log.3.txt -> delete // log.3.txt -> delete
void _rotate() void _rotate()
{ {
_file_helper.close(); _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 src = calc_filename(_base_filename, i - 1, _extension);
std::string target = calc_filename(_base_filename, i, _extension); std::string target = calc_filename(_base_filename, i, _extension);
if (details::file_helper::file_exists(target)) if (details::file_helper::file_exists(target))
{ {
if (std::remove(target.c_str()) != 0) if (std::remove(target.c_str()) != 0)
{ {
throw spdlog_ex("rotating_file_sink: failed removing " + target); throw spdlog_ex("rotating_file_sink: failed removing " + target);
} }
} }
if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str())) 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); throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target);
} }
} }
auto cur_name = _file_helper.filename(); auto cur_name = _file_helper.filename();
if (std::remove(cur_name.c_str()) != 0) if (details::file_helper::file_exists(cur_name))
{ {
throw spdlog_ex("rotating_file_sink: failed removing " + cur_name); throw spdlog_ex("rotating_file_sink: " + cur_name + " still exists after rotating");
} }
_file_helper.open(cur_name); _file_helper.open(cur_name);
} }
std::string _base_filename; std::string _base_filename;

Loading…
Cancel
Save