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