rollingfileappender.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: rollingfileappender.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/rollingfileappender.h"
  28. #include <QtCore/QDebug>
  29. #include <QtCore/QFile>
  30. #include <QtCore/QTextCodec>
  31. #include "log4qt/helpers/optionconverter.h"
  32. #include "log4qt/layout.h"
  33. #include "log4qt/loggingevent.h"
  34. namespace Log4Qt
  35. {
  36. /************************************************************************
  37. Declarations
  38. *************************************************************************/
  39. /************************************************************************
  40. C helper functions
  41. *************************************************************************/
  42. /************************************************************************
  43. Class implementation: RollingFileAppender
  44. *************************************************************************/
  45. RollingFileAppender::RollingFileAppender(QObject *pParent) :
  46. FileAppender(pParent),
  47. mMaxBackupIndex(1),
  48. mMaximumFileSize(10*1024*1024)
  49. {
  50. }
  51. RollingFileAppender::RollingFileAppender(Layout *pLayout,
  52. const QString &rFileName,
  53. QObject *pParent) :
  54. FileAppender(pLayout, rFileName, pParent),
  55. mMaxBackupIndex(1),
  56. mMaximumFileSize(10*1024*1024)
  57. {
  58. }
  59. RollingFileAppender::RollingFileAppender(Layout *pLayout,
  60. const QString &rFileName,
  61. bool append,
  62. QObject *pParent) :
  63. FileAppender(pLayout, rFileName, append, pParent),
  64. mMaxBackupIndex(1),
  65. mMaximumFileSize(10*1024*1024)
  66. {
  67. }
  68. RollingFileAppender::~RollingFileAppender()
  69. {
  70. close();
  71. }
  72. void RollingFileAppender::setMaxFileSize(const QString &rMaxFileSize)
  73. {
  74. bool ok;
  75. qint64 max_file_size = OptionConverter::toFileSize(rMaxFileSize, &ok);
  76. if (ok)
  77. setMaximumFileSize(max_file_size);
  78. }
  79. void RollingFileAppender::append(const LoggingEvent &rEvent)
  80. {
  81. // Q_ASSERT_X(, "RollingFileAppender::append()", "Lock must be held by caller")
  82. FileAppender::append(rEvent);
  83. if (writer()->device()->size() > this->mMaximumFileSize)
  84. rollOver();
  85. }
  86. void RollingFileAppender::rollOver()
  87. {
  88. // Q_ASSERT_X(, "RollingFileAppender::rollOver()", "Lock must be held by caller")
  89. logger()->debug("Rolling over with maxBackupIndex = %1", mMaxBackupIndex);
  90. closeFile();
  91. QFile f;
  92. f.setFileName(file() + QLatin1Char('.') + QString::number(mMaxBackupIndex));
  93. if (f.exists() && !removeFile(f))
  94. return;
  95. QString target_file_name;
  96. int i;
  97. for (i = mMaxBackupIndex - 1; i >=1; i--)
  98. {
  99. f.setFileName(file() + QLatin1Char('.') + QString::number(i));
  100. if (f.exists())
  101. {
  102. target_file_name = file() + QLatin1Char('.') + QString::number(i + 1);
  103. if (!renameFile(f, target_file_name))
  104. return;
  105. }
  106. }
  107. f.setFileName(file());
  108. target_file_name = file() + QLatin1String(".1");
  109. if (!renameFile(f, target_file_name))
  110. return;
  111. openFile();
  112. }
  113. #ifndef QT_NO_DEBUG_STREAM
  114. QDebug RollingFileAppender::debug(QDebug &rDebug) const
  115. {
  116. QString layout_name;
  117. if (layout())
  118. layout_name = layout()->name();
  119. QString codec_name;
  120. if (encoding())
  121. codec_name = QLatin1String(encoding()->name());
  122. rDebug.nospace() << "RollingFileAppender("
  123. << "name:" << name() << " "
  124. << "appendfile:" << appendFile() << " "
  125. << "bufferedio:" << bufferedIo() << " "
  126. << "encoding:" << codec_name << " "
  127. << "file:" << file() << " "
  128. << "filter:" << firstFilter() << " "
  129. << "immediateflush:" << immediateFlush() << " "
  130. << "isactive:" << isActive() << " "
  131. << "isclosed:" << isClosed() << " "
  132. << "layout:" << layout_name << " "
  133. << "maxbackupindex:" << maxBackupIndex() << " "
  134. << "maximumfilesize:" << maximumFileSize() << " "
  135. << "referencecount:" << referenceCount() << " "
  136. << "threshold:" << threshold().toString() << " "
  137. << "writer:" << writer()
  138. << ")";
  139. return rDebug.space();
  140. }
  141. #endif // QT_NO_DEBUG_STREAM
  142. /**************************************************************************
  143. * Implementation: Operators, Helper
  144. **************************************************************************/
  145. } // namespace Log4Qt