setcolor.hpp 9.4 KB

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