pull/194/merge
Christopher Torres 9 years ago
commit 106e5f6343

@ -58,7 +58,14 @@ inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string
}
// Create stdout/stderr loggers (with optinal color support)
inline std::shared_ptr<spdlog::logger> create_console_logger(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
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(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)
{
if (color) //use color wrapper sink
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink);
@ -67,22 +74,22 @@ inline std::shared_ptr<spdlog::logger> create_console_logger(const std::string&
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt(const std::string& logger_name, bool color)
{
return create_console_logger(logger_name, sinks::stdout_sink_mt::instance(), color);
return create_console_logger_mt(logger_name, sinks::stdout_sink_mt::instance(), color);
}
inline std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st(const std::string& logger_name, bool color)
{
return create_console_logger(logger_name, sinks::stdout_sink_st::instance(), color);
return create_console_logger_st(logger_name, sinks::stdout_sink_st::instance(), color);
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt(const std::string& logger_name, bool color)
{
return create_console_logger(logger_name, sinks::stderr_sink_mt::instance(), color);
return create_console_logger_mt(logger_name, sinks::stderr_sink_mt::instance(), color);
}
inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::string& logger_name, bool color)
{
return create_console_logger(logger_name, sinks::stderr_sink_st::instance(), color);
return create_console_logger_st(logger_name, sinks::stderr_sink_st::instance(), color);
}
#if defined(__linux__) || defined(__APPLE__)

@ -64,7 +64,6 @@ public:
const std::string on_cyan = "\033[46m";
const std::string on_white = "\033[47m";
protected:
sink_ptr sink_;
std::map<level::level_enum, std::string> colors_;

@ -0,0 +1,151 @@
//
// Copyright(c) 2016 Christopher J. Torres (a modified verison of ansicolor_sink).
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include <spdlog/sinks/base_sink.h>
#include <spdlog/common.h>
#include <string>
#include <map>
namespace spdlog
{
namespace sinks
{
/**
* @brief The wincolor_sink is a decorator around another sink and uses
* the windows api to set the color depending on the severity
* of the message.
*/
template<class Mutex>
class wincolor_sink : public base_sink<Mutex>
{
public:
wincolor_sink(std::ostream& os, bool force_flush=false);
virtual ~wincolor_sink();
wincolor_sink(const wincolor_sink& other) = delete;
wincolor_sink& operator=(const wincolor_sink& other) = delete;
virtual void flush() override;
// Formatting codes
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
const short blink = reset; // Not implemented in windows
const short reverse = FOREGROUND_RED | FOREGROUND_GREEN |
FOREGROUND_BLUE | BACKGROUND_RED |
BACKGROUND_GREEN | BACKGROUND_BLUE; // XOR to use this
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;
const short yellow = FOREGROUND_RED | FOREGROUND_GREEN;
const short blue = FOREGROUND_BLUE;
const short magenta = FOREGROUND_RED | FOREGROUND_BLUE;
const short cyan = FOREGROUND_GREEN | FOREGROUND_BLUE;
const short white = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
/// Background colors
const short on_grey = BACKGROUND_INTENSITY;
const short on_red = BACKGROUND_RED;
const short on_green = BACKGROUND_GREEN;
const short on_yellow = BACKGROUND_RED | BACKGROUND_GREEN;
const short on_blue = BACKGROUND_BLUE;
const short on_magenta = BACKGROUND_RED | BACKGROUND_BLUE;
const short on_cyan = BACKGROUND_GREEN | BACKGROUND_BLUE;
const short on_white = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
void set_color( level::level_enum level, const short& color );
protected:
virtual void _sink_it(const details::log_msg& msg) override;
void SetConsoleColor( WORD* Attributes, DWORD Color );
void ResetConsoleColor( WORD Attributes );
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<std::mutex> wincolor_sink_mt;
template<class Mutex>
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::debug] = cyan;
colors_[level::info] = white;
colors_[level::err] = red;
colors_[level::off] = reset;
colors_[level::notice] = bold | white;
colors_[level::warn] = bold | yellow;
colors_[level::critical] = bold | red;
colors_[level::alert] = bold | white | on_red;
colors_[level::emerg] = bold | yellow | on_red;
}
template<class Mutex>
inline void wincolor_sink<Mutex>::_sink_it( const details::log_msg& msg )
{
// Wrap the originally formatted message in color codes
WORD Attributes = 0;
SetConsoleColor(&Attributes, colors_[msg.level]);
_ostream.write( msg.formatted.data(), msg.formatted.size() );
if (_force_flush)
_ostream.flush();
ResetConsoleColor(Attributes);
}
template<class Mutex>
inline void wincolor_sink<Mutex>::flush()
{
_ostream.flush();
}
template<class Mutex>
inline void wincolor_sink<Mutex>::set_color( level::level_enum level, const short& color )
{
colors_[level] = color;
}
template<class Mutex>
inline wincolor_sink<Mutex>::~wincolor_sink()
{
flush();
}
template<class Mutex>
void wincolor_sink<Mutex>::SetConsoleColor( WORD* Attributes, DWORD Color )
{
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 spdlog
Loading…
Cancel
Save