patternlayout.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: patternlayout.cpp
  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. /******************************************************************************
  25. * Dependencies
  26. ******************************************************************************/
  27. #include "log4qt/patternlayout.h"
  28. #include <QtCore/QDebug>
  29. #include "log4qt/helpers/patternformatter.h"
  30. #include "log4qt/loggingevent.h"
  31. namespace Log4Qt
  32. {
  33. /**************************************************************************
  34. * Declarations
  35. **************************************************************************/
  36. /**************************************************************************
  37. * C helper functions
  38. **************************************************************************/
  39. /**************************************************************************
  40. * Class implementation: PatternLayout
  41. **************************************************************************/
  42. PatternLayout::PatternLayout(QObject *pParent) :
  43. Layout(pParent),
  44. mPattern(),
  45. mpPatternFormatter(0)
  46. {
  47. setConversionPattern(DEFAULT_CONVERSION_PATTERN);
  48. }
  49. PatternLayout::PatternLayout(const QString &rPattern,
  50. QObject *pParent) :
  51. Layout(pParent),
  52. mPattern(),
  53. mpPatternFormatter(0)
  54. {
  55. setConversionPattern(rPattern);
  56. }
  57. PatternLayout::PatternLayout(ConversionPattern conversionPattern,
  58. QObject *pParent) :
  59. Layout(pParent),
  60. mPattern(),
  61. mpPatternFormatter(0)
  62. {
  63. setConversionPattern(conversionPattern);
  64. }
  65. PatternLayout::~PatternLayout()
  66. {
  67. delete mpPatternFormatter;
  68. }
  69. void PatternLayout::setConversionPattern(ConversionPattern conversionPattern)
  70. {
  71. switch (conversionPattern)
  72. {
  73. case DEFAULT_CONVERSION_PATTERN:
  74. setConversionPattern(QLatin1String("%m%n"));
  75. break;
  76. case TTCC_CONVERSION_PATTERN:
  77. setConversionPattern(QLatin1String("%r [%t] %p %c %x - %m%n"));
  78. break;
  79. default:
  80. Q_ASSERT_X(false, "PatternLayout::setConversionFormat", "Unkown ConversionFormat");
  81. setConversionPattern(QString());
  82. }
  83. }
  84. QString PatternLayout::format(const LoggingEvent &rEvent)
  85. {
  86. Q_ASSERT_X(mpPatternFormatter, "PatternLayout::format()", "mpPatternConverter must not be null");
  87. return mpPatternFormatter->format(rEvent);
  88. }
  89. void PatternLayout::updatePatternFormatter()
  90. {
  91. delete mpPatternFormatter;
  92. mpPatternFormatter = new PatternFormatter(mPattern);
  93. }
  94. #ifndef QT_NO_DEBUG_STREAM
  95. QDebug PatternLayout::debug(QDebug &rDebug) const
  96. {
  97. rDebug.nospace() << "PatternLayout("
  98. << "name:" << name() << " "
  99. << "pattern:" << conversionPattern() << " "
  100. << "referencecount:" << referenceCount()
  101. << ")";
  102. return rDebug.space();
  103. }
  104. #endif // QT_NO_DEBUG_STREAM
  105. /**************************************************************************
  106. * Implementation: Operators, Helper
  107. **************************************************************************/
  108. } // namespace Log4Qt