From eb2118c181455991c768a607ec40ca3b87830388 Mon Sep 17 00:00:00 2001 From: puasonych Date: Wed, 30 May 2018 09:40:09 +0500 Subject: [PATCH] Update step_file_sink --- include/spdlog/contrib/sinks/step_file_sink.h | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/include/spdlog/contrib/sinks/step_file_sink.h b/include/spdlog/contrib/sinks/step_file_sink.h index f88b7566..9010eda4 100644 --- a/include/spdlog/contrib/sinks/step_file_sink.h +++ b/include/spdlog/contrib/sinks/step_file_sink.h @@ -75,7 +75,15 @@ public: { throw spdlog_ex("step_file_sink: Invalid time step in ctor"); } - if (max_size == 0) + + if (!file_header.empty()) + { + pattern_formatter formatter_for_file_header("%v"); + _file_header.raw << file_header; + formatter_for_file_header.format(_file_header); + } + + if (max_size <= _file_header.formatted.size()) { throw spdlog_ex("step_file_sink: Invalid max log size in ctor"); } @@ -88,13 +96,6 @@ public: throw spdlog_ex("step_file_sink: The temporary extension matches the specified in ctor"); } - if (!file_header.empty()) - { - pattern_formatter formatter_for_file_header("%v"); - _file_header.raw << file_header; - formatter_for_file_header.format(_file_header); - } - _file_helper.open(_current_filename); _current_size = _file_helper.size(); // expensive. called only once @@ -120,14 +121,19 @@ protected: if (std::chrono::system_clock::now() >= _tp || _current_size + msg_size > _max_size) { - close_current_file(); + filename_t new_filename; + std::tie(new_filename, std::ignore) = FileNameCalc::calc_filename(_base_filename, _tmp_ext); + if (new_filename != _current_filename) + { + close_current_file(); - std::tie(_current_filename, std::ignore) = FileNameCalc::calc_filename(_base_filename, _tmp_ext); - _file_helper.open(_current_filename); - _tp = _next_tp(); + // std::tie(_current_filename, std::ignore) = FileNameCalc::calc_filename(_base_filename, _tmp_ext); + _file_helper.open(_current_filename); + _tp = _next_tp(); - _current_size = _file_header.formatted.size(); - if (_current_size) _file_helper.write(_file_header); + _current_size = _file_header.formatted.size(); + if (_current_size) _file_helper.write(_file_header); + } } _current_size += msg_size;