WindowsConsoleChannel.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // WindowsConsoleChannel.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/WindowsConsoleChannel.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: WindowsConsoleChannel
  9. //
  10. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/WindowsConsoleChannel.h"
  16. #include "Poco/Message.h"
  17. #if defined(POCO_WIN32_UTF8)
  18. #include "Poco/UnicodeConverter.h"
  19. #endif
  20. #include "Poco/String.h"
  21. #include "Poco/Exception.h"
  22. namespace Poco {
  23. WindowsConsoleChannel::WindowsConsoleChannel():
  24. _isFile(false),
  25. _hConsole(INVALID_HANDLE_VALUE)
  26. {
  27. _hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  28. // check whether the console has been redirected
  29. DWORD mode;
  30. _isFile = (GetConsoleMode(_hConsole, &mode) == 0);
  31. }
  32. WindowsConsoleChannel::~WindowsConsoleChannel()
  33. {
  34. }
  35. void WindowsConsoleChannel::log(const Message& msg)
  36. {
  37. std::string text = msg.getText();
  38. text += "\r\n";
  39. #if defined(POCO_WIN32_UTF8)
  40. if (_isFile)
  41. {
  42. DWORD written;
  43. WriteFile(_hConsole, text.data(), static_cast<DWORD>(text.size()), &written, NULL);
  44. }
  45. else
  46. {
  47. std::wstring utext;
  48. UnicodeConverter::toUTF16(text, utext);
  49. DWORD written;
  50. WriteConsoleW(_hConsole, utext.data(), static_cast<DWORD>(utext.size()), &written, NULL);
  51. }
  52. #else
  53. DWORD written;
  54. WriteFile(_hConsole, text.data(), text.size(), &written, NULL);
  55. #endif
  56. }
  57. WindowsColorConsoleChannel::WindowsColorConsoleChannel():
  58. _enableColors(true),
  59. _isFile(false),
  60. _hConsole(INVALID_HANDLE_VALUE)
  61. {
  62. _hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  63. // check whether the console has been redirected
  64. DWORD mode;
  65. _isFile = (GetConsoleMode(_hConsole, &mode) == 0);
  66. initColors();
  67. }
  68. WindowsColorConsoleChannel::~WindowsColorConsoleChannel()
  69. {
  70. }
  71. void WindowsColorConsoleChannel::log(const Message& msg)
  72. {
  73. std::string text = msg.getText();
  74. text += "\r\n";
  75. if (_enableColors && !_isFile)
  76. {
  77. WORD attr = _colors[0];
  78. attr &= 0xFFF0;
  79. attr |= _colors[msg.getPriority()];
  80. SetConsoleTextAttribute(_hConsole, attr);
  81. }
  82. #if defined(POCO_WIN32_UTF8)
  83. if (_isFile)
  84. {
  85. DWORD written;
  86. WriteFile(_hConsole, text.data(), static_cast<DWORD>(text.size()), &written, NULL);
  87. }
  88. else
  89. {
  90. std::wstring utext;
  91. UnicodeConverter::toUTF16(text, utext);
  92. DWORD written;
  93. WriteConsoleW(_hConsole, utext.data(), static_cast<DWORD>(utext.size()), &written, NULL);
  94. }
  95. #else
  96. DWORD written;
  97. WriteFile(_hConsole, text.data(), text.size(), &written, NULL);
  98. #endif
  99. if (_enableColors && !_isFile)
  100. {
  101. SetConsoleTextAttribute(_hConsole, _colors[0]);
  102. }
  103. }
  104. void WindowsColorConsoleChannel::setProperty(const std::string& name, const std::string& value)
  105. {
  106. if (name == "enableColors")
  107. {
  108. _enableColors = icompare(value, "true") == 0;
  109. }
  110. else if (name == "traceColor")
  111. {
  112. _colors[Message::PRIO_TRACE] = parseColor(value);
  113. }
  114. else if (name == "debugColor")
  115. {
  116. _colors[Message::PRIO_DEBUG] = parseColor(value);
  117. }
  118. else if (name == "informationColor")
  119. {
  120. _colors[Message::PRIO_INFORMATION] = parseColor(value);
  121. }
  122. else if (name == "noticeColor")
  123. {
  124. _colors[Message::PRIO_NOTICE] = parseColor(value);
  125. }
  126. else if (name == "warningColor")
  127. {
  128. _colors[Message::PRIO_WARNING] = parseColor(value);
  129. }
  130. else if (name == "errorColor")
  131. {
  132. _colors[Message::PRIO_ERROR] = parseColor(value);
  133. }
  134. else if (name == "criticalColor")
  135. {
  136. _colors[Message::PRIO_CRITICAL] = parseColor(value);
  137. }
  138. else if (name == "fatalColor")
  139. {
  140. _colors[Message::PRIO_FATAL] = parseColor(value);
  141. }
  142. else
  143. {
  144. Channel::setProperty(name, value);
  145. }
  146. }
  147. std::string WindowsColorConsoleChannel::getProperty(const std::string& name) const
  148. {
  149. if (name == "enableColors")
  150. {
  151. return _enableColors ? "true" : "false";
  152. }
  153. else if (name == "traceColor")
  154. {
  155. return formatColor(_colors[Message::PRIO_TRACE]);
  156. }
  157. else if (name == "debugColor")
  158. {
  159. return formatColor(_colors[Message::PRIO_DEBUG]);
  160. }
  161. else if (name == "informationColor")
  162. {
  163. return formatColor(_colors[Message::PRIO_INFORMATION]);
  164. }
  165. else if (name == "noticeColor")
  166. {
  167. return formatColor(_colors[Message::PRIO_NOTICE]);
  168. }
  169. else if (name == "warningColor")
  170. {
  171. return formatColor(_colors[Message::PRIO_WARNING]);
  172. }
  173. else if (name == "errorColor")
  174. {
  175. return formatColor(_colors[Message::PRIO_ERROR]);
  176. }
  177. else if (name == "criticalColor")
  178. {
  179. return formatColor(_colors[Message::PRIO_CRITICAL]);
  180. }
  181. else if (name == "fatalColor")
  182. {
  183. return formatColor(_colors[Message::PRIO_FATAL]);
  184. }
  185. else
  186. {
  187. return Channel::getProperty(name);
  188. }
  189. }
  190. WORD WindowsColorConsoleChannel::parseColor(const std::string& color) const
  191. {
  192. if (icompare(color, "default") == 0)
  193. return _colors[0];
  194. else if (icompare(color, "black") == 0)
  195. return CC_BLACK;
  196. else if (icompare(color, "red") == 0)
  197. return CC_RED;
  198. else if (icompare(color, "green") == 0)
  199. return CC_GREEN;
  200. else if (icompare(color, "brown") == 0)
  201. return CC_BROWN;
  202. else if (icompare(color, "blue") == 0)
  203. return CC_BLUE;
  204. else if (icompare(color, "magenta") == 0)
  205. return CC_MAGENTA;
  206. else if (icompare(color, "cyan") == 0)
  207. return CC_CYAN;
  208. else if (icompare(color, "gray") == 0)
  209. return CC_GRAY;
  210. else if (icompare(color, "darkGray") == 0)
  211. return CC_DARKGRAY;
  212. else if (icompare(color, "lightRed") == 0)
  213. return CC_LIGHTRED;
  214. else if (icompare(color, "lightGreen") == 0)
  215. return CC_LIGHTGREEN;
  216. else if (icompare(color, "yellow") == 0)
  217. return CC_YELLOW;
  218. else if (icompare(color, "lightBlue") == 0)
  219. return CC_LIGHTBLUE;
  220. else if (icompare(color, "lightMagenta") == 0)
  221. return CC_LIGHTMAGENTA;
  222. else if (icompare(color, "lightCyan") == 0)
  223. return CC_LIGHTCYAN;
  224. else if (icompare(color, "white") == 0)
  225. return CC_WHITE;
  226. else throw InvalidArgumentException("Invalid color value", color);
  227. }
  228. std::string WindowsColorConsoleChannel::formatColor(WORD color) const
  229. {
  230. switch (color)
  231. {
  232. case CC_BLACK: return "black";
  233. case CC_RED: return "red";
  234. case CC_GREEN: return "green";
  235. case CC_BROWN: return "brown";
  236. case CC_BLUE: return "blue";
  237. case CC_MAGENTA: return "magenta";
  238. case CC_CYAN: return "cyan";
  239. case CC_GRAY: return "gray";
  240. case CC_DARKGRAY: return "darkGray";
  241. case CC_LIGHTRED: return "lightRed";
  242. case CC_LIGHTGREEN: return "lightGreen";
  243. case CC_YELLOW: return "yellow";
  244. case CC_LIGHTBLUE: return "lightBlue";
  245. case CC_LIGHTMAGENTA: return "lightMagenta";
  246. case CC_LIGHTCYAN: return "lightCyan";
  247. case CC_WHITE: return "white";
  248. default: return "invalid";
  249. }
  250. }
  251. void WindowsColorConsoleChannel::initColors()
  252. {
  253. if (!_isFile)
  254. {
  255. CONSOLE_SCREEN_BUFFER_INFO csbi;
  256. GetConsoleScreenBufferInfo(_hConsole, &csbi);
  257. _colors[0] = csbi.wAttributes;
  258. }
  259. else
  260. {
  261. _colors[0] = CC_WHITE;
  262. }
  263. _colors[Message::PRIO_FATAL] = CC_LIGHTRED;
  264. _colors[Message::PRIO_CRITICAL] = CC_LIGHTRED;
  265. _colors[Message::PRIO_ERROR] = CC_LIGHTRED;
  266. _colors[Message::PRIO_WARNING] = CC_YELLOW;
  267. _colors[Message::PRIO_NOTICE] = _colors[0];
  268. _colors[Message::PRIO_INFORMATION] = _colors[0];
  269. _colors[Message::PRIO_DEBUG] = CC_GRAY;
  270. _colors[Message::PRIO_TRACE] = CC_GRAY;
  271. }
  272. } // namespace Poco