spdlog/src/sinks/wincolor_sink.cpp

183 lines
6.6 KiB
C++
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2023-09-25 01:26:32 +08:00
#ifdef _WIN32
2019-05-12 20:32:54 +08:00
2023-09-25 01:43:14 +08:00
# pragma once
2019-05-12 20:32:54 +08:00
2023-09-25 01:43:14 +08:00
# include <spdlog/sinks/wincolor_sink.h>
2019-05-13 05:09:00 +08:00
2023-09-25 01:43:14 +08:00
# include <spdlog/details/windows_include.h>
# include <wincon.h>
2023-09-25 01:43:14 +08:00
# include <spdlog/common.h>
# include <spdlog/pattern_formatter.h>
2019-05-13 05:09:00 +08:00
2019-05-12 20:32:54 +08:00
namespace spdlog {
2019-07-14 23:35:59 +08:00
namespace sinks {
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color_mode mode)
: out_handle_(out_handle)
2019-07-14 23:35:59 +08:00
, mutex_(ConsoleMutex::mutex())
, formatter_(std::make_unique<spdlog::pattern_formatter>())
2019-07-14 23:35:59 +08:00
{
set_color_mode_impl(mode);
// set level colors
2023-09-23 23:21:27 +08:00
colors_.at(level_to_number(level::trace)) = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
2023-09-25 01:43:14 +08:00
colors_.at(level_to_number(level::debug)) = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
2023-09-23 23:21:27 +08:00
colors_.at(level_to_number(level::info)) = FOREGROUND_GREEN; // green
2023-09-23 23:45:36 +08:00
colors_.at(level_to_number(level::warn)) = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow
2023-09-23 23:21:27 +08:00
colors_.at(level_to_number(level::err)) = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red
colors_.at(level_to_number(level::critical)) =
BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; // intense white on red background
2023-09-23 23:21:27 +08:00
colors_.at(level_to_number(level::off)) = 0;
2019-07-14 23:35:59 +08:00
}
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
wincolor_sink<ConsoleMutex>::~wincolor_sink()
2019-07-14 23:35:59 +08:00
{
this->flush();
}
// change the color for the given level
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::set_color(level level, std::uint16_t color)
2019-07-14 23:35:59 +08:00
{
std::lock_guard<mutex_t> lock(mutex_);
colors_[level_to_number(level)] = color;
2019-07-14 23:35:59 +08:00
}
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
2019-07-14 23:35:59 +08:00
{
if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE)
{
2021-02-13 21:00:35 +08:00
return;
}
2019-07-14 23:35:59 +08:00
std::lock_guard<mutex_t> lock(mutex_);
2020-02-24 23:01:09 +08:00
msg.color_range_start = 0;
msg.color_range_end = 0;
memory_buf_t formatted;
formatter_->format(msg, formatted);
2019-07-14 23:35:59 +08:00
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
{
// before color range
print_range_(formatted, 0, msg.color_range_start);
// in color range
auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[static_cast<size_t>(msg.log_level)]));
2019-07-14 23:35:59 +08:00
print_range_(formatted, msg.color_range_start, msg.color_range_end);
2019-07-18 19:26:36 +08:00
// reset to orig colors
::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);
2019-07-14 23:35:59 +08:00
print_range_(formatted, msg.color_range_end, formatted.size());
}
else // print without colors if color range is invalid (or color is disabled)
{
write_to_file_(formatted);
2019-07-14 23:35:59 +08:00
}
}
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::flush()
2019-07-14 23:35:59 +08:00
{
// windows console always flushed?
}
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern)
2019-07-14 23:35:59 +08:00
{
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
2019-07-14 23:35:59 +08:00
{
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
2019-07-14 23:35:59 +08:00
{
std::lock_guard<mutex_t> lock(mutex_);
set_color_mode_impl(mode);
}
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode mode)
{
if (mode == color_mode::automatic)
{
// should do colors only if out_handle_ points to actual console.
DWORD console_mode;
bool in_console = ::GetConsoleMode(static_cast<HANDLE>(out_handle_), &console_mode) != 0;
should_do_colors_ = in_console;
}
else
{
should_do_colors_ = mode == color_mode::always ? true : false;
}
2019-07-14 23:35:59 +08:00
}
2019-07-15 17:22:34 +08:00
// set foreground color and return the orig console attributes (for resetting later)
2019-07-14 23:35:59 +08:00
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
std::uint16_t wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs)
{
2019-07-14 23:42:51 +08:00
CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info))
{
2021-02-14 07:49:29 +08:00
// just return white if failed getting console info
2021-07-19 05:50:51 +08:00
return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
}
2021-07-19 05:50:51 +08:00
// change only the foreground bits (lowest 4 bits)
auto new_attribs = static_cast<WORD>(attribs) | (orig_buffer_info.wAttributes & 0xfff0);
2021-03-17 05:39:55 +08:00
auto ignored = ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(new_attribs));
(void)(ignored);
return static_cast<std::uint16_t>(orig_buffer_info.wAttributes); // return orig attribs
2019-07-14 23:35:59 +08:00
}
// print a range of formatted message to console
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
2019-07-14 23:35:59 +08:00
{
if (end > start)
{
auto size = static_cast<DWORD>(end - start);
auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
(void)(ignored);
}
2019-07-14 23:35:59 +08:00
}
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
void wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted)
{
2019-07-14 23:35:59 +08:00
auto size = static_cast<DWORD>(formatted.size());
DWORD bytes_written = 0;
auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, &bytes_written, nullptr);
(void)(ignored);
2019-07-14 23:35:59 +08:00
}
// wincolor_stdout_sink
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode)
2019-07-14 23:35:59 +08:00
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
{}
// wincolor_stderr_sink
template<typename ConsoleMutex>
2023-09-25 01:43:14 +08:00
wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode)
2019-07-14 23:35:59 +08:00
: wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
{}
} // namespace sinks
2019-07-18 06:37:55 +08:00
} // namespace spdlog
2023-09-25 01:26:32 +08:00
2023-09-25 01:29:33 +08:00
// template instantiations
template SPDLOG_API class spdlog::sinks::wincolor_stdout_sink<spdlog::details::console_mutex>;
template SPDLOG_API class spdlog::sinks::wincolor_stdout_sink<spdlog::details::console_nullmutex>;
template SPDLOG_API class spdlog::sinks::wincolor_stderr_sink<spdlog::details::console_mutex>;
template SPDLOG_API class spdlog::sinks::wincolor_stderr_sink<spdlog::details::console_nullmutex>;
2023-09-25 01:26:32 +08:00
#endif // _WIN32