1 | // |
2 | // ConsoleChannel.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Logging |
6 | // Module: ConsoleChannel |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/ConsoleChannel.h" |
16 | #include "Poco/Message.h" |
17 | #include "Poco/String.h" |
18 | #include "Poco/Exception.h" |
19 | #include <iostream> |
20 | |
21 | |
22 | namespace Poco { |
23 | |
24 | |
25 | FastMutex ConsoleChannel::_mutex; |
26 | |
27 | |
28 | ConsoleChannel::ConsoleChannel(): _str(std::clog) |
29 | { |
30 | } |
31 | |
32 | |
33 | ConsoleChannel::ConsoleChannel(std::ostream& str): _str(str) |
34 | { |
35 | } |
36 | |
37 | |
38 | ConsoleChannel::~ConsoleChannel() |
39 | { |
40 | } |
41 | |
42 | |
43 | void ConsoleChannel::log(const Message& msg) |
44 | { |
45 | FastMutex::ScopedLock lock(_mutex); |
46 | |
47 | _str << msg.getText() << std::endl; |
48 | } |
49 | |
50 | |
51 | FastMutex ColorConsoleChannel::_mutex; |
52 | const std::string ColorConsoleChannel::CSI("\033[" ); |
53 | |
54 | |
55 | ColorConsoleChannel::ColorConsoleChannel(): |
56 | _str(std::clog), |
57 | _enableColors(true) |
58 | { |
59 | initColors(); |
60 | } |
61 | |
62 | |
63 | ColorConsoleChannel::ColorConsoleChannel(std::ostream& str): |
64 | _str(str), |
65 | _enableColors(true) |
66 | { |
67 | initColors(); |
68 | } |
69 | |
70 | |
71 | ColorConsoleChannel::~ColorConsoleChannel() |
72 | { |
73 | } |
74 | |
75 | |
76 | void ColorConsoleChannel::log(const Message& msg) |
77 | { |
78 | FastMutex::ScopedLock lock(_mutex); |
79 | |
80 | if (_enableColors) |
81 | { |
82 | int color = _colors[msg.getPriority()]; |
83 | if (color & 0x100) |
84 | { |
85 | _str << CSI << "1m" ; |
86 | } |
87 | color &= 0xff; |
88 | _str << CSI << color << "m" ; |
89 | } |
90 | |
91 | _str << msg.getText(); |
92 | |
93 | if (_enableColors) |
94 | { |
95 | _str << CSI << "0m" ; |
96 | } |
97 | |
98 | _str << std::endl; |
99 | } |
100 | |
101 | |
102 | void ColorConsoleChannel::setProperty(const std::string& name, const std::string& value) |
103 | { |
104 | if (name == "enableColors" ) |
105 | { |
106 | _enableColors = icompare(value, "true" ) == 0; |
107 | } |
108 | else if (name == "traceColor" ) |
109 | { |
110 | _colors[Message::PRIO_TRACE] = parseColor(value); |
111 | } |
112 | else if (name == "debugColor" ) |
113 | { |
114 | _colors[Message::PRIO_DEBUG] = parseColor(value); |
115 | } |
116 | else if (name == "informationColor" ) |
117 | { |
118 | _colors[Message::PRIO_INFORMATION] = parseColor(value); |
119 | } |
120 | else if (name == "noticeColor" ) |
121 | { |
122 | _colors[Message::PRIO_NOTICE] = parseColor(value); |
123 | } |
124 | else if (name == "warningColor" ) |
125 | { |
126 | _colors[Message::PRIO_WARNING] = parseColor(value); |
127 | } |
128 | else if (name == "errorColor" ) |
129 | { |
130 | _colors[Message::PRIO_ERROR] = parseColor(value); |
131 | } |
132 | else if (name == "criticalColor" ) |
133 | { |
134 | _colors[Message::PRIO_CRITICAL] = parseColor(value); |
135 | } |
136 | else if (name == "fatalColor" ) |
137 | { |
138 | _colors[Message::PRIO_FATAL] = parseColor(value); |
139 | } |
140 | else |
141 | { |
142 | Channel::setProperty(name, value); |
143 | } |
144 | } |
145 | |
146 | |
147 | std::string ColorConsoleChannel::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 | |
191 | |
192 | ColorConsoleChannel::Color ColorConsoleChannel::parseColor(const std::string& color) const |
193 | { |
194 | if (icompare(color, "default" ) == 0) |
195 | return CC_DEFAULT; |
196 | else if (icompare(color, "black" ) == 0) |
197 | return CC_BLACK; |
198 | else if (icompare(color, "red" ) == 0) |
199 | return CC_RED; |
200 | else if (icompare(color, "green" ) == 0) |
201 | return CC_GREEN; |
202 | else if (icompare(color, "brown" ) == 0) |
203 | return CC_BROWN; |
204 | else if (icompare(color, "blue" ) == 0) |
205 | return CC_BLUE; |
206 | else if (icompare(color, "magenta" ) == 0) |
207 | return CC_MAGENTA; |
208 | else if (icompare(color, "cyan" ) == 0) |
209 | return CC_CYAN; |
210 | else if (icompare(color, "gray" ) == 0) |
211 | return CC_GRAY; |
212 | else if (icompare(color, "darkGray" ) == 0) |
213 | return CC_DARKGRAY; |
214 | else if (icompare(color, "lightRed" ) == 0) |
215 | return CC_LIGHTRED; |
216 | else if (icompare(color, "lightGreen" ) == 0) |
217 | return CC_LIGHTGREEN; |
218 | else if (icompare(color, "yellow" ) == 0) |
219 | return CC_YELLOW; |
220 | else if (icompare(color, "lightBlue" ) == 0) |
221 | return CC_LIGHTBLUE; |
222 | else if (icompare(color, "lightMagenta" ) == 0) |
223 | return CC_LIGHTMAGENTA; |
224 | else if (icompare(color, "lightCyan" ) == 0) |
225 | return CC_LIGHTCYAN; |
226 | else if (icompare(color, "white" ) == 0) |
227 | return CC_WHITE; |
228 | else throw InvalidArgumentException("Invalid color value" , color); |
229 | } |
230 | |
231 | |
232 | std::string ColorConsoleChannel::formatColor(Color color) const |
233 | { |
234 | switch (color) |
235 | { |
236 | case CC_DEFAULT: return "default" ; |
237 | case CC_BLACK: return "black" ; |
238 | case CC_RED: return "red" ; |
239 | case CC_GREEN: return "green" ; |
240 | case CC_BROWN: return "brown" ; |
241 | case CC_BLUE: return "blue" ; |
242 | case CC_MAGENTA: return "magenta" ; |
243 | case CC_CYAN: return "cyan" ; |
244 | case CC_GRAY: return "gray" ; |
245 | case CC_DARKGRAY: return "darkGray" ; |
246 | case CC_LIGHTRED: return "lightRed" ; |
247 | case CC_LIGHTGREEN: return "lightGreen" ; |
248 | case CC_YELLOW: return "yellow" ; |
249 | case CC_LIGHTBLUE: return "lightBlue" ; |
250 | case CC_LIGHTMAGENTA: return "lightMagenta" ; |
251 | case CC_LIGHTCYAN: return "lightCyan" ; |
252 | case CC_WHITE: return "white" ; |
253 | default: return "invalid" ; |
254 | } |
255 | } |
256 | |
257 | |
258 | void ColorConsoleChannel::initColors() |
259 | { |
260 | _colors[0] = CC_DEFAULT; // unused |
261 | _colors[Message::PRIO_FATAL] = CC_LIGHTRED; |
262 | _colors[Message::PRIO_CRITICAL] = CC_LIGHTRED; |
263 | _colors[Message::PRIO_ERROR] = CC_LIGHTRED; |
264 | _colors[Message::PRIO_WARNING] = CC_YELLOW; |
265 | _colors[Message::PRIO_NOTICE] = CC_DEFAULT; |
266 | _colors[Message::PRIO_INFORMATION] = CC_DEFAULT; |
267 | _colors[Message::PRIO_DEBUG] = CC_GRAY; |
268 | _colors[Message::PRIO_TRACE] = CC_GRAY; |
269 | } |
270 | |
271 | |
272 | } // namespace Poco |
273 | |