| 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 | _ostid(std::move(msg._ostid)), |
| 96 | _thread(std::move(msg._thread)), |
| 97 | _pid(std::move(msg._pid)), |
| 98 | _file(std::move(msg._file)), |
| 99 | _line(std::move(msg._line)) |
| 100 | { |
| 101 | _pMap = msg._pMap; |
| 102 | msg._pMap = nullptr; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | Message::Message(const Message& msg, const std::string& text): |
| 107 | _source(msg._source), |
| 108 | _text(text), |
| 109 | _prio(msg._prio), |
| 110 | _time(msg._time), |
| 111 | _tid(msg._tid), |
| 112 | _ostid(msg._ostid), |
| 113 | _thread(msg._thread), |
| 114 | _pid(msg._pid), |
| 115 | _file(msg._file), |
| 116 | _line(msg._line) |
| 117 | { |
| 118 | if (msg._pMap) |
| 119 | _pMap = new StringMap(*msg._pMap); |
| 120 | else |
| 121 | _pMap = 0; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | Message::~Message() |
| 126 | { |
| 127 | delete _pMap; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void Message::init() |
| 132 | { |
| 133 | #if !defined(POCO_VXWORKS) |
| 134 | _pid = Process::id(); |
| 135 | #endif |
| 136 | _ostid = (IntPtr)Thread::currentOsTid(); |
| 137 | Thread* pThread = Thread::current(); |
| 138 | if (pThread) |
| 139 | { |
| 140 | _tid = pThread->id(); |
| 141 | _thread = pThread->name(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | Message& Message::operator = (const Message& msg) |
| 147 | { |
| 148 | if (&msg != this) |
| 149 | { |
| 150 | Message tmp(msg); |
| 151 | swap(tmp); |
| 152 | } |
| 153 | return *this; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | Message& Message::operator = (Message&& msg) |
| 158 | { |
| 159 | if (&msg != this) |
| 160 | { |
| 161 | _source = std::move(msg._source); |
| 162 | _text = std::move(msg._text); |
| 163 | _prio = std::move(msg._prio); |
| 164 | _time = std::move(msg._time); |
| 165 | _tid = std::move(msg._tid); |
| 166 | _ostid = std::move(msg._ostid); |
| 167 | _thread = std::move(msg._thread); |
| 168 | _pid = std::move(msg._pid); |
| 169 | _file = std::move(msg._file); |
| 170 | _line = std::move(msg._line); |
| 171 | delete _pMap; |
| 172 | _pMap = msg._pMap; |
| 173 | msg._pMap = nullptr; |
| 174 | } |
| 175 | return *this; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | void Message::swap(Message& msg) |
| 180 | { |
| 181 | using std::swap; |
| 182 | swap(_source, msg._source); |
| 183 | swap(_text, msg._text); |
| 184 | swap(_prio, msg._prio); |
| 185 | swap(_time, msg._time); |
| 186 | swap(_tid, msg._tid); |
| 187 | swap(_ostid, msg._ostid); |
| 188 | swap(_thread, msg._thread); |
| 189 | swap(_pid, msg._pid); |
| 190 | swap(_file, msg._file); |
| 191 | swap(_line, msg._line); |
| 192 | swap(_pMap, msg._pMap); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void Message::setSource(const std::string& src) |
| 197 | { |
| 198 | _source = src; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | void Message::setText(const std::string& text) |
| 203 | { |
| 204 | _text = text; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | void Message::setPriority(Priority prio) |
| 209 | { |
| 210 | _prio = prio; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | void Message::setTime(const Timestamp& t) |
| 215 | { |
| 216 | _time = t; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | void Message::setThread(const std::string& thread) |
| 221 | { |
| 222 | _thread = thread; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | void Message::setTid(long tid) |
| 227 | { |
| 228 | _tid = tid; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void Message::setPid(long pid) |
| 233 | { |
| 234 | _pid = pid; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | void Message::setSourceFile(const char* file) |
| 239 | { |
| 240 | _file = file; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | void Message::setSourceLine(int line) |
| 245 | { |
| 246 | _line = line; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | bool Message::has(const std::string& param) const |
| 251 | { |
| 252 | return _pMap && (_pMap->find(param) != _pMap->end()); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | const std::string& Message::get(const std::string& param) const |
| 257 | { |
| 258 | if (_pMap) |
| 259 | { |
| 260 | StringMap::const_iterator it = _pMap->find(param); |
| 261 | if (it != _pMap->end()) |
| 262 | return it->second; |
| 263 | } |
| 264 | |
| 265 | throw NotFoundException(); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | const std::string& Message::get(const std::string& param, const std::string& defaultValue) const |
| 270 | { |
| 271 | if (_pMap) |
| 272 | { |
| 273 | StringMap::const_iterator it = _pMap->find(param); |
| 274 | if (it != _pMap->end()) |
| 275 | return it->second; |
| 276 | } |
| 277 | |
| 278 | return defaultValue; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | void Message::set(const std::string& param, const std::string& value) |
| 283 | { |
| 284 | if (!_pMap) |
| 285 | _pMap = new StringMap; |
| 286 | |
| 287 | std::pair<StringMap::iterator, bool> result = |
| 288 | _pMap->insert(std::make_pair(param, value)); |
| 289 | if (!result.second) |
| 290 | { |
| 291 | result.first->second = value; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | const std::string& Message::operator [] (const std::string& param) const |
| 297 | { |
| 298 | if (_pMap) |
| 299 | return (*_pMap)[param]; |
| 300 | else |
| 301 | throw NotFoundException(); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | std::string& Message::operator [] (const std::string& param) |
| 306 | { |
| 307 | if (!_pMap) |
| 308 | _pMap = new StringMap; |
| 309 | return (*_pMap)[param]; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | } // namespace Poco |
| 314 | |