spdlog/include/spdlog/sinks/wincolor_sink.h

123 lines
3.6 KiB
C
Raw Normal View History

//
// Copyright(c) 2016 spdlog
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
2017-11-11 09:38:08 -05:00
#include "../common.h"
2018-03-09 08:26:33 -05:00
#include "../details/null_mutex.h"
#include "base_sink.h"
#include <mutex>
#include <string>
2018-02-05 05:20:57 -05:00
#include <unordered_map>
#include <wincon.h>
2018-03-09 08:26:33 -05:00
namespace spdlog { namespace sinks {
2016-10-12 16:08:44 -04:00
/*
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
*/
2018-03-16 11:35:56 -04:00
template<class Mutex>
class wincolor_sink : public base_sink<Mutex>
2016-10-12 16:08:44 -04:00
{
public:
const WORD BOLD = FOREGROUND_INTENSITY;
const WORD RED = FOREGROUND_RED;
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
2018-03-09 08:26:33 -05:00
wincolor_sink(HANDLE std_handle)
: out_handle_(std_handle)
2016-10-12 16:08:44 -04:00
{
colors_[level::trace] = CYAN;
colors_[level::debug] = CYAN;
colors_[level::info] = WHITE | BOLD;
colors_[level::warn] = YELLOW | BOLD;
2018-03-09 08:26:33 -05:00
colors_[level::err] = RED | BOLD; // red bold
2016-10-12 16:08:44 -04:00
colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
colors_[level::off] = 0;
}
2018-02-24 17:56:56 -05:00
~wincolor_sink() override
2016-10-12 16:08:44 -04:00
{
this->flush();
2016-10-12 16:08:44 -04:00
}
2018-03-09 08:26:33 -05:00
wincolor_sink(const wincolor_sink &other) = delete;
wincolor_sink &operator=(const wincolor_sink &other) = delete;
2016-10-12 16:08:44 -04:00
2018-02-24 20:43:26 -05:00
// change the color for the given level
void set_color(level::level_enum level, WORD color)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
colors_[level] = color;
}
2017-05-20 20:43:41 -04:00
protected:
2018-03-09 08:26:33 -05:00
void _sink_it(const details::log_msg &msg) override
2016-10-12 16:08:44 -04:00
{
auto color = colors_[msg.level];
auto orig_attribs = set_console_attribs(color);
2016-10-17 05:50:38 -04:00
WriteConsoleA(out_handle_, msg.formatted.data(), static_cast<DWORD>(msg.formatted.size()), nullptr, nullptr);
2018-03-09 08:26:33 -05:00
SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
2016-10-12 16:08:44 -04:00
}
2018-02-24 17:56:56 -05:00
void _flush() override
2016-10-12 16:08:44 -04:00
{
// windows console always flushed?
}
private:
HANDLE out_handle_;
2018-02-05 05:20:57 -05:00
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
2016-10-12 16:08:44 -04:00
// set color and return the orig console attributes (for resetting later)
WORD set_console_attribs(WORD attribs)
{
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
WORD back_color = orig_buffer_info.wAttributes;
// retrieve the current background color
back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
// keep the background color unchanged
SetConsoleTextAttribute(out_handle_, attribs | back_color);
2018-03-09 08:26:33 -05:00
return orig_buffer_info.wAttributes; // return orig attribs
2016-10-12 16:08:44 -04:00
}
};
//
// windows color console to stdout
//
2018-03-16 11:35:56 -04:00
template<class Mutex>
class wincolor_stdout_sink : public wincolor_sink<Mutex>
2016-10-12 16:08:44 -04:00
{
public:
2018-03-09 08:26:33 -05:00
wincolor_stdout_sink()
: wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE))
{
}
2016-10-12 16:08:44 -04:00
};
2018-02-24 16:35:09 -05:00
using wincolor_stdout_sink_mt = wincolor_stdout_sink<std::mutex>;
using wincolor_stdout_sink_st = wincolor_stdout_sink<details::null_mutex>;
2016-10-12 16:08:44 -04:00
//
// windows color console to stderr
//
2018-03-16 11:35:56 -04:00
template<class Mutex>
class wincolor_stderr_sink : public wincolor_sink<Mutex>
2016-10-12 16:08:44 -04:00
{
public:
2018-03-09 08:26:33 -05:00
wincolor_stderr_sink()
: wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE))
{
}
2016-10-12 16:08:44 -04:00
};
2018-02-24 16:35:09 -05:00
using wincolor_stderr_sink_mt = wincolor_stderr_sink<std::mutex>;
using wincolor_stderr_sink_st = wincolor_stderr_sink<details::null_mutex>;
2016-10-12 16:08:44 -04:00
2018-03-09 08:26:33 -05:00
}} // namespace spdlog::sinks