Debian Codegraph @

src/RollingFileAppender.cpp

raw
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 #include <QDateTime>
14 #include <QDir>
15 #include <QFileInfo>
16
17 #include "RollingFileAppender.h"
18
19 #include "rollingfilesink_p.h"
20
21 DLOG_CORE_BEGIN_NAMESPACE
22 extern std::string loggerName(const QFile &logFile);
23 /*!
24 @~english
25 @class Dtk::Core::RollingFileAppender
26 @ingroup dlog
27 @brief The RollingFileAppender class extends FileAppender so that the underlying file is rolled over at a user chosen frequency.
28
29 The class is based on Log4Qt.DailyRollingFileAppender class (http://log4qt.sourceforge.net/)
30 and has the same date pattern format.
31
32 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,
33 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
34 until it rolls over the next day.
35
36 The logFilesLimit parameter is used to automatically delete the oldest log files in the directory during rollover
37 (so no more than logFilesLimit recent log files exist in the directory at any moment).
38 */
39
40 RollingFileAppender::RollingFileAppender(const QString &fileName)
41 : FileAppender(fileName)
42 {
43 setLogFilesLimit(1);
44 setLogSizeLimit(1024 * 1024 * 20);
45 }
46
47 void RollingFileAppender::append(const QDateTime &time, Logger::LogLevel level, const char *file, int line,
48 const char *func, const QString &category, const QString &msg)
49 {
50 FileAppender::append(time, level, file, line, func, category, msg);
51 }
52
53 RollingFileAppender::DatePattern RollingFileAppender::datePattern() const
54 {
55 QMutexLocker locker(&m_rollingMutex);
56 return m_frequency;
57 }
58
59 QString RollingFileAppender::datePatternString() const
60 {
61 QMutexLocker locker(&m_rollingMutex);
62 return m_datePatternString;
63 }
64
65 void RollingFileAppender::setDatePattern(DatePattern datePattern)
66 {
67 QMutexLocker locker(&m_rollingMutex);
68 m_frequency = datePattern;
69
70 computeRollOverTime();
71 }
72
73 void RollingFileAppender::setDatePattern(const QString & /*datePattern*/)
74 {
75 }
76
77 void RollingFileAppender::setDatePatternString(const QString & /*datePatternString*/)
78 {
79 }
80
81 void RollingFileAppender::computeFrequency()
82 {
83 }
84
85 void RollingFileAppender::removeOldFiles()
86 {
87 }
88
89 void RollingFileAppender::computeRollOverTime()
90 {
91 if (auto *fs = get_sink<rolling_file_sink_mt>(loggerName(QFile(fileName()))))
92 {
93 return fs->set_interval(RollingInterval(m_frequency));
94 }
95 }
96
97 void RollingFileAppender::rollOver()
98 {
99 }
100
101 void RollingFileAppender::setLogFilesLimit(int limit)
102 {
103 QMutexLocker locker(&m_rollingMutex);
104 m_logFilesLimit = limit;
105
106 if (auto *fs = get_sink<rolling_file_sink_mt>(loggerName(QFile(fileName()))))
107 {
108 return fs->set_max_files(std::size_t(limit));
109 }
110 }
111
112 int RollingFileAppender::logFilesLimit() const
113 {
114 QMutexLocker locker(&m_rollingMutex);
115 return m_logFilesLimit;
116 }
117
118 void RollingFileAppender::setLogSizeLimit(int limit)
119 {
120 QMutexLocker locker(&m_rollingMutex);
121 m_logSizeLimit = limit;
122
123 if (auto *fs = get_sink<rolling_file_sink_mt>(loggerName(QFile(fileName()))))
124 {
125 return fs->set_max_size(std::size_t(limit));
126 }
127 }
128
129 qint64 RollingFileAppender::logSizeLimit() const
130 {
131 QMutexLocker locker(&m_rollingMutex);
132 return m_logSizeLimit;
133 }
134
135 DLOG_CORE_END_NAMESPACE