ttcclayout.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: ttcclayout.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/ttcclayout.h"
  28. #include <QtCore/QDebug>
  29. #include <QtCore/QDateTime>
  30. #include "log4qt/helpers/datetime.h"
  31. #include "log4qt/helpers/patternformatter.h"
  32. #include "log4qt/logger.h"
  33. #include "log4qt/loggingevent.h"
  34. namespace Log4Qt
  35. {
  36. /**************************************************************************
  37. * Declarations
  38. **************************************************************************/
  39. /**************************************************************************
  40. * C helper functions
  41. **************************************************************************/
  42. /**************************************************************************
  43. * Class implementation: TTCCLayout
  44. **************************************************************************/
  45. TTCCLayout::TTCCLayout(QObject *pParent) :
  46. Layout(pParent),
  47. mCategoryPrefixing(true),
  48. mContextPrinting(true),
  49. mDateFormat(),
  50. mThreadPrinting(true),
  51. mpPatternFormatter(0)
  52. {
  53. setDateFormat(TIME_RELATIVE);
  54. }
  55. TTCCLayout::TTCCLayout(const QString &rDateFormat,
  56. QObject *pParent) :
  57. Layout(pParent),
  58. mCategoryPrefixing(true),
  59. mContextPrinting(true),
  60. mDateFormat(rDateFormat),
  61. mThreadPrinting(true),
  62. mpPatternFormatter(0)
  63. {
  64. }
  65. TTCCLayout::TTCCLayout(DateFormat dateFormat,
  66. QObject *pParent) :
  67. Layout(pParent),
  68. mCategoryPrefixing(true),
  69. mContextPrinting(true),
  70. mDateFormat(),
  71. mThreadPrinting(true),
  72. mpPatternFormatter(0)
  73. {
  74. setDateFormat(dateFormat);
  75. }
  76. TTCCLayout::~TTCCLayout()
  77. {
  78. delete mpPatternFormatter;
  79. }
  80. void TTCCLayout::setDateFormat(DateFormat dateFormat)
  81. {
  82. switch (dateFormat)
  83. {
  84. case NONE:
  85. setDateFormat(QLatin1String("NONE"));
  86. break;
  87. case ISO8601:
  88. setDateFormat(QLatin1String("ISO8601"));
  89. break;
  90. case TIME_ABSOLUTE:
  91. setDateFormat(QLatin1String("TIME_ABSOLUTE"));
  92. break;
  93. case DATE:
  94. setDateFormat(QLatin1String("DATE"));
  95. break;
  96. case TIME_RELATIVE:
  97. setDateFormat(QLatin1String("TIME_RELATIVE"));
  98. break;
  99. default:
  100. Q_ASSERT_X(false, "TTCCLayout::setDateFormat", "Unkown DateFormat");
  101. setDateFormat(QString());
  102. }
  103. }
  104. QString TTCCLayout::format(const LoggingEvent &rEvent)
  105. {
  106. Q_ASSERT_X(mpPatternFormatter, "TTCCLayout::format()", "mpPatternConverter must not be null");
  107. return mpPatternFormatter->format(rEvent);
  108. }
  109. void TTCCLayout::updatePatternFormatter()
  110. {
  111. QString pattern;
  112. pattern += QLatin1String("%d{") + mDateFormat + QLatin1String("}");
  113. if (mThreadPrinting)
  114. pattern += QLatin1String(" [%t]");
  115. pattern += QLatin1String(" %-5p");
  116. if (mCategoryPrefixing)
  117. pattern += QLatin1String(" %c");
  118. if (mContextPrinting)
  119. pattern += QLatin1String(" %x");
  120. pattern += QLatin1String(" - %m%n");
  121. delete mpPatternFormatter;
  122. mpPatternFormatter = new PatternFormatter(pattern);
  123. }
  124. #ifndef QT_NO_DEBUG_STREAM
  125. QDebug TTCCLayout::debug(QDebug &rDebug) const
  126. {
  127. rDebug.nospace() << "TTCCLayout("
  128. << "name:" << name() << " "
  129. << "categoryprefixing:" << categoryPrefixing() << " "
  130. << "contextprinting:" << contextPrinting() << " "
  131. << "dateformat:" << dateFormat() << " "
  132. << "referencecount:" << referenceCount() << " "
  133. << "threadprinting:" << threadPrinting()
  134. << ")";
  135. return rDebug.space();
  136. }
  137. #endif // QT_NO_DEBUG_STREAM
  138. /**************************************************************************
  139. * Implementation: Operators, Helper
  140. **************************************************************************/
  141. } // namespace Log4Qt