From b7b421c8fcf701ab25be064b33a2080a14ab3833 Mon Sep 17 00:00:00 2001 From: gabi Date: Wed, 28 Jan 2015 23:36:16 +0200 Subject: [PATCH] Added auto flush to stdout & stderr sinks --- include/spdlog/sinks/ostream_sink.h | 5 ++++- include/spdlog/sinks/stdout_sinks.h | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h index 68ecec87..91580301 100644 --- a/include/spdlog/sinks/ostream_sink.h +++ b/include/spdlog/sinks/ostream_sink.h @@ -39,7 +39,7 @@ template class ostream_sink: public base_sink { public: - explicit ostream_sink(std::ostream& os) :_ostream(os) {} + explicit ostream_sink(std::ostream& os, bool force_flush=false) :_ostream(os), _force_flush(force_flush) {} ostream_sink(const ostream_sink&) = delete; ostream_sink& operator=(const ostream_sink&) = delete; virtual ~ostream_sink() = default; @@ -48,8 +48,11 @@ protected: virtual void _sink_it(const details::log_msg& msg) override { _ostream.write(msg.formatted.data(), msg.formatted.size()); + if (_force_flush) + _ostream.flush(); } std::ostream& _ostream; + bool _force_flush; }; typedef ostream_sink ostream_sink_mt; diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index b633b2dc..e944e273 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -38,7 +38,7 @@ template class stdout_sink : public ostream_sink { public: - stdout_sink() : ostream_sink(std::cout) {} + stdout_sink() : ostream_sink(std::cout, true) {} }; @@ -50,7 +50,7 @@ template class stderr_sink : public ostream_sink { public: - stderr_sink() : ostream_sink(std::cerr) {} + stderr_sink() : ostream_sink(std::cerr, true) {} }; typedef stderr_sink stderr_sink_mt;