| 1 |
/* |
| 2 |
This program is free software: you can redistribute it and/or modify |
| 3 |
it under the terms of the GNU Lesser General Public License version 2.1 |
| 4 |
as published by the Free Software Foundation and appearing in the file |
| 5 |
LICENSE.LGPL included in the packaging of this file. |
| 6 |
|
| 7 |
This program is distributed in the hope that it will be useful, |
| 8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
GNU Lesser General Public License for more details. |
| 11 |
*/ |
| 12 |
|
| 13 |
/*! |
| 14 |
* \brief The RollingFileAppender class extends FileAppender so that the underlying file is rolled over at a user chosen frequency. |
| 15 |
* |
| 16 |
* The class is based on Log4Qt.DailyRollingFileAppender class (http://log4qt.sourceforge.net/) |
| 17 |
* and has the same date pattern format. |
| 18 |
* |
| 19 |
* For example, if the fileName is set to /foo/bar and the DatePattern set to the daily rollover ('.'yyyy-MM-dd'.log'), on 2014-02-16 at midnight, |
| 20 |
* the logging file /foo/bar.log will be copied to /foo/bar.2014-02-16.log and logging for 2014-02-17 will continue in /foo/bar |
| 21 |
* until it rolls over the next day. |
| 22 |
* |
| 23 |
* The logFilesLimit parameter is used to automatically delete the oldest log files in the directory during rollover |
| 24 |
* (so no more than logFilesLimit recent log files exist in the directory at any moment). |
| 25 |
* \sa setDatePattern(DatePattern), setLogFilesLimit(int) |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef ROLLINGFILEAPPENDER_H |
| 29 |
#define ROLLINGFILEAPPENDER_H |
| 30 |
|
| 31 |
#include <QDateTime> |
| 32 |
|
| 33 |
#include "FileAppender.h" |
| 34 |
|
| 35 |
DLOG_CORE_BEGIN_NAMESPACE |
| 36 |
|
| 37 |
class LIBDLOG_SHARED_EXPORT RollingFileAppender : public FileAppender |
| 38 |
{ |
| 39 |
public: |
| 40 |
/*! |
| 41 |
@~english |
| 42 |
The enum DatePattern defines constants for date patterns. |
| 43 |
\sa setDatePattern(DatePattern) |
| 44 |
*/ |
| 45 |
enum DatePattern |
| 46 |
{ |
| 47 |
|
| 48 |
MinutelyRollover = 0, /*!<@~english The minutely date pattern string is "'.'yyyy-MM-dd-hh-mm". */ |
| 49 |
HourlyRollover, /*!<@~english The hourly date pattern string is "yyyy-MM-dd-hh". */ |
| 50 |
HalfDailyRollover, /*!<@~english The half-daily date pattern string is "'.'yyyy-MM-dd-a". */ |
| 51 |
DailyRollover, /*!<@~english The daily date pattern string is "'.'yyyy-MM-dd". */ |
| 52 |
WeeklyRollover, /*!<@~english The weekly date pattern string is "'.'yyyy-ww". */ |
| 53 |
MonthlyRollover /*!<@~english The monthly date pattern string is "'.'yyyy-MM". */ |
| 54 |
}; |
| 55 |
|
| 56 |
RollingFileAppender(const QString &fileName = QString()); |
| 57 |
|
| 58 |
DatePattern datePattern() const; |
| 59 |
void setDatePattern(DatePattern datePattern); |
| 60 |
QT_DEPRECATED_X("use setDatePattern(DatePattern)") |
| 61 |
void setDatePattern(const QString &datePattern); |
| 62 |
|
| 63 |
QT_DEPRECATED_X("use datePattern(DatePattern)") |
| 64 |
QString datePatternString() const; |
| 65 |
|
| 66 |
void setLogFilesLimit(int limit); |
| 67 |
int logFilesLimit() const; |
| 68 |
|
| 69 |
void setLogSizeLimit(int limit); |
| 70 |
qint64 logSizeLimit() const; |
| 71 |
|
| 72 |
protected: |
| 73 |
virtual void append(const QDateTime &time, Logger::LogLevel level, const char *file, int line, |
| 74 |
const char *func, const QString &category, const QString &msg); |
| 75 |
|
| 76 |
private: |
| 77 |
void rollOver(); |
| 78 |
void computeRollOverTime(); |
| 79 |
void computeFrequency(); |
| 80 |
void removeOldFiles(); |
| 81 |
void setDatePatternString(const QString &datePatternString); |
| 82 |
|
| 83 |
QString m_datePatternString; |
| 84 |
DatePattern m_frequency; |
| 85 |
|
| 86 |
QDateTime m_rollOverTime; |
| 87 |
QString m_rollOverSuffix; |
| 88 |
int m_logFilesLimit; |
| 89 |
qint64 m_logSizeLimit; |
| 90 |
mutable QMutex m_rollingMutex; |
| 91 |
}; |
| 92 |
|
| 93 |
DLOG_CORE_END_NAMESPACE |
| 94 |
|
| 95 |
#endif // ROLLINGFILEAPPENDER_H |