Changed wincolor to be thread safe.

pull/194/head^2
Christopher Torres 10 years ago
parent 3fac9ba212
commit eafc1cc98b
No known key found for this signature in database
GPG Key ID: D26CE56FC38D0C83

@ -14,7 +14,9 @@
#include <spdlog/sinks/stdout_sinks.h>
#include <spdlog/sinks/syslog_sink.h>
#include <spdlog/sinks/ansicolor_sink.h>
#ifdef _WIN32
#include <spdlog/sinks/wincolor_sink.h>
#endif
#include <chrono>
#include <functional>
@ -59,11 +61,22 @@ 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
#ifdef _WIN32
sink = std::make_shared<spdlog::sinks::wincolor_sink_st>(sink);
#else
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink);
#endif
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
#ifdef _WIN32
sink = std::make_shared<spdlog::sinks::wincolor_sink>(sink);
sink = std::make_shared<spdlog::sinks::wincolor_sink_mt>(sink);
#else
sink = std::make_shared<spdlog::sinks::ansicolor_sink>(sink);
#endif
@ -72,22 +85,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__)

@ -16,23 +16,13 @@ namespace spdlog
namespace sinks
{
#ifndef _WIN32
#define FOREGROUND_INTENSITY 0
#define FOREGROUND_RED 0
#define FOREGROUND_GREEN 0
#define FOREGROUND_BLUE 0
#define BACKGROUND_INTENSITY 0
#define BACKGROUND_RED 0
#define BACKGROUND_GREEN 0
#define BACKGROUND_BLUE 0
#endif
/**
* @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.
*/
class wincolor_sink : public sink
template<class Mutex>
class wincolor_sink : public base_sink<Mutex>
{
public:
wincolor_sink(sink_ptr wrapped_sink);
@ -41,7 +31,6 @@ public:
wincolor_sink(const wincolor_sink& other) = delete;
wincolor_sink& operator=(const wincolor_sink& other) = delete;
virtual void log(const details::log_msg& msg) override;
virtual void flush() override;
// Formatting codes
@ -79,10 +68,16 @@ public:
sink_ptr& wrapped_sink();
protected:
virtual void _sink_it(const details::log_msg& msg) override;
sink_ptr sink_;
std::map<level::level_enum, short> colors_;
};
typedef wincolor_sink<details::null_mutex> wincolor_sink_st;
typedef wincolor_sink<std::mutex> wincolor_sink_mt;
inline wincolor_sink::wincolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink)
{
colors_[level::trace] = cyan;
@ -98,20 +93,14 @@ inline wincolor_sink::wincolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink)
colors_[level::emerg] = bold | yellow | on_red;
}
inline void wincolor_sink::log(const details::log_msg& msg)
inline void wincolor_sink::_sink_it(const details::log_msg& msg)
{
// Wrap the originally formatted message in color codes
#ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), colors_[msg.level]);
SetConsoleTextAttribute(GetStdHandle( STD_ERROR_HANDLE ), colors_[msg.level]);
#endif
sink_->log( msg );
#ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle( STD_ERROR_HANDLE ), reset);
SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), reset);
#endif
}
inline void wincolor_sink::flush()

Loading…
Cancel
Save