wincolor_sink is not a wrapper class anymore. Removed my addition to the spdlog create_console_logger function

pull/194/head
Christopher Torres 9 years ago
parent b8872b7b8e
commit 8e3572aa18
No known key found for this signature in database
GPG Key ID: D26CE56FC38D0C83

@ -14,9 +14,6 @@
#include <spdlog/sinks/stdout_sinks.h> #include <spdlog/sinks/stdout_sinks.h>
#include <spdlog/sinks/syslog_sink.h> #include <spdlog/sinks/syslog_sink.h>
#include <spdlog/sinks/ansicolor_sink.h> #include <spdlog/sinks/ansicolor_sink.h>
#ifdef _WIN32
#include <spdlog/sinks/wincolor_sink.h>
#endif
#include <chrono> #include <chrono>
#include <functional> #include <functional>
@ -64,22 +61,14 @@ inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string
inline std::shared_ptr<spdlog::logger> create_console_logger_st(const std::string& logger_name, spdlog::sink_ptr sink, bool color) inline std::shared_ptr<spdlog::logger> create_console_logger_st(const std::string& logger_name, spdlog::sink_ptr sink, bool color)
{ {
if (color) //use color wrapper sink if (color) //use color wrapper sink
#ifdef _WIN32
sink = std::make_shared<spdlog::sinks::wincolor_sink_st>(sink);
#else
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink); sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink);
#endif
return spdlog::details::registry::instance().create(logger_name, sink); return spdlog::details::registry::instance().create(logger_name, sink);
} }
inline std::shared_ptr<spdlog::logger> create_console_logger_mt(const std::string& logger_name, spdlog::sink_ptr sink, bool color) inline std::shared_ptr<spdlog::logger> create_console_logger_mt(const std::string& logger_name, spdlog::sink_ptr sink, bool color)
{ {
if (color) //use color wrapper sink if (color) //use color wrapper sink
#ifdef _WIN32
sink = std::make_shared<spdlog::sinks::wincolor_sink_mt>(sink);
#else
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink); sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink);
#endif
return spdlog::details::registry::instance().create(logger_name, sink); return spdlog::details::registry::instance().create(logger_name, sink);
} }

@ -25,7 +25,7 @@ template<class Mutex>
class wincolor_sink : public base_sink<Mutex> class wincolor_sink : public base_sink<Mutex>
{ {
public: public:
wincolor_sink(sink_ptr wrapped_sink); wincolor_sink(std::ostream& os, bool force_flush=false);
virtual ~wincolor_sink(); virtual ~wincolor_sink();
wincolor_sink(const wincolor_sink& other) = delete; wincolor_sink(const wincolor_sink& other) = delete;
@ -34,7 +34,7 @@ public:
virtual void flush() override; virtual void flush() override;
// Formatting codes // Formatting codes
const short reset = 0; const short reset = FOREGROUND_INTENSITY;
const short bold = FOREGROUND_INTENSITY; const short bold = FOREGROUND_INTENSITY;
const short dark = reset; // Not implemented in windows const short dark = reset; // Not implemented in windows
const short underline = 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 const short concealed = reset; // Not implemented in windows
// Foreground colors // Foreground colors
const short black = 0;
const short grey = bold; const short grey = bold;
const short red = FOREGROUND_RED; const short red = FOREGROUND_RED;
const short green = FOREGROUND_GREEN; const short green = FOREGROUND_GREEN;
@ -65,20 +66,23 @@ public:
const short on_white = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; const short on_white = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
void set_color( level::level_enum level, const short& color ); void set_color( level::level_enum level, const short& color );
sink_ptr& wrapped_sink();
protected: protected:
virtual void _sink_it(const details::log_msg& msg) override; 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<level::level_enum, short> colors_; std::map<level::level_enum, short> colors_;
std::ostream& _ostream;
bool _force_flush;
}; };
typedef wincolor_sink<details::null_mutex> wincolor_sink_st; typedef wincolor_sink<details::null_mutex> wincolor_sink_st;
typedef wincolor_sink<std::mutex> wincolor_sink_mt; typedef wincolor_sink<std::mutex> wincolor_sink_mt;
template<class Mutex> template<class Mutex>
inline wincolor_sink<Mutex>::wincolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink) inline wincolor_sink<Mutex>::wincolor_sink(std::ostream& os, bool force_flush=false) :_ostream(os), _force_flush(force_flush)
{ {
colors_[level::trace] = cyan; colors_[level::trace] = cyan;
colors_[level::debug] = cyan; colors_[level::debug] = cyan;
@ -97,17 +101,18 @@ template<class Mutex>
inline void wincolor_sink<Mutex>::_sink_it( const details::log_msg& msg ) inline void wincolor_sink<Mutex>::_sink_it( const details::log_msg& msg )
{ {
// Wrap the originally formatted message in color codes // Wrap the originally formatted message in color codes
SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), colors_[msg.level]); WORD Attributes = 0;
SetConsoleTextAttribute(GetStdHandle( STD_ERROR_HANDLE ), colors_[msg.level]); SetConsoleColor(&Attributes, colors_[msg.level]);
sink_->log( msg ); _ostream.write( msg.formatted.data(), msg.formatted.size() );
SetConsoleTextAttribute(GetStdHandle( STD_ERROR_HANDLE ), reset); if (_force_flush)
SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), reset); _ostream.flush();
ResetConsoleColor(Attributes);
} }
template<class Mutex> template<class Mutex>
inline void wincolor_sink<Mutex>::flush() inline void wincolor_sink<Mutex>::flush()
{ {
sink_->flush(); _ostream.flush();
} }
template<class Mutex> template<class Mutex>
@ -117,17 +122,30 @@ inline void wincolor_sink<Mutex>::set_color( level::level_enum level, const shor
} }
template<class Mutex> template<class Mutex>
inline sink_ptr& wincolor_sink<Mutex>::wrapped_sink() inline wincolor_sink<Mutex>::~wincolor_sink()
{ {
return sink_; flush();
} }
template<class Mutex> template<class Mutex>
inline wincolor_sink<Mutex>::~wincolor_sink() void wincolor_sink<Mutex>::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<class Mutex>
void wincolor_sink<Mutex>::ResetConsoleColor( WORD Attributes )
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), Attributes );
SetConsoleTextAttribute( GetStdHandle( STD_ERROR_HANDLE ), Attributes );
}
} // namespace sinks } // namespace sinks
} // namespace spdlog } // namespace spdlog

Loading…
Cancel
Save