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 <cstdio> 12 #include <memory> 13 #include <mutex> 14 15 namespace spdlog 16 { 17 namespace sinks 18 { 19 20 template <class Mutex> 21 class stdout_sink: public base_sink<Mutex> 22 { 23 using MyType = stdout_sink<Mutex>; 24 public: stdout_sink()25 stdout_sink() 26 {} instance()27 static std::shared_ptr<MyType> instance() 28 { 29 static std::shared_ptr<MyType> instance = std::make_shared<MyType>(); 30 return instance; 31 } 32 _sink_it(const details::log_msg & msg)33 void _sink_it(const details::log_msg& msg) override 34 { 35 fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stdout); 36 flush(); 37 } 38 flush()39 void flush() override 40 { 41 fflush(stdout); 42 } 43 }; 44 45 typedef stdout_sink<details::null_mutex> stdout_sink_st; 46 typedef stdout_sink<std::mutex> stdout_sink_mt; 47 48 49 template <class Mutex> 50 class stderr_sink: public base_sink<Mutex> 51 { 52 using MyType = stderr_sink<Mutex>; 53 public: stderr_sink()54 stderr_sink() 55 {} instance()56 static std::shared_ptr<MyType> instance() 57 { 58 static std::shared_ptr<MyType> instance = std::make_shared<MyType>(); 59 return instance; 60 } 61 _sink_it(const details::log_msg & msg)62 void _sink_it(const details::log_msg& msg) override 63 { 64 fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stderr); 65 flush(); 66 } 67 flush()68 void flush() override 69 { 70 fflush(stderr); 71 } 72 }; 73 74 typedef stderr_sink<std::mutex> stderr_sink_mt; 75 typedef stderr_sink<details::null_mutex> stderr_sink_st; 76 } 77 } 78