loggingevent.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: loggingevent.h
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * Copyright 2007 Martin Heinrich
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************/
  24. #ifndef LOG4QT_LOG4QTEVENT_H
  25. #define LOG4QT_LOG4QTEVENT_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include <QtCore/QDateTime>
  30. #include <QtCore/QHash>
  31. #include <QtCore/QMetaType>
  32. #include <QtCore/QStringList>
  33. #include "log4qt/level.h"
  34. /******************************************************************************
  35. * Declarations
  36. ******************************************************************************/
  37. namespace Log4Qt
  38. {
  39. class Logger;
  40. /*!
  41. * \brief The class LoggingEvent is the internal representation of a
  42. * logging event.
  43. *
  44. * The class uses milliseconds since 1970-01-01T00:00:00, Coordinated
  45. * Universal Time for time values. For converstion from and to QDateTime
  46. * use DateTime.
  47. */
  48. class LoggingEvent
  49. {
  50. public:
  51. LoggingEvent();
  52. LoggingEvent(const Logger *pLogger,
  53. Level level,
  54. const QString &rMessage);
  55. LoggingEvent(const Logger *pLogger,
  56. Level level,
  57. const QString &rMessage,
  58. qint64 timeStamp);
  59. LoggingEvent(const Logger *pLogger,
  60. Level level,
  61. const QString &rMessage,
  62. const QString &rNdc,
  63. const QHash<QString, QString> &rProperties,
  64. const QString &rThreadName,
  65. qint64 timeStamp);
  66. // LoggingEvent(const LoggingEvent &LoggingEvent::rOther); // Use compiler default
  67. // virtual ~LoggingEvent(); // Use compiler default
  68. // LoggingEvent &operator=(const LoggingEvent &LoggingEvent::rOther); // Use compiler default
  69. // JAVA: QString fqnOfLoggerClass() const;
  70. Level level() const;
  71. // LocationInformation locationInformation() const;
  72. const Logger *logger() const;
  73. QString message() const;
  74. QHash<QString, QString> mdc() const;
  75. QString ndc() const;
  76. QHash<QString, QString> properties() const;
  77. qint64 sequenceNumber() const;
  78. QString threadName() const;
  79. // JAVA: ThrowableInformation throwableInformation() const;
  80. qint64 timeStamp() const;
  81. // JAVA: bool locationInformationExists() const;
  82. QString loggerName() const;
  83. QString property(const QString &rKey) const;
  84. QStringList propertyKeys() const;
  85. void setProperty(const QString &rKey, const QString &rValue);
  86. // JAVA: QString throwableStrRep() const;
  87. QString toString() const;
  88. static qint64 sequenceCount();
  89. static qint64 startTime();
  90. private:
  91. void setThreadNameToCurrent();
  92. static qint64 nextSequenceNumber();
  93. private:
  94. Level mLevel;
  95. const Logger *mpLogger;
  96. QString mMessage;
  97. QString mNdc;
  98. QHash<QString, QString> mProperties;
  99. qint64 mSequenceNumber;
  100. QString mThreadName;
  101. qint64 mTimeStamp;
  102. static qint64 msSequenceCount;
  103. #ifndef QT_NO_DATASTREAM
  104. // Needs to be friend to stream objects
  105. friend QDataStream &operator<<(QDataStream &rStream,
  106. const LoggingEvent &rLoggingEvent);
  107. friend QDataStream &operator>>(QDataStream &rStream,
  108. LoggingEvent &rLoggingEvent);
  109. #endif // QT_NO_DATASTREAM
  110. };
  111. /**************************************************************************
  112. * Operators, Helper
  113. **************************************************************************/
  114. #ifndef QT_NO_DATASTREAM
  115. /*!
  116. * \relates LoggingEvent
  117. *
  118. * Writes the given error \a rLoggingEvent to the given stream \a rStream,
  119. * and returns a reference to the stream.
  120. */
  121. QDataStream &operator<<(QDataStream &rStream,
  122. const LoggingEvent &rLoggingEvent);
  123. /*!
  124. * \relates LoggingEvent
  125. *
  126. * Reads an error from the given stream \a rStream into the given
  127. * error \a rLoggingEvent, and returns a reference to the stream.
  128. */
  129. QDataStream &operator>>(QDataStream &rStream,
  130. LoggingEvent &rLoggingEvent);
  131. #endif // QT_NO_DATASTREAM
  132. #ifndef QT_NO_DEBUG_STREAM
  133. /*!
  134. * \relates LoggingEvent
  135. *
  136. * Writes all object member variables to the given debug stream \a debug and
  137. * returns the stream.
  138. *
  139. * <tt>
  140. * %LoggingEvent(level:"WARN" logger:"Log4Qt::Properties"
  141. * message:"Unknown escape sequence '\j' in property starting at line 1"
  142. * sequencenumber:14 threadname:"main"
  143. * timestamp:1194337148937(QDateTime("Tue Nov 6 03:19:08 2007") )
  144. * sequenceCount: 14 )
  145. * </tt>
  146. *
  147. * \sa QDebug
  148. */
  149. QDebug operator<<(QDebug debug,
  150. const LoggingEvent &rLoggingEvent);
  151. #endif // QT_NO_DEBUG_STREAM
  152. /**************************************************************************
  153. * Inline
  154. **************************************************************************/
  155. inline Level LoggingEvent::level() const
  156. { return mLevel; }
  157. inline const Logger *LoggingEvent::logger() const
  158. { return mpLogger; }
  159. inline QString LoggingEvent::message() const
  160. { return mMessage; }
  161. inline QHash<QString, QString> LoggingEvent::mdc() const
  162. { return mProperties; }
  163. inline QString LoggingEvent::ndc() const
  164. { return mNdc; }
  165. inline QHash<QString, QString> LoggingEvent::properties() const
  166. { return mProperties; }
  167. inline qint64 LoggingEvent::sequenceNumber() const
  168. { return mSequenceNumber; }
  169. inline QString LoggingEvent::threadName() const
  170. { return mThreadName; }
  171. inline qint64 LoggingEvent::timeStamp() const
  172. { return mTimeStamp; }
  173. inline QString LoggingEvent::property(const QString &rKey) const
  174. { return mProperties.value(rKey); }
  175. inline QStringList LoggingEvent::propertyKeys() const
  176. { return QStringList(mProperties.keys()); }
  177. inline void LoggingEvent::setProperty(const QString &rKey, const QString &rValue)
  178. { mProperties.insert(rKey, rValue); }
  179. } // namespace Log4Qt
  180. Q_DECLARE_METATYPE(Log4Qt::LoggingEvent)
  181. Q_DECLARE_TYPEINFO(Log4Qt::LoggingEvent, Q_MOVABLE_TYPE);
  182. #endif // LOG4QT_LOG4QTEVENT_H