patternformatter.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: patternformatter.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_PATTERNFORMATTER_H
  25. #define LOG4QT_PATTERNFORMATTER_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include <QtCore/QList>
  30. #include <QtCore/QString>
  31. /******************************************************************************
  32. * Declarations
  33. ******************************************************************************/
  34. namespace Log4Qt
  35. {
  36. class FormattingInfo;
  37. class PatternConverter;
  38. class LoggingEvent;
  39. /*!
  40. * \brief The class PatternFormatter formats a logging event based on a
  41. * pattern string.
  42. *
  43. * The class PatternFormatter formats a LoggingEvent base on a pattern
  44. * string. It is used by the patternLayout and TTCCLayout class to
  45. * implement the formatting.
  46. *
  47. * On object construction the provided patterns tring is parsed. Based on
  48. * the information found a chain of PatternConverter is created. Each
  49. * PatternConverter handles a certain member of a LoggingEvent.
  50. *
  51. * \sa PatternLayout::format()
  52. * \sa TTCCLayout::format()
  53. */
  54. class PatternFormatter
  55. {
  56. public:
  57. /*!
  58. * Creates a PatternFormatter using a the specified \a rPattern.
  59. */
  60. PatternFormatter(const QString &rPattern);
  61. /*!
  62. * Destroys the PatternFormatter and all PatternConverter.
  63. */
  64. virtual ~PatternFormatter();
  65. private:
  66. PatternFormatter(const PatternFormatter &rOther); // Not implemented
  67. PatternFormatter &operator=(const PatternFormatter &rOther); // Not implemented
  68. public:
  69. /*!
  70. * Formats the given \a rLoggingEvent using the chain of
  71. * PatternConverter created during construction from the specified
  72. * pattern.
  73. */
  74. QString format(const LoggingEvent &rLoggingEvent) const;
  75. private:
  76. /*!
  77. * If the character \a rDigit is a digit the digit is added to the
  78. * integer \a rValue and the function returns true. Otherwise the
  79. * function returns false.
  80. *
  81. * The function adds the digit by multiplying the existing value
  82. * with ten and adding the numerical value of the digit. If the
  83. * maximum integer value would be exceeded by the operation
  84. * \a rValue is set to INT_MAX.
  85. */
  86. bool addDigit(const QChar &rDigit,
  87. int &rValue);
  88. /*!
  89. * Creates a PatternConverter based on the specified conversion
  90. * character \a rChar, the formatting information
  91. * \a rFormattingInfo and the option \a rOption.
  92. *
  93. * The PatternConverter converter is appended to the list of
  94. * PatternConverters.
  95. */
  96. void createConverter(const QChar &rChar,
  97. const FormattingInfo &rFormattingInfo,
  98. const QString &rOption = QString());
  99. /*!
  100. * Creates a LiteralPatternConverter with the string literal
  101. * \a rLiteral.
  102. *
  103. * The PatternConverter converter is appended to the list of
  104. * PatternConverters.
  105. */
  106. void createLiteralConverter(const QString &rLiteral);
  107. /*!
  108. * Parses the pattern string specified on construction and creates
  109. * PatternConverter according to it.
  110. */
  111. void parse();
  112. /*!
  113. * Parses an integer option from an option string. If the string is
  114. * not a valid integer or the integer value is less then zero, zero
  115. * is returned. Returns the end of line seperator for the operating
  116. * system.
  117. */
  118. int parseIntegerOption(const QString &rOption);
  119. private:
  120. const QString mIgnoreCharacters;
  121. const QString mConversionCharacters;
  122. const QString mOptionCharacters;
  123. QString mPattern;
  124. QList<PatternConverter *> mPatternConverters;
  125. // Needs to be friend to access internal data
  126. friend QDebug operator<<(QDebug, const PatternFormatter &rPatternFormatter);
  127. };
  128. /**************************************************************************
  129. * Operators, Helper
  130. **************************************************************************/
  131. #ifndef QT_NO_DEBUG_STREAM
  132. /*!
  133. * \relates PatternFormatter
  134. *
  135. * Writes all object member variables to the given debug stream \a rDebug and
  136. * returns the stream.
  137. *
  138. * <tt>
  139. * %PatternFormatter(pattern:"%r [%t] %p %c %x - %m%n"
  140. * converters:(
  141. * DatePatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) format: "TIME_RELATIVE" ) ,
  142. * LiteralPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) literal: " [" ) ,
  143. * BasicPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) type: "THREAD_CONVERTER" ) ,
  144. * LiteralPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) literal: "] " ) ,
  145. * BasicPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) type: "LEVEL_CONVERTER" ) ,
  146. * LiteralPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) literal: " " ) ,
  147. * LoggerPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) precision: 0 ) ,
  148. * LiteralPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) literal: " " ) ,
  149. * BasicPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) type: "NDC_CONVERTER" ) ,
  150. * LiteralPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) literal: " - " ) ,
  151. * BasicPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) type: "MESSAGE_CONVERTER" ) ,
  152. * LiteralPatternConverter(FormattingInfo(min:"0" max:"INT_MAX" left:false) literal: "" ) ) )
  153. * </tt>
  154. * \sa QDebug
  155. */
  156. QDebug operator<<(QDebug debug,
  157. const PatternFormatter &rPatternFormatter);
  158. #endif // QT_NO_DEBUG_STREAM
  159. /**************************************************************************
  160. * Inline
  161. **************************************************************************/
  162. } // namespace Log4Qt
  163. Q_DECLARE_TYPEINFO(Log4Qt::PatternFormatter, Q_MOVABLE_TYPE);
  164. #endif // LOG4QT_PATTERNFORMATTER_H