Files
spdlog/src/sinks/wincolor_sink.cpp

147 lines
6.0 KiB
C++
Raw Normal View History

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