patternlayout.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: patternlayout.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_PATTERNLAYOUT_H
  25. #define LOG4QT_PATTERNLAYOUT_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include "log4qt/layout.h"
  30. /******************************************************************************
  31. * Declarations
  32. ******************************************************************************/
  33. namespace Log4Qt
  34. {
  35. class PatternFormatter;
  36. /*!
  37. * \brief The class PatternLayout outputs a logging event based on a
  38. * pattern string.
  39. *
  40. * \note The ownership and lifetime of objects of this class are managed.
  41. * See \ref Ownership "Object ownership" for more details.
  42. */
  43. class PatternLayout : public Layout
  44. {
  45. Q_OBJECT
  46. /*!
  47. * The property holds the conversion pattern used by the appender.
  48. *
  49. * The default is "%m%n".
  50. *
  51. * \sa conversionPattern(), setConversionPattern()
  52. */
  53. Q_PROPERTY(QString conversionPattern READ conversionPattern WRITE setConversionPattern)
  54. public:
  55. /*!
  56. * The enum ConversionPattern defines constants for pattern strings.
  57. *
  58. * \sa setConversionPattern(ConversionPattern);
  59. */
  60. enum ConversionPattern
  61. {
  62. /*! The default conversion pattern string is "%m,%n". */
  63. DEFAULT_CONVERSION_PATTERN,
  64. /*!
  65. * The ttcc conversion pattern string is
  66. * "%r [%t] %p %c %x - %m%n".
  67. */
  68. TTCC_CONVERSION_PATTERN,
  69. };
  70. Q_ENUMS(ConversionPattern)
  71. PatternLayout(QObject *pParent = 0);
  72. PatternLayout(const QString &rPattern,
  73. QObject *pParent = 0);
  74. /*!
  75. * Creates a PatternLayout with the conversion pattern value specified
  76. * by the \a conversionPattern constant.
  77. */
  78. PatternLayout(ConversionPattern conversionPattern,
  79. QObject *pParent = 0);
  80. virtual ~PatternLayout();
  81. private:
  82. PatternLayout(const PatternLayout &rOther); // Not implemented
  83. PatternLayout &operator=(const PatternLayout &rOther); // Not implemented
  84. public:
  85. QString conversionPattern() const;
  86. void setConversionPattern(const QString &rPattern);
  87. /*!
  88. * Sets the conversion pattern to the value specified by the
  89. * \a conversionPattern constant.
  90. */
  91. void setConversionPattern(ConversionPattern conversionPattern);
  92. virtual QString format(const LoggingEvent &rEvent);
  93. protected:
  94. #ifndef QT_NO_DEBUG_STREAM
  95. /*!
  96. * Writes all object member variables to the given debug stream
  97. * \a rDebug and returns the stream.
  98. *
  99. * <tt>
  100. * %PatternLayout(name:"PL" pattern:"%r [%t] %p %c %x - %m%n"
  101. * "referencecount:3")
  102. * </tt>
  103. * \sa QDebug, operator<<(QDebug debug, const LogObject &rLogObject)
  104. */
  105. virtual QDebug debug(QDebug &rDebug) const;
  106. #endif // QT_NO_DEBUG_STREAM
  107. private:
  108. void updatePatternFormatter();
  109. private:
  110. QString mPattern;
  111. PatternFormatter *mpPatternFormatter;
  112. };
  113. /**************************************************************************
  114. * Operators, Helper
  115. **************************************************************************/
  116. /**************************************************************************
  117. * Inline
  118. **************************************************************************/
  119. inline QString PatternLayout::conversionPattern() const
  120. { return PatternLayout::mPattern; }
  121. inline void PatternLayout::setConversionPattern(const QString &rPattern)
  122. { mPattern = rPattern;
  123. updatePatternFormatter(); }
  124. } // namespace Log4Qt
  125. // Q_DECLARE_TYPEINFO(Log4Qt::PatternLayout, Q_COMPLEX_TYPE); // Use default
  126. #endif // LOG4QT_PATTERNLAYOUT_H