WireCellToolkit
Wire Cell Simulation, Signal Process and Reconstruction Toolki for Liquid Argon Detectors
wincolor_sink.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2016 spdlog
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 #ifndef SPDLOG_H
9 #include "spdlog/spdlog.h"
10 #endif
11 
12 #include "spdlog/common.h"
15 #include "spdlog/sinks/sink.h"
16 
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 #include <unordered_map>
21 #include <wincon.h>
22 
23 namespace spdlog {
24 namespace sinks {
25 /*
26  * Windows color console sink. Uses WriteConsoleA to write to the console with
27  * colors
28  */
29 template<typename OutHandle, typename ConsoleMutex>
30 class wincolor_sink : public sink
31 {
32 public:
33  const WORD BOLD = FOREGROUND_INTENSITY;
34  const WORD RED = FOREGROUND_RED;
35  const WORD GREEN = FOREGROUND_GREEN;
36  const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
37  const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
38  const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
39 
41  : out_handle_(OutHandle::handle())
42  , mutex_(ConsoleMutex::mutex())
43  {
44  colors_[level::trace] = WHITE;
45  colors_[level::debug] = CYAN;
46  colors_[level::info] = GREEN;
47  colors_[level::warn] = YELLOW | BOLD;
48  colors_[level::err] = RED | BOLD; // red bold
49  colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
50  colors_[level::off] = 0;
51  }
52 
53  ~wincolor_sink() override
54  {
55  this->flush();
56  }
57 
58  wincolor_sink(const wincolor_sink &other) = delete;
59  wincolor_sink &operator=(const wincolor_sink &other) = delete;
60 
61  // change the color for the given level
63  {
64  std::lock_guard<mutex_t> lock(mutex_);
65  colors_[level] = color;
66  }
67 
68  void log(const details::log_msg &msg) final override
69  {
70  std::lock_guard<mutex_t> lock(mutex_);
71  fmt::memory_buffer formatted;
72  formatter_->format(msg, formatted);
73  if (msg.color_range_end > msg.color_range_start)
74  {
75  // before color range
76  print_range_(formatted, 0, msg.color_range_start);
77 
78  // in color range
79  auto orig_attribs = set_console_attribs(colors_[msg.level]);
80  print_range_(formatted, msg.color_range_start, msg.color_range_end);
81  ::SetConsoleTextAttribute(out_handle_,
82  orig_attribs); // reset to orig colors
83  // after color range
84  print_range_(formatted, msg.color_range_end, formatted.size());
85  }
86  else // print without colors if color range is invalid
87  {
88  print_range_(formatted, 0, formatted.size());
89  }
90  }
91 
92  void flush() final override
93  {
94  // windows console always flushed?
95  }
96 
97  void set_pattern(const std::string &pattern) override final
98  {
99  std::lock_guard<mutex_t> lock(mutex_);
100  formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
101  }
102 
103  void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override final
104  {
105  std::lock_guard<mutex_t> lock(mutex_);
106  formatter_ = std::move(sink_formatter);
107  }
108 
109 private:
110  using mutex_t = typename ConsoleMutex::mutex_t;
111  // set color and return the orig console attributes (for resetting later)
112  WORD set_console_attribs(WORD attribs)
113  {
114  CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
115  ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
116  WORD back_color = orig_buffer_info.wAttributes;
117  // retrieve the current background color
118  back_color &= static_cast<WORD>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
119  // keep the background color unchanged
120  ::SetConsoleTextAttribute(out_handle_, attribs | back_color);
121  return orig_buffer_info.wAttributes; // return orig attribs
122  }
123 
124  // print a range of formatted message to console
125  void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
126  {
127  auto size = static_cast<DWORD>(end - start);
128  ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
129  }
130 
131  HANDLE out_handle_;
132  mutex_t &mutex_;
133  std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
134 };
135 
138 
141 
142 } // namespace sinks
143 } // namespace spdlog
wincolor_sink & operator=(const wincolor_sink &other)=delete
basic_memory_buffer< char > memory_buffer
Definition: format.h:553
void flush() final override
Definition: wincolor_sink.h:92
std::unique_ptr< spdlog::formatter > formatter_
Definition: sink.h:55
void log(const details::log_msg &msg) final override
Definition: wincolor_sink.h:68
void set_pattern(const std::string &pattern) override final
Definition: wincolor_sink.h:97
Definition: async.h:27
FMT_CONSTEXPR auto end(const C &c) -> decltype(c.end())
Definition: format.h:257
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) override final
color
Definition: color.h:50
void set_color(level::level_enum level, WORD color)
Definition: wincolor_sink.h:62
level::level_enum level() const
Definition: sink.h:45