spdlog
Loading...
Searching...
No Matches
wincolor_sink-inl.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#ifndef SPDLOG_HEADER_ONLY
8#endif
9
11#include <wincon.h>
12
13#include <spdlog/common.h>
15
16namespace spdlog {
17namespace sinks {
18template<typename ConsoleMutex>
20 : out_handle_(out_handle)
21 , mutex_(ConsoleMutex::mutex())
22 , formatter_(details::make_unique<spdlog::pattern_formatter>())
23{
24
26 // set level colors
27 colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
28 colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
29 colors_[level::info] = FOREGROUND_GREEN; // green
30 colors_[level::warn] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow
31 colors_[level::err] = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red
33 BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; // intense white on red background
35}
36
37template<typename ConsoleMutex>
42
43// change the color for the given level
44template<typename ConsoleMutex>
50
51template<typename ConsoleMutex>
53{
54 if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE)
55 {
56 return;
57 }
58
59 std::lock_guard<mutex_t> lock(mutex_);
60 msg.color_range_start = 0;
61 msg.color_range_end = 0;
62 memory_buf_t formatted;
63 formatter_->format(msg, formatted);
64 if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
65 {
66 // before color range
67 print_range_(formatted, 0, msg.color_range_start);
68 // in color range
69 auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[msg.level]));
70 print_range_(formatted, msg.color_range_start, msg.color_range_end);
71 // reset to orig colors
72 ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), orig_attribs);
73 print_range_(formatted, msg.color_range_end, formatted.size());
74 }
75 else // print without colors if color range is invalid (or color is disabled)
76 {
77 write_to_file_(formatted);
78 }
79}
80
81template<typename ConsoleMutex>
83{
84 // windows console always flushed?
85}
86
87template<typename ConsoleMutex>
93
94template<typename ConsoleMutex>
96{
97 std::lock_guard<mutex_t> lock(mutex_);
98 formatter_ = std::move(sink_formatter);
99}
100
101template<typename ConsoleMutex>
103{
104 std::lock_guard<mutex_t> lock(mutex_);
105 set_color_mode_impl(mode);
106}
107
108template<typename ConsoleMutex>
110{
111 if (mode == color_mode::automatic)
112 {
113 // should do colors only if out_handle_ points to actual console.
114 DWORD console_mode;
115 bool in_console = ::GetConsoleMode(static_cast<HANDLE>(out_handle_), &console_mode) != 0;
116 should_do_colors_ = in_console;
117 }
118 else
119 {
120 should_do_colors_ = mode == color_mode::always ? true : false;
121 }
122}
123
124// set foreground color and return the orig console attributes (for resetting later)
125template<typename ConsoleMutex>
127{
128 CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
129 if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(out_handle_), &orig_buffer_info))
130 {
131 // just return white if failed getting console info
132 return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
133 }
134
135 // change only the foreground bits (lowest 4 bits)
136 auto new_attribs = static_cast<WORD>(attribs) | (orig_buffer_info.wAttributes & 0xfff0);
137 auto ignored = ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(new_attribs));
138 (void)(ignored);
139 return static_cast<std::uint16_t>(orig_buffer_info.wAttributes); // return orig attribs
140}
141
142// print a range of formatted message to console
143template<typename ConsoleMutex>
144void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end)
145{
146 if (end > start)
147 {
148 auto size = static_cast<DWORD>(end - start);
149 auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr);
150 (void)(ignored);
151 }
152}
153
154template<typename ConsoleMutex>
156{
157 auto size = static_cast<DWORD>(formatted.size());
158 DWORD bytes_written = 0;
159 auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, &bytes_written, nullptr);
160 (void)(ignored);
161}
162
163// wincolor_stdout_sink
164template<typename ConsoleMutex>
166 : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
167{}
168
169// wincolor_stderr_sink
170template<typename ConsoleMutex>
172 : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
173{}
174} // namespace sinks
175} // namespace spdlog
void flush() final override
std::array< std::uint16_t, level::n_levels > colors_
void log(const details::log_msg &msg) final override
void write_to_file_(const memory_buf_t &formatted)
void set_color_mode_impl(color_mode mode)
void print_range_(const memory_buf_t &formatted, size_t start, size_t end)
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override final
std::uint16_t set_foreground_color_(std::uint16_t attribs)
void set_pattern(const std::string &pattern) override final
void set_color_mode(color_mode mode)
wincolor_sink(void *out_handle, color_mode mode)
void set_color(level::level_enum level, std::uint16_t color)
wincolor_stderr_sink(color_mode mode=color_mode::automatic)
wincolor_stdout_sink(color_mode mode=color_mode::automatic)
color
Definition color.h:23
#define SPDLOG_INLINE
Definition common.h:33
Definition async.h:25
color_mode
Definition common.h:207
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116
level::level_enum level
Definition log_msg.h:21