From 8e3572aa188988f7b7025583ae48a9f998879802 Mon Sep 17 00:00:00 2001 From: Christopher Torres Date: Sun, 24 Apr 2016 22:40:25 -0400 Subject: [PATCH] wincolor_sink is not a wrapper class anymore. Removed my addition to the spdlog create_console_logger function --- include/spdlog/details/spdlog_impl.h | 11 ------- include/spdlog/sinks/wincolor_sink.h | 48 +++++++++++++++++++--------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 0d599697..1414c8a8 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -14,9 +14,6 @@ #include #include #include -#ifdef _WIN32 -#include -#endif #include #include @@ -64,22 +61,14 @@ inline std::shared_ptr spdlog::daily_logger_st(const std::string inline std::shared_ptr create_console_logger_st(const std::string& logger_name, spdlog::sink_ptr sink, bool color) { if (color) //use color wrapper sink -#ifdef _WIN32 - sink = std::make_shared(sink); -#else sink = std::make_shared(sink); -#endif return spdlog::details::registry::instance().create(logger_name, sink); } inline std::shared_ptr create_console_logger_mt(const std::string& logger_name, spdlog::sink_ptr sink, bool color) { if (color) //use color wrapper sink -#ifdef _WIN32 - sink = std::make_shared(sink); -#else sink = std::make_shared(sink); -#endif return spdlog::details::registry::instance().create(logger_name, sink); } diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 4acff262..5bc744d9 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -25,7 +25,7 @@ template class wincolor_sink : public base_sink { public: - wincolor_sink(sink_ptr wrapped_sink); + wincolor_sink(std::ostream& os, bool force_flush=false); virtual ~wincolor_sink(); wincolor_sink(const wincolor_sink& other) = delete; @@ -34,7 +34,7 @@ public: virtual void flush() override; // Formatting codes - const short reset = 0; + const short reset = FOREGROUND_INTENSITY; const short bold = FOREGROUND_INTENSITY; const short dark = reset; // Not implemented in windows const short underline = reset; // Not implemented in windows @@ -45,6 +45,7 @@ public: const short concealed = reset; // Not implemented in windows // Foreground colors + const short black = 0; const short grey = bold; const short red = FOREGROUND_RED; const short green = FOREGROUND_GREEN; @@ -65,20 +66,23 @@ public: const short on_white = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; void set_color( level::level_enum level, const short& color ); - sink_ptr& wrapped_sink(); protected: virtual void _sink_it(const details::log_msg& msg) override; - sink_ptr sink_; + void SetConsoleColor( WORD* Attributes, DWORD Color ); + void ResetConsoleColor( WORD Attributes ); + std::map colors_; + std::ostream& _ostream; + bool _force_flush; }; typedef wincolor_sink wincolor_sink_st; typedef wincolor_sink wincolor_sink_mt; template -inline wincolor_sink::wincolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink) +inline wincolor_sink::wincolor_sink(std::ostream& os, bool force_flush=false) :_ostream(os), _force_flush(force_flush) { colors_[level::trace] = cyan; colors_[level::debug] = cyan; @@ -97,17 +101,18 @@ template inline void wincolor_sink::_sink_it( const details::log_msg& msg ) { // Wrap the originally formatted message in color codes - SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), colors_[msg.level]); - SetConsoleTextAttribute(GetStdHandle( STD_ERROR_HANDLE ), colors_[msg.level]); - sink_->log( msg ); - SetConsoleTextAttribute(GetStdHandle( STD_ERROR_HANDLE ), reset); - SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), reset); + WORD Attributes = 0; + SetConsoleColor(&Attributes, colors_[msg.level]); + _ostream.write( msg.formatted.data(), msg.formatted.size() ); + if (_force_flush) + _ostream.flush(); + ResetConsoleColor(Attributes); } template inline void wincolor_sink::flush() { - sink_->flush(); + _ostream.flush(); } template @@ -117,17 +122,30 @@ inline void wincolor_sink::set_color( level::level_enum level, const shor } template -inline sink_ptr& wincolor_sink::wrapped_sink() +inline wincolor_sink::~wincolor_sink() { - return sink_; + flush(); } template -inline wincolor_sink::~wincolor_sink() +void wincolor_sink::SetConsoleColor( WORD* Attributes, DWORD Color ) { - flush(); + CONSOLE_SCREEN_BUFFER_INFO Info; + HANDLE hStdout = GetStdHandle( STD_OUTPUT_HANDLE ); + GetConsoleScreenBufferInfo( hStdout, &Info ); + *Attributes = Info.wAttributes; + SetConsoleTextAttribute( hStdout, Color ); + SetConsoleTextAttribute( GetStdHandle(STD_ERROR_HANDLE), Color ); } +template +void wincolor_sink::ResetConsoleColor( WORD Attributes ) +{ + SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), Attributes ); + SetConsoleTextAttribute( GetStdHandle( STD_ERROR_HANDLE ), Attributes ); +} + + } // namespace sinks } // namespace spdlog