WindowsConsoleChannel.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // WindowsConsoleChannel.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/WindowsConsoleChannel.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: WindowsConsoleChannel
  9. //
  10. // Definition of the WindowsConsoleChannel class.
  11. //
  12. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_WindowsConsoleChannel_INCLUDED
  18. #define Foundation_WindowsConsoleChannel_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Channel.h"
  21. #include "Poco/Mutex.h"
  22. #include "Poco/UnWindows.h"
  23. namespace Poco {
  24. class Foundation_API WindowsConsoleChannel: public Channel
  25. /// A channel that writes to the Windows console.
  26. ///
  27. /// Only the message's text is written, followed
  28. /// by a newline.
  29. ///
  30. /// If POCO has been compiled with POCO_WIN32_UTF8,
  31. /// log messages are assumed to be UTF-8 encoded, and
  32. /// are converted to UTF-16 prior to writing them to the
  33. /// console. This is the main difference to the ConsoleChannel
  34. /// class, which cannot handle UTF-8 encoded messages on Windows.
  35. ///
  36. /// Chain this channel to a FormattingChannel with an
  37. /// appropriate Formatter to control what is contained
  38. /// in the text.
  39. ///
  40. /// Only available on Windows platforms.
  41. {
  42. public:
  43. WindowsConsoleChannel();
  44. /// Creates the WindowsConsoleChannel.
  45. void log(const Message& msg);
  46. /// Logs the given message to the channel's stream.
  47. protected:
  48. ~WindowsConsoleChannel();
  49. private:
  50. HANDLE _hConsole;
  51. bool _isFile;
  52. };
  53. class Foundation_API WindowsColorConsoleChannel: public Channel
  54. /// A channel that writes to the Windows console.
  55. ///
  56. /// Only the message's text is written, followed
  57. /// by a newline.
  58. ///
  59. /// If POCO has been compiled with POCO_WIN32_UTF8,
  60. /// log messages are assumed to be UTF-8 encoded, and
  61. /// are converted to UTF-16 prior to writing them to the
  62. /// console. This is the main difference to the ConsoleChannel
  63. /// class, which cannot handle UTF-8 encoded messages on Windows.
  64. ///
  65. /// Messages can be colored depending on priority.
  66. ///
  67. /// To enable message coloring, set the "enableColors"
  68. /// property to true (default). Furthermore, colors can be
  69. /// configured by setting the following properties
  70. /// (default values are given in parenthesis):
  71. ///
  72. /// * traceColor (gray)
  73. /// * debugColor (gray)
  74. /// * informationColor (default)
  75. /// * noticeColor (default)
  76. /// * warningColor (yellow)
  77. /// * errorColor (lightRed)
  78. /// * criticalColor (lightRed)
  79. /// * fatalColor (lightRed)
  80. ///
  81. /// The following color values are supported:
  82. ///
  83. /// * default
  84. /// * black
  85. /// * red
  86. /// * green
  87. /// * brown
  88. /// * blue
  89. /// * magenta
  90. /// * cyan
  91. /// * gray
  92. /// * darkgray
  93. /// * lightRed
  94. /// * lightGreen
  95. /// * yellow
  96. /// * lightBlue
  97. /// * lightMagenta
  98. /// * lightCyan
  99. /// * white
  100. ///
  101. /// Chain this channel to a FormattingChannel with an
  102. /// appropriate Formatter to control what is contained
  103. /// in the text.
  104. ///
  105. /// Only available on Windows platforms.
  106. {
  107. public:
  108. WindowsColorConsoleChannel();
  109. /// Creates the WindowsConsoleChannel.
  110. void log(const Message& msg);
  111. /// Logs the given message to the channel's stream.
  112. void setProperty(const std::string& name, const std::string& value);
  113. /// Sets the property with the given name.
  114. ///
  115. /// The following properties are supported:
  116. /// * enableColors: Enable or disable colors.
  117. /// * traceColor: Specify color for trace messages.
  118. /// * debugColor: Specify color for debug messages.
  119. /// * informationColor: Specify color for information messages.
  120. /// * noticeColor: Specify color for notice messages.
  121. /// * warningColor: Specify color for warning messages.
  122. /// * errorColor: Specify color for error messages.
  123. /// * criticalColor: Specify color for critical messages.
  124. /// * fatalColor: Specify color for fatal messages.
  125. ///
  126. /// See the class documentation for a list of supported color values.
  127. std::string getProperty(const std::string& name) const;
  128. /// Returns the value of the property with the given name.
  129. /// See setProperty() for a description of the supported
  130. /// properties.
  131. protected:
  132. enum Color
  133. {
  134. CC_BLACK = 0x0000,
  135. CC_RED = 0x0004,
  136. CC_GREEN = 0x0002,
  137. CC_BROWN = 0x0006,
  138. CC_BLUE = 0x0001,
  139. CC_MAGENTA = 0x0005,
  140. CC_CYAN = 0x0003,
  141. CC_GRAY = 0x0007,
  142. CC_DARKGRAY = 0x0008,
  143. CC_LIGHTRED = 0x000C,
  144. CC_LIGHTGREEN = 0x000A,
  145. CC_YELLOW = 0x000E,
  146. CC_LIGHTBLUE = 0x0009,
  147. CC_LIGHTMAGENTA = 0x000D,
  148. CC_LIGHTCYAN = 0x000B,
  149. CC_WHITE = 0x000F
  150. };
  151. ~WindowsColorConsoleChannel();
  152. WORD parseColor(const std::string& color) const;
  153. std::string formatColor(WORD color) const;
  154. void initColors();
  155. private:
  156. bool _enableColors;
  157. HANDLE _hConsole;
  158. bool _isFile;
  159. WORD _colors[9];
  160. };
  161. } // namespace Poco
  162. #endif // Foundation_WindowsConsoleChannel_INCLUDED