ttcclayout.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: ttcclayout.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_TTCCLAYOUT_H
  25. #define LOG4QT_TTCCLAYOUT_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include "log4qt/layout.h"
  30. /******************************************************************************
  31. * Declarations
  32. ******************************************************************************/
  33. namespace Log4Qt
  34. {
  35. class LoggingEvent;
  36. class PatternFormatter;
  37. /*!
  38. * \brief The class TTCCLayout outputs the time, thread, logger and nested
  39. * diagnostic context information of a logging event.
  40. *
  41. * \note The ownership and lifetime of objects of this class are managed.
  42. * See \ref Ownership "Object ownership" for more details.
  43. */
  44. class TTCCLayout : public Layout
  45. {
  46. Q_OBJECT
  47. /*!
  48. * The property holds if the logger name is part of the formatted output.
  49. *
  50. * The default value is true for including the logger name.
  51. *
  52. * \sa categoryPrefixing(), setCategoryPrefixing()
  53. */
  54. Q_PROPERTY(bool categoryPrefixing READ categoryPrefixing WRITE setCategoryPrefixing)
  55. /*!
  56. * The property holds if the nested context information is part of the
  57. * formatted output.
  58. *
  59. * The default value it true for including the nested context information.
  60. *
  61. * \sa contextPrinting(), setContextPrinting()
  62. */
  63. Q_PROPERTY(bool contextPrinting READ contextPrinting WRITE setContextPrinting)
  64. /*!
  65. * The property holds the date format used by the layout.
  66. *
  67. * The default date format is "TIME_RELATIVE".
  68. *
  69. * \sa dateFormat(), setDateFormat()
  70. */
  71. Q_PROPERTY(QString dateFormat READ dateFormat WRITE setDateFormat)
  72. /*!
  73. * The property holds if the thread name is part of the formatted output.
  74. *
  75. * The default value it true for including the thread name.
  76. *
  77. * \sa threadPrinting(), setThreadPrinting()
  78. */
  79. Q_PROPERTY(bool threadPrinting READ threadPrinting WRITE setThreadPrinting)
  80. public:
  81. /*!
  82. * The enum DateFormat defines constants for date formats.
  83. *
  84. * \sa setDateFormat(DateFormat), DateTime::toString()
  85. */
  86. enum DateFormat
  87. {
  88. /*! The none date format string is "NONE". */
  89. NONE,
  90. /*!
  91. * The iso8601 date format string is "ISO8601". The date will be
  92. * formatted as yyyy-MM-dd hh:mm:ss.zzz.
  93. */
  94. ISO8601,
  95. /*!
  96. * The absolute date format string is "TIME_ABSOLUTE". The date will be
  97. * formatted as HH:mm:ss.zzz.
  98. */
  99. TIME_ABSOLUTE,
  100. /*!
  101. * The date date format string is "DATE". The date will be formatted
  102. * as MMM YYYY HH:mm:ss.zzzz.
  103. */
  104. DATE,
  105. /*!
  106. * The relative date format string is "TIME_RELATIVE". The date will be
  107. * formatted as milliseconds since start of the program.
  108. */
  109. TIME_RELATIVE
  110. };
  111. Q_ENUMS(DateFormat)
  112. TTCCLayout(QObject *pParent = 0);
  113. TTCCLayout(const QString &rDateFormat,
  114. QObject *pParent = 0);
  115. /*!
  116. * Creates a TTCCLayout with the date formar value specified by
  117. * the \a dateFormat constant and the parent \a pParent.
  118. */
  119. TTCCLayout(DateFormat dateFormat,
  120. QObject *pParent = 0);
  121. virtual ~TTCCLayout();
  122. private:
  123. TTCCLayout(const TTCCLayout &rOther); // Not implemented
  124. TTCCLayout &operator=(const TTCCLayout &rOther); // Not implemented
  125. public:
  126. bool categoryPrefixing() const;
  127. bool contextPrinting() const;
  128. QString dateFormat() const;
  129. // JAVA: bool ignoresThrowable() const;
  130. bool threadPrinting() const;
  131. void setCategoryPrefixing(bool categoryPrefixing);
  132. void setContextPrinting(bool contextPrinting);
  133. void setDateFormat(const QString &rDateFormat);
  134. /*!
  135. * Sets the date format to the value specified by the \a dateFormat
  136. * constant.
  137. */
  138. void setDateFormat(DateFormat dateFormat);
  139. // JAVA: setIgnoresThrowable(bool ignoresThrowable);
  140. void setThreadPrinting(bool threadPrinting);
  141. virtual QString format(const LoggingEvent &rEvent);
  142. protected:
  143. #ifndef QT_NO_DEBUG_STREAM
  144. /*!
  145. * Writes all object member variables to the given debug stream
  146. * \a rDebug and returns the stream.
  147. *
  148. * <tt>
  149. * %TTCCLayout(name:"TTCC" categoryprefixing:true
  150. * contextprinting:true dateformat:"ISO8601"
  151. * referencecount:1 threadprinting:true)
  152. * </tt>
  153. * \sa QDebug, operator<<(QDebug debug, const LogObject &rLogObject)
  154. */
  155. virtual QDebug debug(QDebug &rDebug) const;
  156. #endif // QT_NO_DEBUG_STREAM
  157. private:
  158. void updatePatternFormatter();
  159. private:
  160. bool mCategoryPrefixing;
  161. bool mContextPrinting;
  162. QString mDateFormat;
  163. bool mThreadPrinting;
  164. PatternFormatter *mpPatternFormatter;
  165. };
  166. /**************************************************************************
  167. * Operators, Helper
  168. **************************************************************************/
  169. /**************************************************************************
  170. * Inline
  171. **************************************************************************/
  172. inline bool TTCCLayout::categoryPrefixing() const
  173. { return mCategoryPrefixing; }
  174. inline bool TTCCLayout::contextPrinting() const
  175. { return mContextPrinting; }
  176. inline QString TTCCLayout::dateFormat() const
  177. { return mDateFormat; }
  178. inline bool TTCCLayout::threadPrinting() const
  179. { return mThreadPrinting; }
  180. inline void TTCCLayout::setCategoryPrefixing(bool categoryPrefixing)
  181. { mCategoryPrefixing = categoryPrefixing;
  182. updatePatternFormatter(); }
  183. inline void TTCCLayout::setContextPrinting(bool contextPrinting)
  184. { mContextPrinting = contextPrinting;
  185. updatePatternFormatter(); }
  186. inline void TTCCLayout::setDateFormat(const QString &rDateFormat)
  187. { mDateFormat = rDateFormat;
  188. updatePatternFormatter(); }
  189. inline void TTCCLayout::setThreadPrinting(bool threadPrinting)
  190. { mThreadPrinting = threadPrinting;
  191. updatePatternFormatter(); }
  192. } // namespace Log4Qt
  193. // Q_DECLARE_TYPEINFO(Log4Qt::TTCCLayout, Q_COMPLEX_TYPE); // Use default
  194. #endif // LOG4QT_TTCCLAYOUT_H