2016-10-12 08:10:10 -04:00
|
|
|
//
|
|
|
|
// Copyright(c) 2016 spdlog
|
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-06-23 18:32:39 -04:00
|
|
|
#include "../fmt/fmt.h"
|
2018-04-28 18:31:09 -04:00
|
|
|
#include "spdlog/common.h"
|
|
|
|
#include "spdlog/details/null_mutex.h"
|
|
|
|
#include "spdlog/details/traits.h"
|
|
|
|
#include "spdlog/sinks/sink.h"
|
2016-10-12 08:10:10 -04:00
|
|
|
|
2018-04-15 18:07:22 -04:00
|
|
|
#include <memory>
|
2018-04-20 06:20:19 -04:00
|
|
|
#include <mutex>
|
2016-10-12 08:10:10 -04:00
|
|
|
#include <string>
|
2018-02-05 05:20:57 -05:00
|
|
|
#include <unordered_map>
|
2016-10-12 08:10:10 -04:00
|
|
|
#include <wincon.h>
|
|
|
|
|
2018-03-17 06:47:46 -04:00
|
|
|
namespace spdlog {
|
2018-04-20 06:20:19 -04:00
|
|
|
namespace sinks {
|
|
|
|
/*
|
|
|
|
* Windows color console sink. Uses WriteConsoleA to write to the console with colors
|
|
|
|
*/
|
|
|
|
template<class HandleTrait, class ConsoleMutexTrait>
|
|
|
|
class wincolor_sink : public sink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const WORD BOLD = FOREGROUND_INTENSITY;
|
|
|
|
const WORD RED = FOREGROUND_RED;
|
|
|
|
const WORD GREEN = FOREGROUND_GREEN;
|
|
|
|
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
|
|
|
|
|
|
|
|
wincolor_sink()
|
|
|
|
: out_handle_(HandleTrait::handle())
|
2018-06-10 15:59:17 -04:00
|
|
|
, mutex_(ConsoleMutexTrait::console_mutex())
|
2018-04-20 06:20:19 -04:00
|
|
|
{
|
|
|
|
colors_[level::trace] = WHITE;
|
|
|
|
colors_[level::debug] = CYAN;
|
|
|
|
colors_[level::info] = GREEN;
|
|
|
|
colors_[level::warn] = YELLOW | BOLD;
|
|
|
|
colors_[level::err] = RED | BOLD; // red bold
|
|
|
|
colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
|
|
|
|
colors_[level::off] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~wincolor_sink() override
|
|
|
|
{
|
|
|
|
this->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
wincolor_sink(const wincolor_sink &other) = delete;
|
|
|
|
wincolor_sink &operator=(const wincolor_sink &other) = delete;
|
|
|
|
|
|
|
|
// change the color for the given level
|
|
|
|
void set_color(level::level_enum level, WORD color)
|
|
|
|
{
|
2018-06-10 15:59:17 -04:00
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
2018-04-20 06:20:19 -04:00
|
|
|
colors_[level] = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void log(const details::log_msg &msg) SPDLOG_FINAL override
|
|
|
|
{
|
2018-06-10 15:59:17 -04:00
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
2018-06-23 18:32:39 -04:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
formatter_->format(msg, formatted);
|
2018-04-20 06:20:19 -04:00
|
|
|
if (msg.color_range_end > msg.color_range_start)
|
|
|
|
{
|
|
|
|
// before color range
|
2018-06-23 18:32:39 -04:00
|
|
|
print_range_(formatted, 0, msg.color_range_start);
|
2018-04-20 06:20:19 -04:00
|
|
|
|
|
|
|
// in color range
|
|
|
|
auto orig_attribs = set_console_attribs(colors_[msg.level]);
|
2018-06-23 18:32:39 -04:00
|
|
|
print_range_(formatted, msg.color_range_start, msg.color_range_end);
|
2018-04-20 06:20:19 -04:00
|
|
|
::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
|
|
|
|
// after color range
|
2018-06-23 18:32:39 -04:00
|
|
|
print_range_(formatted, msg.color_range_end, formatted.size());
|
2018-04-20 06:20:19 -04:00
|
|
|
}
|
|
|
|
else // print without colors if color range is invalid
|
|
|
|
{
|
2018-06-23 18:32:39 -04:00
|
|
|
print_range_(formatted, 0, formatted.size());
|
2018-04-20 06:20:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush() SPDLOG_FINAL override
|
|
|
|
{
|
|
|
|
// windows console always flushed?
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using mutex_t = typename ConsoleMutexTrait::mutex_t;
|
|
|
|
// 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);
|
|
|
|
return orig_buffer_info.wAttributes; // return orig attribs
|
|
|
|
}
|
|
|
|
|
|
|
|
// print a range of formatted message to console
|
2018-06-25 09:32:22 -04:00
|
|
|
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
|
2018-04-20 06:20:19 -04:00
|
|
|
{
|
|
|
|
auto size = static_cast<DWORD>(end - start);
|
2018-06-23 18:32:39 -04:00
|
|
|
::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
|
2018-04-20 06:20:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE out_handle_;
|
2018-06-10 15:59:17 -04:00
|
|
|
mutex_t &mutex_;
|
2018-04-20 06:20:19 -04:00
|
|
|
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
|
|
|
|
};
|
|
|
|
|
|
|
|
using wincolor_stdout_sink_mt = wincolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
|
|
|
|
using wincolor_stdout_sink_st = wincolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;
|
|
|
|
|
|
|
|
using wincolor_stderr_sink_mt = wincolor_sink<details::console_stderr_trait, details::console_mutex_trait>;
|
|
|
|
using wincolor_stderr_sink_st = wincolor_sink<details::console_stderr_trait, details::console_null_mutex_trait>;
|
|
|
|
|
|
|
|
} // namespace sinks
|
2018-03-17 06:47:46 -04:00
|
|
|
} // namespace spdlog
|