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