Agora  1.2.0
Agora project
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 
16 namespace spdlog {
17 namespace sinks {
18 template<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
34  colors_[level::off] = 0;
35 }
36 
37 template<typename ConsoleMutex>
39 {
40  this->flush();
41 }
42 
43 // change the color for the given level
44 template<typename ConsoleMutex>
46 {
47  std::lock_guard<mutex_t> lock(mutex_);
48  colors_[level] = color;
49 }
50 
51 template<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 
81 template<typename ConsoleMutex>
83 {
84  // windows console always flushed?
85 }
86 
87 template<typename ConsoleMutex>
89 {
90  std::lock_guard<mutex_t> lock(mutex_);
91  formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
92 }
93 
94 template<typename ConsoleMutex>
95 void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
96 {
97  std::lock_guard<mutex_t> lock(mutex_);
98  formatter_ = std::move(sink_formatter);
99 }
100 
101 template<typename ConsoleMutex>
103 {
104  std::lock_guard<mutex_t> lock(mutex_);
105  set_color_mode_impl(mode);
106 }
107 
108 template<typename ConsoleMutex>
110 {
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)
125 template<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
143 template<typename ConsoleMutex>
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 
154 template<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
164 template<typename ConsoleMutex>
166  : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode)
167 {}
168 
169 // wincolor_stderr_sink
170 template<typename ConsoleMutex>
172  : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode)
173 {}
174 } // namespace sinks
175 } // namespace spdlog
fmt::v8::color
color
Definition: color.h:23
spdlog::sinks::wincolor_stderr_sink::wincolor_stderr_sink
wincolor_stderr_sink(color_mode mode=color_mode::automatic)
Definition: wincolor_sink-inl.h:171
windows_include.h
size
end IFFT Reshape the symbol vector into two different spatial streams size
Definition: generate_data.m:73
spdlog::sinks::wincolor_stdout_sink::wincolor_stdout_sink
wincolor_stdout_sink(color_mode mode=color_mode::automatic)
Definition: wincolor_sink-inl.h:165
spdlog::level::trace
@ trace
Definition: common.h:213
spdlog::level::off
@ off
Definition: common.h:219
spdlog::sinks::wincolor_sink
Definition: wincolor_sink.h:24
spdlog::sinks::wincolor_sink::set_color
void set_color(level::level_enum level, std::uint16_t color)
Definition: wincolor_sink-inl.h:45
spdlog::level::warn
@ warn
Definition: common.h:216
mm_gui.mode
string mode
Definition: mm_gui.py:105
fmt::v8::detail::buffer::size
constexpr auto size() const -> size_t
Definition: core.h:820
spdlog::level::info
@ info
Definition: common.h:215
spdlog::sinks::wincolor_sink::set_foreground_color_
std::uint16_t set_foreground_color_(std::uint16_t attribs)
Definition: wincolor_sink-inl.h:126
spdlog::level::level_enum
level_enum
Definition: common.h:211
spdlog::details::log_msg::color_range_end
size_t color_range_end
Definition: log_msg.h:27
spdlog::level::debug
@ debug
Definition: common.h:214
spdlog::pattern_formatter
Definition: pattern_formatter.h:77
spdlog::level::err
@ err
Definition: common.h:217
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog::sinks::wincolor_sink::set_color_mode
void set_color_mode(color_mode mode)
Definition: wincolor_sink-inl.h:102
spdlog::sinks::wincolor_sink::set_color_mode_impl
void set_color_mode_impl(color_mode mode)
Definition: wincolor_sink-inl.h:109
spdlog::sinks::wincolor_sink::flush
void flush() final override
Definition: wincolor_sink-inl.h:82
spdlog
Definition: async.h:25
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail::void
j template void())
Definition: json.hpp:4744
spdlog::sinks::wincolor_sink::write_to_file_
void write_to_file_(const memory_buf_t &formatted)
Definition: wincolor_sink-inl.h:155
spdlog::color_mode::always
@ always
spdlog::sinks::wincolor_sink::wincolor_sink
wincolor_sink(void *out_handle, color_mode mode)
Definition: wincolor_sink-inl.h:19
SPDLOG_INLINE
#define SPDLOG_INLINE
Definition: common.h:42
start
end start
Definition: inspect_agora_results.m:95
spdlog::details::make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: common.h:334
spdlog::details::log_msg::level
level::level_enum level
Definition: log_msg.h:21
fmt::v8::detail::buffer::data
auto data() -> T *
Definition: core.h:826
spdlog::sinks::wincolor_sink::set_pattern
void set_pattern(const std::string &pattern) override final
Definition: wincolor_sink-inl.h:88
common.h
spdlog::details::log_msg
Definition: log_msg.h:11
pattern_formatter.h
spdlog::sinks::wincolor_sink::set_formatter
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override final
Definition: wincolor_sink-inl.h:95
spdlog::sinks::wincolor_sink::colors_
std::array< std::uint16_t, level::n_levels > colors_
Definition: wincolor_sink.h:47
wincolor_sink.h
spdlog::details::log_msg::color_range_start
size_t color_range_start
Definition: log_msg.h:26
spdlog::sinks::wincolor_sink::log
void log(const details::log_msg &msg) final override
Definition: wincolor_sink-inl.h:52
spdlog::color_mode::automatic
@ automatic
spdlog::level::critical
@ critical
Definition: common.h:218
spdlog::sinks::wincolor_sink::~wincolor_sink
~wincolor_sink() override
Definition: wincolor_sink-inl.h:38
spdlog::sinks::wincolor_sink::print_range_
void print_range_(const memory_buf_t &formatted, size_t start, size_t end)
Definition: wincolor_sink-inl.h:144
spdlog::color_mode
color_mode
Definition: common.h:256
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::end
end_tag end(T &&...)