From 095cb1f560a16631d56d21afa37ac175f83098ca Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 21 May 2017 03:43:41 +0300 Subject: [PATCH] Added _flush() to base_sink --- include/spdlog/sinks/ansicolor_sink.h | 10 +++++---- include/spdlog/sinks/base_sink.h | 5 +++++ include/spdlog/sinks/dist_sink.h | 14 +++++++------ include/spdlog/sinks/file_sinks.h | 29 +++++++++++++++------------ include/spdlog/sinks/msvc_sink.h | 7 ++++--- include/spdlog/sinks/null_sink.h | 2 +- include/spdlog/sinks/stdout_sinks.h | 8 ++++---- include/spdlog/sinks/wincolor_sink.h | 3 ++- 8 files changed, 46 insertions(+), 32 deletions(-) diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index d0871737..48d72f94 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -42,10 +42,7 @@ public: flush(); } - void flush() override - { - fflush(target_file_); - } + protected: virtual void _sink_it(const details::log_msg& msg) override @@ -66,6 +63,11 @@ protected: } flush(); } + + void _flush() override + { + fflush(target_file_); + } FILE* target_file_; bool should_do_colors_; std::map colors_; diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index c0a910e0..978ade14 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -36,9 +36,14 @@ public: std::lock_guard lock(_mutex); _sink_it(msg); } + void flush() SPDLOG_FINAL override + { + _flush(); + } protected: virtual void _sink_it(const details::log_msg& msg) = 0; + virtual void _flush() = 0; Mutex _mutex; }; } diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index cef08bfb..b1d35d2c 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -44,13 +44,15 @@ protected: } } + void _flush() override + { + std::lock_guard lock(base_sink::_mutex); + for (auto &sink : _sinks) + sink->flush(); + } + public: - void flush() override - { - std::lock_guard lock(base_sink::_mutex); - for (auto &sink : _sinks) - sink->flush(); - } + void add_sink(std::shared_ptr sink) { diff --git a/include/spdlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h index fe40a362..5726117a 100644 --- a/include/spdlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -33,10 +33,7 @@ public: { _file_helper.open(filename, truncate); } - void flush() override - { - _file_helper.flush(); - } + void set_force_flush(bool force_flush) { _force_flush = force_flush; @@ -49,6 +46,10 @@ protected: if(_force_flush) _file_helper.flush(); } + void _flush() override + { + _file_helper.flush(); + } private: details::file_helper _file_helper; bool _force_flush; @@ -76,11 +77,7 @@ public: _current_size = _file_helper.size(); //expensive. called only once } - void flush() override - { - _file_helper.flush(); - } - + protected: void _sink_it(const details::log_msg& msg) override { @@ -93,6 +90,11 @@ protected: _file_helper.write(msg); } + void _flush() override + { + _file_helper.flush(); + } + private: static filename_t calc_filename(const filename_t& filename, std::size_t index) { @@ -194,10 +196,6 @@ public: _file_helper.open(FileNameCalc::calc_filename(_base_filename)); } - void flush() override - { - _file_helper.flush(); - } protected: void _sink_it(const details::log_msg& msg) override @@ -210,6 +208,11 @@ protected: _file_helper.write(msg); } + void _flush() override + { + _file_helper.flush(); + } + private: std::chrono::system_clock::time_point _next_rotation_tp() { diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index 16342ca2..1b6c880f 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -30,15 +30,16 @@ public: { } - void flush() override - { - } + protected: void _sink_it(const details::log_msg& msg) override { OutputDebugStringA(msg.formatted.c_str()); } + + void _flush() override + {} }; typedef msvc_sink msvc_sink_mt; diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h index 1d427aa2..8ae21fc2 100644 --- a/include/spdlog/sinks/null_sink.h +++ b/include/spdlog/sinks/null_sink.h @@ -22,7 +22,7 @@ protected: void _sink_it(const details::log_msg&) override {} - void flush() override + void _flush() override {} }; diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index 4dd52ed8..b5109291 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -29,14 +29,14 @@ public: static std::shared_ptr instance = std::make_shared(); return instance; } - +protected: void _sink_it(const details::log_msg& msg) override { fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stdout); flush(); } - void flush() override + void _flush() override { fflush(stdout); } @@ -58,14 +58,14 @@ public: static std::shared_ptr instance = std::make_shared(); return instance; } - +protected: void _sink_it(const details::log_msg& msg) override { fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stderr); flush(); } - void flush() override + void _flush() override { fflush(stderr); } diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 63ecbe21..379b7ef0 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -50,6 +50,7 @@ public: wincolor_sink(const wincolor_sink& other) = delete; wincolor_sink& operator=(const wincolor_sink& other) = delete; +protected: virtual void _sink_it(const details::log_msg& msg) override { auto color = colors_[msg.level]; @@ -58,7 +59,7 @@ public: SetConsoleTextAttribute(out_handle_, orig_attribs); //reset to orig colors } - virtual void flush() override + virtual void _flush() override { // windows console always flushed? }