1 | // |
2 | // LogStream.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Logging |
6 | // Module: LogStream |
7 | // |
8 | // Definition of the LogStream class. |
9 | // |
10 | // Copyright (c) 2006-2007, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_LogStream_INCLUDED |
18 | #define Foundation_LogStream_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/Logger.h" |
23 | #include "Poco/UnbufferedStreamBuf.h" |
24 | #include <istream> |
25 | |
26 | |
27 | namespace Poco { |
28 | |
29 | |
30 | class Foundation_API LogStreamBuf: public UnbufferedStreamBuf |
31 | /// This class implements a streambuf interface |
32 | /// to a Logger. |
33 | /// |
34 | /// The streambuf appends all characters written to it |
35 | /// to a string. As soon as a CR or LF (std::endl) is written, |
36 | /// the string is sent to the Logger, with the set |
37 | /// priority. |
38 | { |
39 | public: |
40 | LogStreamBuf(Logger& logger, Message::Priority priority); |
41 | /// Creates the LogStream. |
42 | |
43 | ~LogStreamBuf(); |
44 | /// Destroys the LogStream. |
45 | |
46 | void setPriority(Message::Priority priority); |
47 | /// Sets the priority for log messages. |
48 | |
49 | Message::Priority getPriority() const; |
50 | /// Returns the priority for log messages. |
51 | |
52 | Logger& logger() const; |
53 | /// Returns a reference to the Logger. |
54 | |
55 | private: |
56 | int writeToDevice(char c); |
57 | |
58 | private: |
59 | Logger& _logger; |
60 | Message::Priority _priority; |
61 | std::string _message; |
62 | }; |
63 | |
64 | |
65 | class Foundation_API LogIOS: public virtual std::ios |
66 | /// The base class for LogStream. |
67 | /// |
68 | /// This class is needed to ensure the correct initialization |
69 | /// order of the stream buffer and base classes. |
70 | { |
71 | public: |
72 | LogIOS(Logger& logger, Message::Priority priority); |
73 | ~LogIOS(); |
74 | LogStreamBuf* rdbuf(); |
75 | |
76 | protected: |
77 | LogStreamBuf _buf; |
78 | }; |
79 | |
80 | |
81 | class Foundation_API LogStream: public LogIOS, public std::ostream |
82 | /// This class implements an ostream interface |
83 | /// to a Logger. |
84 | /// |
85 | /// The stream's buffer appends all characters written to it |
86 | /// to a string. As soon as a CR or LF (std::endl) is written, |
87 | /// the string is sent to the Logger, with the current |
88 | /// priority. |
89 | /// |
90 | /// Usage example: |
91 | /// LogStream ls(someLogger); |
92 | /// ls << "Some informational message" << std::endl; |
93 | /// ls.error() << "Some error message" << std::endl; |
94 | { |
95 | public: |
96 | LogStream(Logger& logger, Message::Priority priority = Message::PRIO_INFORMATION); |
97 | /// Creates the LogStream, using the given logger and priority. |
98 | |
99 | LogStream(const std::string& loggerName, Message::Priority priority = Message::PRIO_INFORMATION); |
100 | /// Creates the LogStream, using the logger identified |
101 | /// by loggerName, and sets the priority. |
102 | |
103 | ~LogStream(); |
104 | /// Destroys the LogStream. |
105 | |
106 | LogStream& fatal(); |
107 | /// Sets the priority for log messages to Message::PRIO_FATAL. |
108 | |
109 | LogStream& fatal(const std::string& message); |
110 | /// Sets the priority for log messages to Message::PRIO_FATAL |
111 | /// and writes the given message. |
112 | |
113 | LogStream& critical(); |
114 | /// Sets the priority for log messages to Message::PRIO_CRITICAL. |
115 | |
116 | LogStream& critical(const std::string& message); |
117 | /// Sets the priority for log messages to Message::PRIO_CRITICAL |
118 | /// and writes the given message. |
119 | |
120 | LogStream& error(); |
121 | /// Sets the priority for log messages to Message::PRIO_ERROR. |
122 | |
123 | LogStream& error(const std::string& message); |
124 | /// Sets the priority for log messages to Message::PRIO_ERROR |
125 | /// and writes the given message. |
126 | |
127 | LogStream& warning(); |
128 | /// Sets the priority for log messages to Message::PRIO_WARNING. |
129 | |
130 | LogStream& warning(const std::string& message); |
131 | /// Sets the priority for log messages to Message::PRIO_WARNING |
132 | /// and writes the given message. |
133 | |
134 | LogStream& notice(); |
135 | /// Sets the priority for log messages to Message::PRIO_NOTICE. |
136 | |
137 | LogStream& notice(const std::string& message); |
138 | /// Sets the priority for log messages to Message::PRIO_NOTICE |
139 | /// and writes the given message. |
140 | |
141 | LogStream& information(); |
142 | /// Sets the priority for log messages to Message::PRIO_INFORMATION. |
143 | |
144 | LogStream& information(const std::string& message); |
145 | /// Sets the priority for log messages to Message::PRIO_INFORMATION |
146 | /// and writes the given message. |
147 | |
148 | LogStream& debug(); |
149 | /// Sets the priority for log messages to Message::PRIO_DEBUG. |
150 | |
151 | LogStream& debug(const std::string& message); |
152 | /// Sets the priority for log messages to Message::PRIO_DEBUG |
153 | /// and writes the given message. |
154 | |
155 | LogStream& trace(); |
156 | /// Sets the priority for log messages to Message::PRIO_TRACE. |
157 | |
158 | LogStream& trace(const std::string& message); |
159 | /// Sets the priority for log messages to Message::PRIO_TRACE |
160 | /// and writes the given message. |
161 | |
162 | LogStream& priority(Message::Priority priority); |
163 | /// Sets the priority for log messages. |
164 | }; |
165 | |
166 | |
167 | // |
168 | // inlines |
169 | // |
170 | inline Message::Priority LogStreamBuf::getPriority() const |
171 | { |
172 | return _priority; |
173 | } |
174 | |
175 | |
176 | inline Logger& LogStreamBuf::logger() const |
177 | { |
178 | return _logger; |
179 | } |
180 | |
181 | |
182 | } // namespace Poco |
183 | |
184 | |
185 | #endif // Foundation_LogStream_INCLUDED |
186 | |