1 //
2 // Copyright(c) 2016 spdlog
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 #include "spdlog/sinks/base_sink.h"
9 #include "spdlog/details/null_mutex.h"
10 #include "spdlog/common.h"
11 
12 #include <mutex>
13 #include <string>
14 #include <map>
15 #include <wincon.h>
16 
17 namespace spdlog
18 {
19 namespace sinks
20 {
21 /*
22  * Windows color console sink. Uses WriteConsoleA to write to the console with colors
23  */
24 template<class Mutex>
25 class wincolor_sink: public  base_sink<Mutex>
26 {
27 public:
28     const WORD BOLD = FOREGROUND_INTENSITY;
29     const WORD RED = FOREGROUND_RED;
30     const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
31     const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
32     const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
33 
wincolor_sink(HANDLE std_handle)34     wincolor_sink(HANDLE std_handle): out_handle_(std_handle)
35     {
36         colors_[level::trace] = CYAN;
37         colors_[level::debug] = CYAN;
38         colors_[level::info] = WHITE | BOLD;
39         colors_[level::warn] = YELLOW | BOLD;
40         colors_[level::err] = RED | BOLD; // red bold
41         colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
42         colors_[level::off] = 0;
43     }
44 
~wincolor_sink()45     virtual ~wincolor_sink()
46     {
47         this->flush();
48     }
49 
50     wincolor_sink(const wincolor_sink& other) = delete;
51     wincolor_sink& operator=(const wincolor_sink& other) = delete;
52 
53 protected:
54     virtual void _sink_it(const details::log_msg& msg) override
55     {
56         auto color = colors_[msg.level];
57         auto orig_attribs = set_console_attribs(color);
58         WriteConsoleA(out_handle_, msg.formatted.data(), static_cast<DWORD>(msg.formatted.size()), nullptr, nullptr);
59         SetConsoleTextAttribute(out_handle_, orig_attribs); //reset to orig colors
60     }
61 
62     virtual void _flush() override
63     {
64         // windows console always flushed?
65     }
66 
67     // change the  color for the given level
set_color(level::level_enum level, WORD color)68     void set_color(level::level_enum level, WORD color)
69     {
70         std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
71         colors_[level] = color;
72     }
73 
74 private:
75     HANDLE out_handle_;
76     std::map<level::level_enum, WORD> colors_;
77 
78     // set color and return the orig console attributes (for resetting later)
set_console_attribs(WORD attribs)79     WORD set_console_attribs(WORD attribs)
80     {
81         CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
82         GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
83         WORD back_color = orig_buffer_info.wAttributes;
84         // retrieve the current background color
85         back_color &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
86         // keep the background color unchanged
87         SetConsoleTextAttribute(out_handle_, attribs | back_color);
88         return  orig_buffer_info.wAttributes; //return orig attribs
89     }
90 };
91 
92 //
93 // windows color console to stdout
94 //
95 template<class Mutex>
96 class wincolor_stdout_sink: public wincolor_sink<Mutex>
97 {
98 public:
wincolor_stdout_sink()99     wincolor_stdout_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_OUTPUT_HANDLE))
100     {}
101 };
102 
103 typedef wincolor_stdout_sink<std::mutex> wincolor_stdout_sink_mt;
104 typedef wincolor_stdout_sink<details::null_mutex> wincolor_stdout_sink_st;
105 
106 //
107 // windows color console to stderr
108 //
109 template<class Mutex>
110 class wincolor_stderr_sink: public wincolor_sink<Mutex>
111 {
112 public:
wincolor_stderr_sink()113     wincolor_stderr_sink() : wincolor_sink<Mutex>(GetStdHandle(STD_ERROR_HANDLE))
114     {}
115 };
116 
117 typedef wincolor_stderr_sink<std::mutex> wincolor_stderr_sink_mt;
118 typedef wincolor_stderr_sink<details::null_mutex> wincolor_stderr_sink_st;
119 
120 }
121 }
122