rollingfileappender.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: rollingfileappender.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_ROLINGFILEAPPENDER_H
  25. #define LOG4QT_ROLINGFILEAPPENDER_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include "log4qt/fileappender.h"
  30. /******************************************************************************
  31. * Declarations
  32. ******************************************************************************/
  33. namespace Log4Qt
  34. {
  35. /*!
  36. * \brief The class RollingFileAppender extends FileAppender to backup
  37. * the log files when they reach a certain size.
  38. *
  39. * \note All the functions declared in this class are thread-safe.
  40. *
  41. * \note The ownership and lifetime of objects of this class are managed.
  42. * See \ref Ownership "Object ownership" for more details.
  43. */
  44. class RollingFileAppender : public FileAppender
  45. {
  46. Q_OBJECT
  47. /*!
  48. * The property holds the maximum backup count used by the appender.
  49. *
  50. * The default is 1.
  51. *
  52. * \sa maxBackupIndex(), setMaxBackupIndex()
  53. */
  54. Q_PROPERTY(int maxBackupIndex READ maxBackupIndex WRITE setMaxBackupIndex)
  55. /*!
  56. * The property holds the maximum file size used by the appender.
  57. *
  58. * The default is 10 MB (10 * 1024 * 1024).
  59. *
  60. * \sa maximumFileSize(), setMaximumFileSize()
  61. */
  62. Q_PROPERTY(qint64 maximumFileSize READ maximumFileSize WRITE setMaximumFileSize)
  63. /*!
  64. * The property sets the maximum file size from a string value.
  65. *
  66. * \sa setMaxFileSize(), maximumFileSize()
  67. */
  68. Q_PROPERTY(QString maxFileSize WRITE setMaxFileSize)
  69. public:
  70. RollingFileAppender(QObject *pParent = 0);
  71. RollingFileAppender(Layout *pLayout,
  72. const QString &rFileName,
  73. QObject *pParent = 0);
  74. RollingFileAppender(Layout *pLayout,
  75. const QString &rFileName,
  76. bool append,
  77. QObject *pParent = 0);
  78. virtual ~RollingFileAppender();
  79. private:
  80. RollingFileAppender(const RollingFileAppender &rOther); // Not implemented
  81. RollingFileAppender &operator=(const RollingFileAppender &rOther); // Not implemented
  82. public:
  83. int maxBackupIndex() const;
  84. qint64 maximumFileSize() const;
  85. void setMaxBackupIndex(int maxBackupIndex);
  86. void setMaximumFileSize(qint64 maximumFileSize);
  87. void setMaxFileSize(const QString &rMaxFileSize);
  88. protected:
  89. virtual void append(const LoggingEvent &rEvent);
  90. #ifndef QT_NO_DEBUG_STREAM
  91. /*!
  92. * Writes all object member variables to the given debug stream
  93. * \a rDebug and returns the stream.
  94. *
  95. * <tt>
  96. * %RollingFileAppender(name:"RFA" appendfile:false bufferedio:true
  97. * encoding:"" file:"/log.txt" filter: 0x0
  98. * immediateflush:true isactive:true
  99. * isclosed:false layout:"TTCC" maxbackupindex:2
  100. * maximumfilesize:40 referencecount:1
  101. * threshold:"NULL" writer:0x4175af8)
  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 rollOver();
  109. private:
  110. int mMaxBackupIndex;
  111. qint64 mMaximumFileSize;
  112. };
  113. /**************************************************************************
  114. * Operators, Helper
  115. **************************************************************************/
  116. /**************************************************************************
  117. * Inline
  118. **************************************************************************/
  119. inline int RollingFileAppender::maxBackupIndex() const
  120. { QMutexLocker locker(&mObjectGuard);
  121. return mMaxBackupIndex; }
  122. inline qint64 RollingFileAppender::maximumFileSize() const
  123. { QMutexLocker locker(&mObjectGuard);
  124. return mMaximumFileSize; }
  125. inline void RollingFileAppender::setMaxBackupIndex(int maxBackupIndex)
  126. { QMutexLocker locker(&mObjectGuard);
  127. mMaxBackupIndex = maxBackupIndex; }
  128. inline void RollingFileAppender::setMaximumFileSize(qint64 maximumFileSize)
  129. { QMutexLocker locker(&mObjectGuard);
  130. mMaximumFileSize = maximumFileSize; }
  131. } // namespace Log4Qt
  132. // Q_DECLARE_TYPEINFO(Log4Qt::RollingFileAppender, Q_COMPLEX_TYPE); // Use default
  133. #endif // LOG4QT_ROLINGFILEAPPENDER_H