1 | // |
---|
2 | // LogStream.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/LogStream.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Logging |
---|
8 | // Module: LogStream |
---|
9 | // |
---|
10 | // Definition of the LogStream class. |
---|
11 | // |
---|
12 | // Copyright (c) 2006-2007, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_LogStream_INCLUDED |
---|
40 | #define Foundation_LogStream_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Logger.h" |
---|
45 | #include "Poco/UnbufferedStreamBuf.h" |
---|
46 | #include <istream> |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | class Foundation_API LogStreamBuf: public UnbufferedStreamBuf |
---|
53 | /// This class implements a streambuf interface |
---|
54 | /// to a Logger. |
---|
55 | /// |
---|
56 | /// The streambuf appends all characters written to it |
---|
57 | /// to a string. As soon as a CR or LF (std::endl) is written, |
---|
58 | /// the string is sent to the Logger, with the set |
---|
59 | /// priority. |
---|
60 | { |
---|
61 | public: |
---|
62 | LogStreamBuf(Logger& logger, Message::Priority priority); |
---|
63 | /// Creates the LogStream. |
---|
64 | |
---|
65 | ~LogStreamBuf(); |
---|
66 | /// Destroys the LogStream. |
---|
67 | |
---|
68 | void setPriority(Message::Priority priority); |
---|
69 | /// Sets the priority for log messages. |
---|
70 | |
---|
71 | Message::Priority getPriority() const; |
---|
72 | /// Returns the priority for log messages. |
---|
73 | |
---|
74 | Logger& logger() const; |
---|
75 | /// Returns a reference to the Logger. |
---|
76 | |
---|
77 | private: |
---|
78 | int writeToDevice(char c); |
---|
79 | |
---|
80 | private: |
---|
81 | Logger& _logger; |
---|
82 | Message::Priority _priority; |
---|
83 | std::string _message; |
---|
84 | }; |
---|
85 | |
---|
86 | |
---|
87 | class Foundation_API LogIOS: public virtual std::ios |
---|
88 | /// The base class for LogStream. |
---|
89 | /// |
---|
90 | /// This class is needed to ensure the correct initialization |
---|
91 | /// order of the stream buffer and base classes. |
---|
92 | { |
---|
93 | public: |
---|
94 | LogIOS(Logger& logger, Message::Priority priority); |
---|
95 | ~LogIOS(); |
---|
96 | LogStreamBuf* rdbuf(); |
---|
97 | |
---|
98 | protected: |
---|
99 | LogStreamBuf _buf; |
---|
100 | }; |
---|
101 | |
---|
102 | |
---|
103 | class Foundation_API LogStream: public LogIOS, public std::ostream |
---|
104 | /// This class implements an ostream interface |
---|
105 | /// to a Logger. |
---|
106 | /// |
---|
107 | /// The stream's buffer appends all characters written to it |
---|
108 | /// to a string. As soon as a CR or LF (std::endl) is written, |
---|
109 | /// the string is sent to the Logger, with the current |
---|
110 | /// priority. |
---|
111 | /// |
---|
112 | /// Usage example: |
---|
113 | /// LogStream ls(someLogger); |
---|
114 | /// ls << "Some informational message" << std::endl; |
---|
115 | /// ls.error() << "Some error message" << std::endl; |
---|
116 | { |
---|
117 | public: |
---|
118 | LogStream(Logger& logger, Message::Priority priority = Message::PRIO_INFORMATION); |
---|
119 | /// Creates the LogStream, using the given logger and priority. |
---|
120 | |
---|
121 | LogStream(const std::string& loggerName, Message::Priority priority = Message::PRIO_INFORMATION); |
---|
122 | /// Creates the LogStream, using the logger identified |
---|
123 | /// by loggerName, and sets the priority. |
---|
124 | |
---|
125 | ~LogStream(); |
---|
126 | /// Destroys the LogStream. |
---|
127 | |
---|
128 | LogStream& fatal(); |
---|
129 | /// Sets the priority for log messages to Message::PRIO_FATAL. |
---|
130 | |
---|
131 | LogStream& fatal(const std::string& message); |
---|
132 | /// Sets the priority for log messages to Message::PRIO_FATAL |
---|
133 | /// and writes the given message. |
---|
134 | |
---|
135 | LogStream& critical(); |
---|
136 | /// Sets the priority for log messages to Message::PRIO_CRITICAL. |
---|
137 | |
---|
138 | LogStream& critical(const std::string& message); |
---|
139 | /// Sets the priority for log messages to Message::PRIO_CRITICAL |
---|
140 | /// and writes the given message. |
---|
141 | |
---|
142 | LogStream& error(); |
---|
143 | /// Sets the priority for log messages to Message::PRIO_ERROR. |
---|
144 | |
---|
145 | LogStream& error(const std::string& message); |
---|
146 | /// Sets the priority for log messages to Message::PRIO_ERROR |
---|
147 | /// and writes the given message. |
---|
148 | |
---|
149 | LogStream& warning(); |
---|
150 | /// Sets the priority for log messages to Message::PRIO_WARNING. |
---|
151 | |
---|
152 | LogStream& warning(const std::string& message); |
---|
153 | /// Sets the priority for log messages to Message::PRIO_WARNING |
---|
154 | /// and writes the given message. |
---|
155 | |
---|
156 | LogStream& notice(); |
---|
157 | /// Sets the priority for log messages to Message::PRIO_NOTICE. |
---|
158 | |
---|
159 | LogStream& notice(const std::string& message); |
---|
160 | /// Sets the priority for log messages to Message::PRIO_NOTICE |
---|
161 | /// and writes the given message. |
---|
162 | |
---|
163 | LogStream& information(); |
---|
164 | /// Sets the priority for log messages to Message::PRIO_INFORMATION. |
---|
165 | |
---|
166 | LogStream& information(const std::string& message); |
---|
167 | /// Sets the priority for log messages to Message::PRIO_INFORMATION |
---|
168 | /// and writes the given message. |
---|
169 | |
---|
170 | LogStream& debug(); |
---|
171 | /// Sets the priority for log messages to Message::PRIO_DEBUG. |
---|
172 | |
---|
173 | LogStream& debug(const std::string& message); |
---|
174 | /// Sets the priority for log messages to Message::PRIO_DEBUG |
---|
175 | /// and writes the given message. |
---|
176 | |
---|
177 | LogStream& trace(); |
---|
178 | /// Sets the priority for log messages to Message::PRIO_TRACE. |
---|
179 | |
---|
180 | LogStream& trace(const std::string& message); |
---|
181 | /// Sets the priority for log messages to Message::PRIO_TRACE |
---|
182 | /// and writes the given message. |
---|
183 | |
---|
184 | LogStream& priority(Message::Priority priority); |
---|
185 | /// Sets the priority for log messages. |
---|
186 | }; |
---|
187 | |
---|
188 | |
---|
189 | // |
---|
190 | // inlines |
---|
191 | // |
---|
192 | inline Message::Priority LogStreamBuf::getPriority() const |
---|
193 | { |
---|
194 | return _priority; |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | inline Logger& LogStreamBuf::logger() const |
---|
199 | { |
---|
200 | return _logger; |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | } // namespace Poco |
---|
205 | |
---|
206 | |
---|
207 | #endif // Foundation_LogStream_INCLUDED |
---|