Message.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // Message.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Message.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: Message
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  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. namespace Poco {
  23. Message::Message():
  24. _prio(PRIO_FATAL),
  25. _tid(0),
  26. _pid(0),
  27. _file(0),
  28. _line(0),
  29. _pMap(0)
  30. {
  31. init();
  32. }
  33. Message::Message(const std::string& source, const std::string& text, Priority prio):
  34. _source(source),
  35. _text(text),
  36. _prio(prio),
  37. _tid(0),
  38. _pid(0),
  39. _file(0),
  40. _line(0),
  41. _pMap(0)
  42. {
  43. init();
  44. }
  45. Message::Message(const std::string& source, const std::string& text, Priority prio, const char* file, int line):
  46. _source(source),
  47. _text(text),
  48. _prio(prio),
  49. _tid(0),
  50. _pid(0),
  51. _file(file),
  52. _line(line),
  53. _pMap(0)
  54. {
  55. init();
  56. }
  57. Message::Message(const Message& msg):
  58. _source(msg._source),
  59. _text(msg._text),
  60. _prio(msg._prio),
  61. _time(msg._time),
  62. _tid(msg._tid),
  63. _thread(msg._thread),
  64. _pid(msg._pid),
  65. _file(msg._file),
  66. _line(msg._line)
  67. {
  68. if (msg._pMap)
  69. _pMap = new StringMap(*msg._pMap);
  70. else
  71. _pMap = 0;
  72. }
  73. Message::Message(const Message& msg, const std::string& text):
  74. _source(msg._source),
  75. _text(text),
  76. _prio(msg._prio),
  77. _time(msg._time),
  78. _tid(msg._tid),
  79. _thread(msg._thread),
  80. _pid(msg._pid),
  81. _file(msg._file),
  82. _line(msg._line)
  83. {
  84. if (msg._pMap)
  85. _pMap = new StringMap(*msg._pMap);
  86. else
  87. _pMap = 0;
  88. }
  89. Message::~Message()
  90. {
  91. delete _pMap;
  92. }
  93. void Message::init()
  94. {
  95. #if !defined(POCO_VXWORKS)
  96. _pid = Process::id();
  97. #endif
  98. Thread* pThread = Thread::current();
  99. if (pThread)
  100. {
  101. _tid = pThread->id();
  102. _thread = pThread->name();
  103. }
  104. }
  105. Message& Message::operator = (const Message& msg)
  106. {
  107. if (&msg != this)
  108. {
  109. Message tmp(msg);
  110. swap(tmp);
  111. }
  112. return *this;
  113. }
  114. void Message::swap(Message& msg)
  115. {
  116. using std::swap;
  117. swap(_source, msg._source);
  118. swap(_text, msg._text);
  119. swap(_prio, msg._prio);
  120. swap(_time, msg._time);
  121. swap(_tid, msg._tid);
  122. swap(_thread, msg._thread);
  123. swap(_pid, msg._pid);
  124. swap(_file, msg._file);
  125. swap(_line, msg._line);
  126. swap(_pMap, msg._pMap);
  127. }
  128. void Message::setSource(const std::string& src)
  129. {
  130. _source = src;
  131. }
  132. void Message::setText(const std::string& text)
  133. {
  134. _text = text;
  135. }
  136. void Message::setPriority(Priority prio)
  137. {
  138. _prio = prio;
  139. }
  140. void Message::setTime(const Timestamp& t)
  141. {
  142. _time = t;
  143. }
  144. void Message::setThread(const std::string& thread)
  145. {
  146. _thread = thread;
  147. }
  148. void Message::setTid(long tid)
  149. {
  150. _tid = tid;
  151. }
  152. void Message::setPid(long pid)
  153. {
  154. _pid = pid;
  155. }
  156. void Message::setSourceFile(const char* file)
  157. {
  158. _file = file;
  159. }
  160. void Message::setSourceLine(int line)
  161. {
  162. _line = line;
  163. }
  164. bool Message::has(const std::string& param) const
  165. {
  166. return _pMap && (_pMap->find(param) != _pMap->end());
  167. }
  168. const std::string& Message::get(const std::string& param) const
  169. {
  170. if (_pMap)
  171. {
  172. StringMap::const_iterator it = _pMap->find(param);
  173. if (it != _pMap->end())
  174. return it->second;
  175. }
  176. throw NotFoundException();
  177. }
  178. const std::string& Message::get(const std::string& param, const std::string& defaultValue) const
  179. {
  180. if (_pMap)
  181. {
  182. StringMap::const_iterator it = _pMap->find(param);
  183. if (it != _pMap->end())
  184. return it->second;
  185. }
  186. return defaultValue;
  187. }
  188. void Message::set(const std::string& param, const std::string& value)
  189. {
  190. if (!_pMap)
  191. _pMap = new StringMap;
  192. std::pair<StringMap::iterator, bool> result =
  193. _pMap->insert(std::make_pair(param, value));
  194. if (!result.second)
  195. {
  196. result.first->second = value;
  197. }
  198. }
  199. const std::string& Message::operator [] (const std::string& param) const
  200. {
  201. if (_pMap)
  202. return (*_pMap)[param];
  203. else
  204. throw NotFoundException();
  205. }
  206. std::string& Message::operator [] (const std::string& param)
  207. {
  208. if (!_pMap)
  209. _pMap = new StringMap;
  210. return (*_pMap)[param];
  211. }
  212. } // namespace Poco