2019-06-03 17:09:16 -04:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2019-05-12 09:11:35 -04:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
2019-05-12 08:32:54 -04:00
|
|
|
|
2019-05-12 09:11:35 -04:00
|
|
|
#pragma once
|
2019-05-12 08:32:54 -04:00
|
|
|
|
2019-05-12 17:09:00 -04:00
|
|
|
#ifndef SPDLOG_HEADER_ONLY
|
|
|
|
#include "spdlog/sinks/wincolor_sink.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "spdlog/common.h"
|
|
|
|
|
2019-05-12 08:32:54 -04:00
|
|
|
namespace spdlog {
|
|
|
|
namespace sinks {
|
2019-05-12 17:09:00 -04:00
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(HANDLE out_handle, color_mode mode)
|
|
|
|
: out_handle_(out_handle)
|
2019-05-12 16:08:14 -04:00
|
|
|
, mutex_(ConsoleMutex::mutex())
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
2019-05-24 05:33:14 -04:00
|
|
|
set_color_mode(mode);
|
2019-05-12 08:32:54 -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;
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink()
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
this->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// change the color for the given level
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, WORD color)
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
colors_[level] = color;
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
formatter_->format(msg, formatted);
|
2019-05-24 05:33:14 -04:00
|
|
|
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
// before color range
|
|
|
|
print_range_(formatted, 0, msg.color_range_start);
|
|
|
|
|
|
|
|
// in color range
|
|
|
|
auto orig_attribs = set_console_attribs(colors_[msg.level]);
|
|
|
|
print_range_(formatted, msg.color_range_start, msg.color_range_end);
|
|
|
|
::SetConsoleTextAttribute(out_handle_,
|
|
|
|
orig_attribs); // reset to orig colors
|
2019-05-12 16:08:14 -04:00
|
|
|
// after color range
|
2019-05-12 08:32:54 -04:00
|
|
|
print_range_(formatted, msg.color_range_end, formatted.size());
|
|
|
|
}
|
2019-05-24 05:33:14 -04:00
|
|
|
else // print without colors if color range is invalid (or color is disabled)
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
print_range_(formatted, 0, formatted.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::flush()
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
// windows console always flushed?
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
formatter_ = std::move(sink_formatter);
|
|
|
|
}
|
2019-05-12 16:08:14 -04:00
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
|
2019-05-24 05:33:14 -04:00
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
2019-06-03 17:09:16 -04:00
|
|
|
case color_mode::always:
|
|
|
|
case color_mode::automatic:
|
|
|
|
should_do_colors_ = true;
|
|
|
|
break;
|
|
|
|
case color_mode::never:
|
|
|
|
should_do_colors_ = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
should_do_colors_ = true;
|
2019-05-24 05:33:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 08:32:54 -04:00
|
|
|
// set color and return the orig console attributes (for resetting later)
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
WORD SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_console_attribs(WORD attribs)
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
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
|
2019-06-18 05:32:51 -04:00
|
|
|
template<typename ConsoleMutex>
|
|
|
|
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
|
2019-05-12 08:32:54 -04:00
|
|
|
{
|
|
|
|
auto size = static_cast<DWORD>(end - start);
|
|
|
|
::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2019-06-18 05:32:51 -04:00
|
|
|
// wincolor_stdout_sink
|
|
|
|
template<typename ConsoleMutex>
|
|
|
|
SPDLOG_INLINE wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode)
|
|
|
|
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// wincolor_stderr_sink
|
|
|
|
template<typename ConsoleMutex>
|
|
|
|
SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode)
|
|
|
|
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
|
|
|
|
{}
|
|
|
|
|
2019-05-12 16:08:14 -04:00
|
|
|
} // namespace sinks
|
2019-05-12 08:32:54 -04:00
|
|
|
} // namespace spdlog
|