1 | // |
2 | // LogStream.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Logging |
6 | // Module: LogStream |
7 | // |
8 | // Copyright (c) 2006-2007, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/LogStream.h" |
16 | |
17 | |
18 | namespace Poco { |
19 | |
20 | |
21 | // |
22 | // LogStreamBuf |
23 | // |
24 | |
25 | |
26 | LogStreamBuf::LogStreamBuf(Logger& rLogger, Message::Priority priority): |
27 | _logger(rLogger), |
28 | _priority(priority) |
29 | { |
30 | } |
31 | |
32 | |
33 | LogStreamBuf::~LogStreamBuf() |
34 | { |
35 | } |
36 | |
37 | |
38 | void LogStreamBuf::setPriority(Message::Priority priority) |
39 | { |
40 | _priority = priority; |
41 | } |
42 | |
43 | |
44 | int LogStreamBuf::writeToDevice(char c) |
45 | { |
46 | if (c == '\n' || c == '\r') |
47 | { |
48 | if (_message.find_first_not_of("\r\n" ) != std::string::npos) |
49 | { |
50 | Message msg(_logger.name(), _message, _priority); |
51 | _message.clear(); |
52 | _logger.log(msg); |
53 | } |
54 | } |
55 | else _message += c; |
56 | return c; |
57 | } |
58 | |
59 | |
60 | // |
61 | // LogIOS |
62 | // |
63 | |
64 | |
65 | LogIOS::LogIOS(Logger& logger, Message::Priority priority): |
66 | _buf(logger, priority) |
67 | { |
68 | poco_ios_init(&_buf); |
69 | } |
70 | |
71 | |
72 | LogIOS::~LogIOS() |
73 | { |
74 | } |
75 | |
76 | |
77 | LogStreamBuf* LogIOS::rdbuf() |
78 | { |
79 | return &_buf; |
80 | } |
81 | |
82 | |
83 | // |
84 | // LogStream |
85 | // |
86 | |
87 | |
88 | LogStream::LogStream(Logger& logger, Message::Priority messagePriority): |
89 | LogIOS(logger, messagePriority), |
90 | std::ostream(&_buf) |
91 | { |
92 | } |
93 | |
94 | |
95 | LogStream::LogStream(const std::string& loggerName, Message::Priority messagePriority): |
96 | LogIOS(Logger::get(loggerName), messagePriority), |
97 | std::ostream(&_buf) |
98 | { |
99 | } |
100 | |
101 | |
102 | LogStream::~LogStream() |
103 | { |
104 | } |
105 | |
106 | |
107 | LogStream& LogStream::fatal() |
108 | { |
109 | return priority(Message::PRIO_FATAL); |
110 | } |
111 | |
112 | |
113 | LogStream& LogStream::fatal(const std::string& message) |
114 | { |
115 | _buf.logger().fatal(message); |
116 | return priority(Message::PRIO_FATAL); |
117 | } |
118 | |
119 | |
120 | LogStream& LogStream::critical() |
121 | { |
122 | return priority(Message::PRIO_CRITICAL); |
123 | } |
124 | |
125 | |
126 | LogStream& LogStream::critical(const std::string& message) |
127 | { |
128 | _buf.logger().critical(message); |
129 | return priority(Message::PRIO_CRITICAL); |
130 | } |
131 | |
132 | |
133 | LogStream& LogStream::error() |
134 | { |
135 | return priority(Message::PRIO_ERROR); |
136 | } |
137 | |
138 | |
139 | LogStream& LogStream::error(const std::string& message) |
140 | { |
141 | _buf.logger().error(message); |
142 | return priority(Message::PRIO_ERROR); |
143 | } |
144 | |
145 | |
146 | LogStream& LogStream::warning() |
147 | { |
148 | return priority(Message::PRIO_WARNING); |
149 | } |
150 | |
151 | |
152 | LogStream& LogStream::warning(const std::string& message) |
153 | { |
154 | _buf.logger().warning(message); |
155 | return priority(Message::PRIO_WARNING); |
156 | } |
157 | |
158 | |
159 | LogStream& LogStream::notice() |
160 | { |
161 | return priority(Message::PRIO_NOTICE); |
162 | } |
163 | |
164 | |
165 | LogStream& LogStream::notice(const std::string& message) |
166 | { |
167 | _buf.logger().notice(message); |
168 | return priority(Message::PRIO_NOTICE); |
169 | } |
170 | |
171 | |
172 | LogStream& LogStream::information() |
173 | { |
174 | return priority(Message::PRIO_INFORMATION); |
175 | } |
176 | |
177 | |
178 | LogStream& LogStream::information(const std::string& message) |
179 | { |
180 | _buf.logger().information(message); |
181 | return priority(Message::PRIO_INFORMATION); |
182 | } |
183 | |
184 | |
185 | LogStream& LogStream::debug() |
186 | { |
187 | return priority(Message::PRIO_DEBUG); |
188 | } |
189 | |
190 | |
191 | LogStream& LogStream::debug(const std::string& message) |
192 | { |
193 | _buf.logger().debug(message); |
194 | return priority(Message::PRIO_DEBUG); |
195 | } |
196 | |
197 | |
198 | LogStream& LogStream::trace() |
199 | { |
200 | return priority(Message::PRIO_TRACE); |
201 | } |
202 | |
203 | |
204 | LogStream& LogStream::trace(const std::string& message) |
205 | { |
206 | _buf.logger().trace(message); |
207 | return priority(Message::PRIO_TRACE); |
208 | } |
209 | |
210 | |
211 | LogStream& LogStream::priority(Message::Priority messagePriority) |
212 | { |
213 | _buf.setPriority(messagePriority); |
214 | return *this; |
215 | } |
216 | |
217 | |
218 | } // namespace Poco |
219 | |