]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/test/utils/setcolor.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / test / utils / setcolor.hpp
1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 // File : $RCSfile$
9 //
10 // Version : $Revision$
11 //
12 // Description : contains definition for setcolor iostream manipulator
13 // ***************************************************************************
14
15 #ifndef BOOST_TEST_UTILS_SETCOLOR_HPP
16 #define BOOST_TEST_UTILS_SETCOLOR_HPP
17
18 // Boost.Test
19 #include <boost/test/detail/config.hpp>
20
21 // STL
22 #include <iostream>
23 #include <cstdio>
24
25 #include <boost/test/detail/suppress_warnings.hpp>
26
27 #ifdef _WIN32
28 #include <windows.h>
29
30 #if defined(__MINGW32__) && !defined(COMMON_LVB_UNDERSCORE)
31 // mingw badly mimicking windows.h
32 #define COMMON_LVB_UNDERSCORE 0x8000
33 #endif
34 #endif
35
36 //____________________________________________________________________________//
37
38 namespace boost {
39 namespace unit_test {
40 namespace utils {
41
42 // ************************************************************************** //
43 // ************** term_attr ************** //
44 // ************************************************************************** //
45
46 struct term_attr { enum _ {
47 NORMAL = 0,
48 BRIGHT = 1,
49 DIM = 2,
50 UNDERLINE = 4,
51 BLINK = 5,
52 REVERSE = 7,
53 CROSSOUT = 9
54 }; };
55
56 // ************************************************************************** //
57 // ************** term_color ************** //
58 // ************************************************************************** //
59
60 struct term_color { enum _ {
61 BLACK = 0,
62 RED = 1,
63 GREEN = 2,
64 YELLOW = 3,
65 BLUE = 4,
66 MAGENTA = 5,
67 CYAN = 6,
68 WHITE = 7,
69 ORIGINAL = 9
70 }; };
71
72 // ************************************************************************** //
73 // ************** setcolor ************** //
74 // ************************************************************************** //
75
76 #ifndef _WIN32
77 class setcolor {
78 public:
79 // Constructor
80 explicit setcolor( term_attr::_ attr = term_attr::NORMAL,
81 term_color::_ fg = term_color::ORIGINAL,
82 term_color::_ bg = term_color::ORIGINAL )
83 {
84 m_command_size = std::sprintf( m_control_command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40 );
85 }
86
87 friend std::ostream&
88 operator<<( std::ostream& os, setcolor const& sc )
89 {
90 if (&os == &std::cout || &os == &std::cerr) {
91 return os.write( sc.m_control_command, sc.m_command_size );
92 }
93 return os;
94 }
95
96 private:
97 // Data members
98 char m_control_command[13];
99 int m_command_size;
100 };
101
102 #else
103
104 class setcolor {
105
106 protected:
107 void set_console_color(std::ostream& os, WORD *attributes = NULL) const {
108 DWORD console_type;
109 if (&os == &std::cout) {
110 console_type = STD_OUTPUT_HANDLE;
111 }
112 else if (&os == &std::cerr) {
113 console_type = STD_ERROR_HANDLE;
114 }
115 else {
116 return;
117 }
118 HANDLE hConsole = GetStdHandle(console_type);
119
120 if(hConsole == INVALID_HANDLE_VALUE || hConsole == NULL )
121 return;
122
123 if(attributes != NULL) {
124 SetConsoleTextAttribute(hConsole, *attributes);
125 return;
126 }
127
128 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
129 GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
130 //if(!has_written_console_ext) {
131 saved_attributes = consoleInfo.wAttributes;
132 //}
133
134 WORD fg_attr = 0;
135 switch(m_fg)
136 {
137 case term_color::WHITE:
138 fg_attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
139 break;
140 case term_color::BLACK:
141 fg_attr = 0;
142 break;
143 case term_color::RED:
144 fg_attr = FOREGROUND_RED;
145 break;
146 case term_color::GREEN:
147 fg_attr = FOREGROUND_GREEN;
148 break;
149 case term_color::CYAN:
150 fg_attr = FOREGROUND_GREEN | FOREGROUND_BLUE;
151 break;
152 case term_color::MAGENTA:
153 fg_attr = FOREGROUND_RED | FOREGROUND_BLUE;
154 break;
155 case term_color::BLUE:
156 fg_attr = FOREGROUND_BLUE;
157 break;
158 case term_color::YELLOW:
159 fg_attr = FOREGROUND_RED | FOREGROUND_GREEN;
160 break;
161 case term_color::ORIGINAL:
162 default:
163 fg_attr = saved_attributes & (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
164 break;
165 }
166
167 WORD bg_attr = 0;
168 switch(m_bg)
169 {
170 case term_color::BLACK:
171 bg_attr = 0;
172 break;
173 case term_color::WHITE:
174 bg_attr = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
175 break;
176 case term_color::RED:
177 bg_attr = BACKGROUND_RED;
178 break;
179 case term_color::GREEN:
180 bg_attr = BACKGROUND_GREEN;
181 break;
182 case term_color::BLUE:
183 bg_attr = BACKGROUND_BLUE;
184 break;
185 case term_color::ORIGINAL:
186 default:
187 bg_attr = saved_attributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
188 break;
189 }
190
191 WORD text_attr = 0;
192 switch(m_attr)
193 {
194 case term_attr::BRIGHT:
195 text_attr = FOREGROUND_INTENSITY;
196 break;
197 case term_attr::UNDERLINE:
198 text_attr = COMMON_LVB_UNDERSCORE;
199 break;
200 default:
201 break;
202 }
203
204 SetConsoleTextAttribute(hConsole, fg_attr | bg_attr | text_attr);
205
206 //has_written_console_ext = true;
207 return;
208 }
209
210 public:
211 // Constructor
212 explicit setcolor(
213 term_attr::_ attr = term_attr::NORMAL,
214 term_color::_ fg = term_color::ORIGINAL,
215 term_color::_ bg = term_color::ORIGINAL )
216 : /*has_written_console_ext(false)
217 , */m_attr(attr)
218 , m_fg(fg)
219 , m_bg(bg)
220 {}
221
222 friend std::ostream&
223 operator<<( std::ostream& os, setcolor const& sc )
224 {
225 sc.set_console_color(os);
226 return os;
227 }
228
229 private:
230 term_attr::_ m_attr;
231 term_color::_ m_fg;
232 term_color::_ m_bg;
233
234 protected:
235 // Data members
236 mutable WORD saved_attributes;
237 //mutable bool has_written_console_ext;
238 };
239
240 #endif
241 // ************************************************************************** //
242 // ************** scope_setcolor ************** //
243 // ************************************************************************** //
244
245 #ifndef _WIN32
246
247 struct scope_setcolor {
248 scope_setcolor() : m_os( 0 ) {}
249 explicit scope_setcolor( std::ostream& os,
250 term_attr::_ attr = term_attr::NORMAL,
251 term_color::_ fg = term_color::ORIGINAL,
252 term_color::_ bg = term_color::ORIGINAL )
253 : m_os( &os )
254 {
255 os << setcolor( attr, fg, bg );
256 }
257 ~scope_setcolor()
258 {
259 if( m_os )
260 *m_os << setcolor();
261 }
262 private:
263 scope_setcolor(const scope_setcolor& r);
264 scope_setcolor& operator=(const scope_setcolor& r);
265 // Data members
266 std::ostream* m_os;
267 };
268
269 #else
270
271 struct scope_setcolor : setcolor {
272 scope_setcolor() : m_os( 0 ) {}
273 explicit scope_setcolor(
274 std::ostream& os,
275 term_attr::_ attr = term_attr::NORMAL,
276 term_color::_ fg = term_color::ORIGINAL,
277 term_color::_ bg = term_color::ORIGINAL )
278 :
279 setcolor(attr, fg, bg),
280 m_os( &os )
281 {
282 os << *this;
283 }
284
285 ~scope_setcolor()
286 {
287 if (m_os) {
288 set_console_color(*m_os, &this->saved_attributes);
289 }
290 }
291 private:
292 scope_setcolor(const scope_setcolor& r);
293 scope_setcolor& operator=(const scope_setcolor& r);
294 // Data members
295 std::ostream* m_os;
296 };
297
298
299 #endif
300
301 #define BOOST_TEST_SCOPE_SETCOLOR( is_color_output, os, attr, color ) \
302 utils::scope_setcolor const sc( \
303 os, \
304 is_color_output ? utils::attr : utils::term_attr::NORMAL, \
305 is_color_output ? utils::color : utils::term_color::ORIGINAL);\
306 ut_detail::ignore_unused_variable_warning( sc ) \
307 /**/
308
309 } // namespace utils
310 } // namespace unit_test
311 } // namespace boost
312
313 #include <boost/test/detail/enable_warnings.hpp>
314
315 #endif // BOOST_TEST_UTILS_SETCOLOR_HPP