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