From ea1a92769b270f520d335087606c5cb8944ae776 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 14 Aug 2015 21:16:08 +0300 Subject: [PATCH] Fixed rotating_logger_mt does not rotate properly if app restarts #116 --- include/spdlog/details/file_helper.h | 25 +++++++++++++++++++++++++ include/spdlog/sinks/file_sinks.h | 1 + 2 files changed, 26 insertions(+) diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 5062e977..4ba556ac 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -113,6 +113,29 @@ public: } + long size() + { + if (!_fd) + throw spdlog_ex("Cannot use size() on closed file " + _filename); + + auto pos = ftell(_fd); + if (fseek(_fd, 0, SEEK_END) != 0) + throw spdlog_ex("fseek failed on file " + _filename); + + auto size = ftell(_fd); + + if(fseek(_fd, pos, SEEK_SET) !=0) + throw spdlog_ex("fseek failed on file " + _filename); + + if (size == -1) + throw spdlog_ex("ftell failed on file " + _filename); + + + return size; + + + } + const std::string& filename() const { return _filename; @@ -132,6 +155,8 @@ public: } } + + private: FILE* _fd; std::string _filename; diff --git a/include/spdlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h index 8d36f316..6c8f2df0 100644 --- a/include/spdlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -82,6 +82,7 @@ public: _file_helper(force_flush) { _file_helper.open(calc_filename(_base_filename, 0, _extension)); + _current_size = _file_helper.size(); //expensive. called only once } void flush() override