Message.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // Message.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Message.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: Message
  9. //
  10. // Definition of the Message class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Message_INCLUDED
  18. #define Foundation_Message_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Timestamp.h"
  21. #include <map>
  22. namespace Poco {
  23. class Foundation_API Message
  24. /// This class represents a log message that is sent through a
  25. /// chain of log channels.
  26. ///
  27. /// A Message contains a priority denoting the severity of the
  28. /// message, a source describing its origin, a text describing
  29. /// its meaning, the time of its creation, and an identifier of
  30. /// the process and thread that created the message.
  31. ///
  32. /// Optionally a Message can also contain the source file path
  33. /// and line number of the statement generating the message.
  34. ///
  35. /// A Message can also contain any number of named parameters
  36. /// that contain additional information about the event that
  37. /// caused the message.
  38. {
  39. public:
  40. enum Priority
  41. {
  42. PRIO_FATAL = 1, /// A fatal error. The application will most likely terminate. This is the highest priority.
  43. PRIO_CRITICAL, /// A critical error. The application might not be able to continue running successfully.
  44. PRIO_ERROR, /// An error. An operation did not complete successfully, but the application as a whole is not affected.
  45. PRIO_WARNING, /// A warning. An operation completed with an unexpected result.
  46. PRIO_NOTICE, /// A notice, which is an information with just a higher priority.
  47. PRIO_INFORMATION, /// An informational message, usually denoting the successful completion of an operation.
  48. PRIO_DEBUG, /// A debugging message.
  49. PRIO_TRACE /// A tracing message. This is the lowest priority.
  50. };
  51. Message();
  52. /// Creates an empty Message.
  53. /// The thread and process ids are set.
  54. Message(const std::string& source, const std::string& text, Priority prio);
  55. /// Creates a Message with the given source, text and priority.
  56. /// The thread and process ids are set.
  57. Message(const std::string& source, const std::string& text, Priority prio, const char* file, int line);
  58. /// Creates a Message with the given source, text, priority,
  59. /// source file path and line.
  60. ///
  61. /// The source file path must be a
  62. /// static string with a lifetime that's at least the lifetime
  63. /// of the message object (the string is not copied internally).
  64. /// Usually, this will be the path string obtained from the
  65. /// __FILE__ macro.
  66. ///
  67. /// The thread and process ids are set.
  68. Message(const Message& msg);
  69. /// Creates a Message by copying another one.
  70. Message(const Message& msg, const std::string& text);
  71. /// Creates a Message by copying all but the text from another message.
  72. ~Message();
  73. /// Destroys the Message.
  74. Message& operator = (const Message& msg);
  75. /// Assignment operator.
  76. void swap(Message& msg);
  77. /// Swaps the message with another one.
  78. void setSource(const std::string& src);
  79. /// Sets the source of the message.
  80. const std::string& getSource() const;
  81. /// Returns the source of the message.
  82. void setText(const std::string& text);
  83. /// Sets the text of the message.
  84. const std::string& getText() const;
  85. /// Returns the text of the message.
  86. void setPriority(Priority prio);
  87. /// Sets the priority of the message.
  88. Priority getPriority() const;
  89. /// Returns the priority of the message.
  90. void setTime(const Timestamp& time);
  91. /// Sets the time of the message.
  92. const Timestamp& getTime() const;
  93. /// Returns the time of the message.
  94. void setThread(const std::string& thread);
  95. /// Sets the thread identifier for the message.
  96. const std::string& getThread() const;
  97. /// Returns the thread identifier for the message.
  98. void setTid(long pid);
  99. /// Sets the numeric thread identifier for the message.
  100. long getTid() const;
  101. /// Returns the numeric thread identifier for the message.
  102. void setPid(long pid);
  103. /// Sets the process identifier for the message.
  104. long getPid() const;
  105. /// Returns the process identifier for the message.
  106. void setSourceFile(const char* file);
  107. /// Sets the source file path of the statement
  108. /// generating the log message.
  109. ///
  110. /// File must be a static string, such as the value of
  111. /// the __FILE__ macro. The string is not copied
  112. /// internally for performance reasons.
  113. const char* getSourceFile() const;
  114. /// Returns the source file path of the code creating
  115. /// the message. May be 0 if not set.
  116. void setSourceLine(int line);
  117. /// Sets the source file line of the statement
  118. /// generating the log message.
  119. ///
  120. /// This is usually the result of the __LINE__
  121. /// macro.
  122. int getSourceLine() const;
  123. /// Returns the source file line of the statement
  124. /// generating the log message. May be 0
  125. /// if not set.
  126. bool has(const std::string& param) const;
  127. /// Returns true if a parameter with the given name exists.
  128. const std::string& get(const std::string& param) const;
  129. /// Returns a const reference to the value of the parameter
  130. /// with the given name. Throws a NotFoundException if the
  131. /// parameter does not exist.
  132. const std::string& get(const std::string& param, const std::string& defaultValue) const;
  133. /// Returns a const reference to the value of the parameter
  134. /// with the given name. If the parameter with the given name
  135. /// does not exist, then defaultValue is returned.
  136. void set(const std::string& param, const std::string& value);
  137. /// Sets the value for a parameter. If the parameter does
  138. /// not exist, then it is created.
  139. const std::string& operator [] (const std::string& param) const;
  140. /// Returns a const reference to the value of the parameter
  141. /// with the given name. Throws a NotFoundException if the
  142. /// parameter does not exist.
  143. std::string& operator [] (const std::string& param);
  144. /// Returns a reference to the value of the parameter with the
  145. /// given name. This can be used to set the parameter's value.
  146. /// If the parameter does not exist, it is created with an
  147. /// empty string value.
  148. protected:
  149. void init();
  150. typedef std::map<std::string, std::string> StringMap;
  151. private:
  152. std::string _source;
  153. std::string _text;
  154. Priority _prio;
  155. Timestamp _time;
  156. int _tid;
  157. std::string _thread;
  158. long _pid;
  159. const char* _file;
  160. int _line;
  161. StringMap* _pMap;
  162. };
  163. //
  164. // inlines
  165. //
  166. inline const std::string& Message::getSource() const
  167. {
  168. return _source;
  169. }
  170. inline const std::string& Message::getText() const
  171. {
  172. return _text;
  173. }
  174. inline Message::Priority Message::getPriority() const
  175. {
  176. return _prio;
  177. }
  178. inline const Timestamp& Message::getTime() const
  179. {
  180. return _time;
  181. }
  182. inline const std::string& Message::getThread() const
  183. {
  184. return _thread;
  185. }
  186. inline long Message::getTid() const
  187. {
  188. return _tid;
  189. }
  190. inline long Message::getPid() const
  191. {
  192. return _pid;
  193. }
  194. inline const char* Message::getSourceFile() const
  195. {
  196. return _file;
  197. }
  198. inline int Message::getSourceLine() const
  199. {
  200. return _line;
  201. }
  202. inline void swap(Message& m1, Message& m2)
  203. {
  204. m1.swap(m2);
  205. }
  206. } // namespace Poco
  207. #endif // Foundation_Message_INCLUDED