dailyrollingfileappender.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: dailyrollingfileappender.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_DAILYROLLINGFILEAPPENDER_H
  25. #define LOG4QT_DAILYROLLINGFILEAPPENDER_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include "log4qt/fileappender.h"
  30. #include <QtCore/QDateTime>
  31. /******************************************************************************
  32. * Declarations
  33. ******************************************************************************/
  34. namespace Log4Qt
  35. {
  36. /*!
  37. * \brief The class DailyRollingFileAppender extends FileAppender so that the
  38. * underlying file is rolled over at a specified frequency.
  39. *
  40. * \note All the functions declared in this class are thread-safe.
  41. *
  42. * \note The ownership and lifetime of objects of this class are managed. See
  43. * \ref Ownership "Object ownership" for more details.
  44. */
  45. class DailyRollingFileAppender : public FileAppender
  46. {
  47. Q_OBJECT
  48. /*!
  49. * The property holds the date pattern used by the appender.
  50. *
  51. * The default is DAILY_ROLLOVER for rollover at midnight each day.
  52. *
  53. * \sa datePattern(), setDatePattern()
  54. */
  55. Q_PROPERTY(QString datePattern READ datePattern WRITE setDatePattern)
  56. public:
  57. /*!
  58. * The enum DatePattern defines constants for date patterns.
  59. *
  60. * \sa setDatePattern(DatePattern)
  61. */
  62. enum DatePattern
  63. {
  64. /*! The minutely date pattern string is "'.'yyyy-MM-dd-hh-mm". */
  65. MINUTELY_ROLLOVER = 0,
  66. /*! The hourly date pattern string is "'.'yyyy-MM-dd-hh". */
  67. HOURLY_ROLLOVER,
  68. /*! The half-daily date pattern string is "'.'yyyy-MM-dd-a". */
  69. HALFDAILY_ROLLOVER,
  70. /*! The daily date pattern string is "'.'yyyy-MM-dd". */
  71. DAILY_ROLLOVER,
  72. /*! The weekly date pattern string is "'.'yyyy-ww". */
  73. WEEKLY_ROLLOVER,
  74. /*! The monthly date pattern string is "'.'yyyy-MM". */
  75. MONTHLY_ROLLOVER
  76. };
  77. Q_ENUMS(DatePattern)
  78. DailyRollingFileAppender(QObject *pParent = 0);
  79. DailyRollingFileAppender(Layout *pLayout,
  80. const QString &rFileName,
  81. const QString &rDatePattern,
  82. QObject *pParent = 0);
  83. virtual ~DailyRollingFileAppender();
  84. private:
  85. DailyRollingFileAppender(const DailyRollingFileAppender &rOther); // Not implemented
  86. DailyRollingFileAppender &operator=(const DailyRollingFileAppender &rOther); // Not implemented
  87. public:
  88. QString datePattern() const;
  89. /*!
  90. * Sets the datePattern to the value specified by the \a datePattern
  91. * constant.
  92. */
  93. void setDatePattern(DatePattern datePattern);
  94. void setDatePattern(const QString &rDatePattern);
  95. virtual void activateOptions();
  96. protected:
  97. virtual void append(const LoggingEvent &rEvent);
  98. /*!
  99. * Tests if all entry conditions for using append() in this class are
  100. * met.
  101. *
  102. * If a conditions is not met, an error is logged and the function
  103. * returns false. Otherwise the result of
  104. * FileAppender::checkEntryConditions() is returned.
  105. *
  106. * The checked conditions are:
  107. * - A valid pattern has been set (APPENDER_USE_INVALID_PATTERN_ERROR)
  108. *
  109. * The function is called as part of the checkEntryConditions() chain
  110. * started by AppenderSkeleton::doAppend().
  111. *
  112. * \sa AppenderSkeleton::doAppend(),
  113. * AppenderSkeleton::checkEntryConditions()
  114. */
  115. virtual bool checkEntryConditions() const;
  116. protected:
  117. #ifndef QT_NO_DEBUG_STREAM
  118. /*!
  119. * Writes all object member variables to the given debug stream
  120. * \a rDebug and returns the stream.
  121. *
  122. * <tt>
  123. * %DailyRollingFileAppender(name:"DRFA" activedatepattern:"'.'yyyy-MM-dd-hh-mm"
  124. * appendfile:false bufferedio:true
  125. * datepattern:"'.'yyyy-MM-dd-hh-mm"
  126. * encoding:"" frequency:"MINUTELY_ROLLOVER"
  127. * file:"/log.txt" filter:0x0 immediateflush:true
  128. * isactive:true isclosed:false layout:"TTCC"
  129. * referencecount:1
  130. * rollovertime:QDateTime("Mon Oct 22 05:23:00 2007")
  131. * threshold: "NULL" writer: 0x0 )
  132. * </tt>
  133. * \sa QDebug, operator<<(QDebug debug, const LogObject &rLogObject)
  134. */
  135. virtual QDebug debug(QDebug &rDebug) const;
  136. #endif // QT_NO_DEBUG_STREAM
  137. private:
  138. void computeFrequency();
  139. void computeRollOverTime();
  140. QString frequencyToString() const;
  141. void rollOver();
  142. private:
  143. QString mDatePattern;
  144. DatePattern mFrequency;
  145. QString mActiveDatePattern;
  146. QDateTime mRollOverTime;
  147. QString mRollOverSuffix;
  148. };
  149. /**************************************************************************
  150. * Operators, Helper
  151. **************************************************************************/
  152. /**************************************************************************
  153. * Inline
  154. **************************************************************************/
  155. inline QString DailyRollingFileAppender::datePattern() const
  156. { QMutexLocker locker(&mObjectGuard);
  157. return mDatePattern; }
  158. inline void DailyRollingFileAppender::setDatePattern(const QString &rDatePattern)
  159. { QMutexLocker locker(&mObjectGuard);
  160. mDatePattern = rDatePattern; }
  161. } // namespace Log4Qt
  162. // Q_DECLARE_TYPEINFO(Log4Qt::DailyRollingFileAppender, Q_COMPLEX_TYPE); // Use default
  163. #endif // LOG4QT_DAILYROLLINGFILEAPPENDER_H