1 // 2 // Copyright(c) 2015 Gabi Melman. 3 // Distributed under the MIT License (http://opensource.org/licenses/MIT) 4 // 5 6 #pragma once 7 8 #include <spdlog/details/null_mutex.h> 9 #include <spdlog/sinks/base_sink.h> 10 11 #include <ostream> 12 #include <mutex> 13 14 namespace spdlog 15 { 16 namespace sinks 17 { 18 template<class Mutex> 19 class ostream_sink: public base_sink<Mutex> 20 { 21 public: _ostream(os)22 explicit ostream_sink(std::ostream& os, bool force_flush=false) :_ostream(os), _force_flush(force_flush) {} 23 ostream_sink(const ostream_sink&) = delete; 24 ostream_sink& operator=(const ostream_sink&) = delete; 25 virtual ~ostream_sink() = default; 26 27 protected: _sink_it(const details::log_msg & msg)28 void _sink_it(const details::log_msg& msg) override 29 { 30 _ostream.write(msg.formatted.data(), msg.formatted.size()); 31 if (_force_flush) 32 _ostream.flush(); 33 } 34 flush()35 void flush() override 36 { 37 _ostream.flush(); 38 } 39 40 std::ostream& _ostream; 41 bool _force_flush; 42 }; 43 44 typedef ostream_sink<std::mutex> ostream_sink_mt; 45 typedef ostream_sink<details::null_mutex> ostream_sink_st; 46 } 47 } 48