1 | // |
2 | // Message.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Logging |
6 | // Module: Message |
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/Message.h" |
16 | #include "Poco/Exception.h" |
17 | #if !defined(POCO_VXWORKS) |
18 | #include "Poco/Process.h" |
19 | #endif |
20 | #include "Poco/Thread.h" |
21 | #include <algorithm> |
22 | |
23 | |
24 | namespace Poco { |
25 | |
26 | |
27 | Message::Message(): |
28 | _prio(PRIO_FATAL), |
29 | _tid(0), |
30 | _ostid(0), |
31 | _pid(0), |
32 | _file(0), |
33 | _line(0), |
34 | _pMap(0) |
35 | { |
36 | init(); |
37 | } |
38 | |
39 | |
40 | Message::Message(const std::string& source, const std::string& text, Priority prio): |
41 | _source(source), |
42 | _text(text), |
43 | _prio(prio), |
44 | _tid(0), |
45 | _ostid(0), |
46 | _pid(0), |
47 | _file(0), |
48 | _line(0), |
49 | _pMap(0) |
50 | { |
51 | init(); |
52 | } |
53 | |
54 | |
55 | Message::Message(const std::string& source, const std::string& text, Priority prio, const char* file, int line): |
56 | _source(source), |
57 | _text(text), |
58 | _prio(prio), |
59 | _tid(0), |
60 | _ostid(0), |
61 | _pid(0), |
62 | _file(file), |
63 | _line(line), |
64 | _pMap(0) |
65 | { |
66 | init(); |
67 | } |
68 | |
69 | |
70 | Message::Message(const Message& msg): |
71 | _source(msg._source), |
72 | _text(msg._text), |
73 | _prio(msg._prio), |
74 | _time(msg._time), |
75 | _tid(msg._tid), |
76 | _ostid(msg._ostid), |
77 | _thread(msg._thread), |
78 | _pid(msg._pid), |
79 | _file(msg._file), |
80 | _line(msg._line) |
81 | { |
82 | if (msg._pMap) |
83 | _pMap = new StringMap(*msg._pMap); |
84 | else |
85 | _pMap = 0; |
86 | } |
87 | |
88 | |
89 | Message::Message(Message&& msg) : |
90 | _source(std::move(msg._source)), |
91 | _text(std::move(msg._text)), |
92 | _prio(std::move(msg._prio)), |
93 | _time(std::move(msg._time)), |
94 | _tid(std::move(msg._tid)), |
95 | _thread(std::move(msg._thread)), |
96 | _pid(std::move(msg._pid)), |
97 | _file(std::move(msg._file)), |
98 | _line(std::move(msg._line)) |
99 | { |
100 | _pMap = msg._pMap; |
101 | msg._pMap = nullptr; |
102 | } |
103 | |
104 | |
105 | Message::Message(const Message& msg, const std::string& text): |
106 | _source(msg._source), |
107 | _text(text), |
108 | _prio(msg._prio), |
109 | _time(msg._time), |
110 | _tid(msg._tid), |
111 | _ostid(msg._ostid), |
112 | _thread(msg._thread), |
113 | _pid(msg._pid), |
114 | _file(msg._file), |
115 | _line(msg._line) |
116 | { |
117 | if (msg._pMap) |
118 | _pMap = new StringMap(*msg._pMap); |
119 | else |
120 | _pMap = 0; |
121 | } |
122 | |
123 | |
124 | Message::~Message() |
125 | { |
126 | delete _pMap; |
127 | } |
128 | |
129 | |
130 | void Message::init() |
131 | { |
132 | #if !defined(POCO_VXWORKS) |
133 | _pid = Process::id(); |
134 | #endif |
135 | _ostid = (IntPtr)Thread::currentTid(); |
136 | Thread* pThread = Thread::current(); |
137 | if (pThread) |
138 | { |
139 | _tid = pThread->id(); |
140 | _thread = pThread->name(); |
141 | } |
142 | } |
143 | |
144 | |
145 | Message& Message::operator = (const Message& msg) |
146 | { |
147 | if (&msg != this) |
148 | { |
149 | Message tmp(msg); |
150 | swap(tmp); |
151 | } |
152 | return *this; |
153 | } |
154 | |
155 | |
156 | Message& Message::operator = (Message&& msg) |
157 | { |
158 | if (&msg != this) |
159 | { |
160 | _source = std::move(msg._source); |
161 | _text = std::move(msg._text); |
162 | _prio = std::move(msg._prio); |
163 | _time = std::move(msg._time); |
164 | _tid = std::move(msg._tid); |
165 | _thread = std::move(msg._thread); |
166 | _pid = std::move(msg._pid); |
167 | _file = std::move(msg._file); |
168 | _line = std::move(msg._line); |
169 | delete _pMap; |
170 | _pMap = msg._pMap; |
171 | msg._pMap = nullptr; |
172 | } |
173 | return *this; |
174 | } |
175 | |
176 | |
177 | void Message::swap(Message& msg) |
178 | { |
179 | using std::swap; |
180 | swap(_source, msg._source); |
181 | swap(_text, msg._text); |
182 | swap(_prio, msg._prio); |
183 | swap(_time, msg._time); |
184 | swap(_tid, msg._tid); |
185 | swap(_thread, msg._thread); |
186 | swap(_pid, msg._pid); |
187 | swap(_file, msg._file); |
188 | swap(_line, msg._line); |
189 | swap(_pMap, msg._pMap); |
190 | } |
191 | |
192 | |
193 | void Message::setSource(const std::string& src) |
194 | { |
195 | _source = src; |
196 | } |
197 | |
198 | |
199 | void Message::setText(const std::string& text) |
200 | { |
201 | _text = text; |
202 | } |
203 | |
204 | |
205 | void Message::setPriority(Priority prio) |
206 | { |
207 | _prio = prio; |
208 | } |
209 | |
210 | |
211 | void Message::setTime(const Timestamp& t) |
212 | { |
213 | _time = t; |
214 | } |
215 | |
216 | |
217 | void Message::setThread(const std::string& thread) |
218 | { |
219 | _thread = thread; |
220 | } |
221 | |
222 | |
223 | void Message::setTid(long tid) |
224 | { |
225 | _tid = tid; |
226 | } |
227 | |
228 | |
229 | void Message::setPid(long pid) |
230 | { |
231 | _pid = pid; |
232 | } |
233 | |
234 | |
235 | void Message::setSourceFile(const char* file) |
236 | { |
237 | _file = file; |
238 | } |
239 | |
240 | |
241 | void Message::setSourceLine(int line) |
242 | { |
243 | _line = line; |
244 | } |
245 | |
246 | |
247 | bool Message::has(const std::string& param) const |
248 | { |
249 | return _pMap && (_pMap->find(param) != _pMap->end()); |
250 | } |
251 | |
252 | |
253 | const std::string& Message::get(const std::string& param) const |
254 | { |
255 | if (_pMap) |
256 | { |
257 | StringMap::const_iterator it = _pMap->find(param); |
258 | if (it != _pMap->end()) |
259 | return it->second; |
260 | } |
261 | |
262 | throw NotFoundException(); |
263 | } |
264 | |
265 | |
266 | const std::string& Message::get(const std::string& param, const std::string& defaultValue) const |
267 | { |
268 | if (_pMap) |
269 | { |
270 | StringMap::const_iterator it = _pMap->find(param); |
271 | if (it != _pMap->end()) |
272 | return it->second; |
273 | } |
274 | |
275 | return defaultValue; |
276 | } |
277 | |
278 | |
279 | void Message::set(const std::string& param, const std::string& value) |
280 | { |
281 | if (!_pMap) |
282 | _pMap = new StringMap; |
283 | |
284 | std::pair<StringMap::iterator, bool> result = |
285 | _pMap->insert(std::make_pair(param, value)); |
286 | if (!result.second) |
287 | { |
288 | result.first->second = value; |
289 | } |
290 | } |
291 | |
292 | |
293 | const std::string& Message::operator [] (const std::string& param) const |
294 | { |
295 | if (_pMap) |
296 | return (*_pMap)[param]; |
297 | else |
298 | throw NotFoundException(); |
299 | } |
300 | |
301 | |
302 | std::string& Message::operator [] (const std::string& param) |
303 | { |
304 | if (!_pMap) |
305 | _pMap = new StringMap; |
306 | return (*_pMap)[param]; |
307 | } |
308 | |
309 | |
310 | } // namespace Poco |
311 | |