ConsoleChannel.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // ConsoleChannel.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/ConsoleChannel.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: ConsoleChannel
  9. //
  10. // Definition of the ConsoleChannel class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_ConsoleChannel_INCLUDED
  18. #define Foundation_ConsoleChannel_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Channel.h"
  21. #include "Poco/Mutex.h"
  22. #include <ostream>
  23. namespace Poco {
  24. class Foundation_API ConsoleChannel: public Channel
  25. /// A channel that writes to an ostream.
  26. ///
  27. /// Only the message's text is written, followed
  28. /// by a newline.
  29. ///
  30. /// Chain this channel to a FormattingChannel with an
  31. /// appropriate Formatter to control what is contained
  32. /// in the text.
  33. ///
  34. /// Similar to StreamChannel, except that a static
  35. /// mutex is used to protect against multiple
  36. /// console channels concurrently writing to the
  37. /// same stream.
  38. {
  39. public:
  40. ConsoleChannel();
  41. /// Creates the channel and attaches std::clog.
  42. ConsoleChannel(std::ostream& str);
  43. /// Creates the channel using the given stream.
  44. void log(const Message& msg);
  45. /// Logs the given message to the channel's stream.
  46. protected:
  47. ~ConsoleChannel();
  48. private:
  49. std::ostream& _str;
  50. static FastMutex _mutex;
  51. };
  52. class Foundation_API ColorConsoleChannel: public Channel
  53. /// A channel that writes to an ostream.
  54. ///
  55. /// Only the message's text is written, followed
  56. /// by a newline.
  57. ///
  58. /// Messages can be colored depending on priority.
  59. /// The console device must support ANSI escape codes
  60. /// in order to display colored messages.
  61. ///
  62. /// To enable message coloring, set the "enableColors"
  63. /// property to true (default). Furthermore, colors can be
  64. /// configured by setting the following properties
  65. /// (default values are given in parenthesis):
  66. ///
  67. /// * traceColor (gray)
  68. /// * debugColor (gray)
  69. /// * informationColor (default)
  70. /// * noticeColor (default)
  71. /// * warningColor (yellow)
  72. /// * errorColor (lightRed)
  73. /// * criticalColor (lightRed)
  74. /// * fatalColor (lightRed)
  75. ///
  76. /// The following color values are supported:
  77. ///
  78. /// * default
  79. /// * black
  80. /// * red
  81. /// * green
  82. /// * brown
  83. /// * blue
  84. /// * magenta
  85. /// * cyan
  86. /// * gray
  87. /// * darkgray
  88. /// * lightRed
  89. /// * lightGreen
  90. /// * yellow
  91. /// * lightBlue
  92. /// * lightMagenta
  93. /// * lightCyan
  94. /// * white
  95. ///
  96. /// Chain this channel to a FormattingChannel with an
  97. /// appropriate Formatter to control what is contained
  98. /// in the text.
  99. ///
  100. /// Similar to StreamChannel, except that a static
  101. /// mutex is used to protect against multiple
  102. /// console channels concurrently writing to the
  103. /// same stream.
  104. {
  105. public:
  106. ColorConsoleChannel();
  107. /// Creates the channel and attaches std::clog.
  108. ColorConsoleChannel(std::ostream& str);
  109. /// Creates the channel using the given stream.
  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_DEFAULT = 0x0027,
  135. CC_BLACK = 0x001e,
  136. CC_RED = 0x001f,
  137. CC_GREEN = 0x0020,
  138. CC_BROWN = 0x0021,
  139. CC_BLUE = 0x0022,
  140. CC_MAGENTA = 0x0023,
  141. CC_CYAN = 0x0024,
  142. CC_GRAY = 0x0025,
  143. CC_DARKGRAY = 0x011e,
  144. CC_LIGHTRED = 0x011f,
  145. CC_LIGHTGREEN = 0x0120,
  146. CC_YELLOW = 0x0121,
  147. CC_LIGHTBLUE = 0x0122,
  148. CC_LIGHTMAGENTA = 0x0123,
  149. CC_LIGHTCYAN = 0x0124,
  150. CC_WHITE = 0x0125
  151. };
  152. ~ColorConsoleChannel();
  153. Color parseColor(const std::string& color) const;
  154. std::string formatColor(Color color) const;
  155. void initColors();
  156. private:
  157. std::ostream& _str;
  158. bool _enableColors;
  159. Color _colors[9];
  160. static FastMutex _mutex;
  161. static const std::string CSI;
  162. };
  163. } // namespace Poco
  164. #endif // Foundation_ConsoleChannel_INCLUDED