Mailcommon

filterlog.cpp
1/*
2 SPDX-FileCopyrightText: 2003 Andreas Gungl <a.gungl@gmx.de>
3
4 SPDX-License-Identifier: GPL-2.0-only
5*/
6
7#include "filterlog.h"
8
9#include "mailcommon_debug.h"
10
11#include <QFile>
12#include <QTime>
13
14#include <sys/stat.h>
15
16using namespace MailCommon;
17
18class Q_DECL_HIDDEN FilterLog::FilterLogPrivate
19{
20public:
21 FilterLogPrivate(FilterLog *qq)
22 : q(qq)
23 , mMaxLogSize(512 * 1024)
24 , mAllowedTypes(FilterLog::Meta | FilterLog::PatternDescription | FilterLog::RuleResult | FilterLog::PatternResult | FilterLog::AppliedAction)
25 {
26 }
27
28 static FilterLog *mSelf;
29
30 FilterLog *const q;
31 QStringList mLogEntries;
32 long mMaxLogSize;
33 long mCurrentLogSize = 0;
34 int mAllowedTypes;
35 bool mLogging = false;
36
37 void checkLogSize();
38};
39
40void FilterLog::FilterLogPrivate::checkLogSize()
41{
42 if (mCurrentLogSize > mMaxLogSize && mMaxLogSize > -1) {
43 qCDebug(MAILCOMMON_LOG) << "Filter log: memory limit reached, starting to discard old items, size =" << QString::number(mCurrentLogSize);
44
45 // avoid some kind of hysteresis, shrink the log to 90% of its maximum
46 while (mCurrentLogSize > (mMaxLogSize * 0.9)) {
47 QStringList::Iterator it = mLogEntries.begin();
48 if (it != mLogEntries.end()) {
49 mCurrentLogSize -= (*it).length();
50 mLogEntries.erase(it);
51 qCDebug(MAILCOMMON_LOG) << "Filter log: new size =" << QString::number(mCurrentLogSize);
52 } else {
53 qCDebug(MAILCOMMON_LOG) << "Filter log: size reduction disaster!";
54 q->clear();
55 }
56 }
57
59 }
60}
61
62FilterLog *FilterLog::FilterLogPrivate::mSelf = nullptr;
63
64FilterLog::FilterLog()
65 : d(new FilterLogPrivate(this))
66{
67}
68
69FilterLog::~FilterLog() = default;
70
72{
73 if (!FilterLog::FilterLogPrivate::mSelf) {
74 FilterLog::FilterLogPrivate::mSelf = new FilterLog();
75 }
76
77 return FilterLog::FilterLogPrivate::mSelf;
78}
79
81{
82 return d->mLogging;
83}
84
85void FilterLog::setLogging(bool active)
86{
87 d->mLogging = active;
89}
90
92{
93 if (size < -1) {
94 size = -1;
95 }
96
97 // do not allow less than 1 KByte except unlimited (-1)
98 if (size >= 0 && size < 1024) {
99 size = 1024;
100 }
101
102 d->mMaxLogSize = size;
104 d->checkLogSize();
105}
106
108{
109 return d->mMaxLogSize;
110}
111
112void FilterLog::setContentTypeEnabled(ContentType contentType, bool enable)
113{
114 if (enable) {
115 d->mAllowedTypes |= contentType;
116 } else {
117 d->mAllowedTypes &= ~contentType;
118 }
119
121}
122
124{
125 return d->mAllowedTypes & contentType;
126}
127
128void FilterLog::add(const QString &logEntry, ContentType contentType)
129{
130 if (isLogging() && (d->mAllowedTypes & contentType)) {
131 QString timedLog;
132 if (contentType & ~Meta) {
133 timedLog = QLatin1Char('[') + QTime::currentTime().toString() + QLatin1StringView("] ") + logEntry;
134 } else {
135 timedLog = logEntry;
136 }
137
138 d->mLogEntries.append(timedLog);
139 Q_EMIT logEntryAdded(timedLog);
140 d->mCurrentLogSize += timedLog.length();
141 d->checkLogSize();
142 }
143}
144
146{
147 add(QStringLiteral("------------------------------"), Meta);
148}
149
151{
152 d->mLogEntries.clear();
153 d->mCurrentLogSize = 0;
154}
155
157{
158 return d->mLogEntries;
159}
160
162{
163 qCDebug(MAILCOMMON_LOG) << "----- starting filter log -----";
164 for (const QString &entry : std::as_const(d->mLogEntries)) {
165 qCDebug(MAILCOMMON_LOG) << entry;
166 }
167 qCDebug(MAILCOMMON_LOG) << "------ end of filter log ------";
168}
169
170bool FilterLog::saveToFile(const QString &fileName) const
171{
172 QFile file(fileName);
173 if (!file.open(QIODevice::WriteOnly)) {
174 return false;
175 }
176
177 file.write("<html>\n<body>\n");
178 file.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n");
179 for (const QString &entry : std::as_const(d->mLogEntries)) {
180 const QString line = QLatin1StringView("<p>") + entry + QLatin1StringView("</p>") + QLatin1Char('\n');
181 file.write(line.toLocal8Bit());
182 }
183 file.write("</body>\n</html>\n");
184 file.close();
185 return true;
186}
187
189{
190 return plain.toHtmlEscaped();
191}
192
193#include "moc_filterlog.cpp"
KMail Filter Log Collector.
Definition filterlog.h:33
void logShrinked()
This signal is emitted whenever the log has shrunk.
bool saveToFile(const QString &fileName) const
Saves the log to the file with the given fileName.
void clear()
Clears the log.
bool isLogging() const
Returns whether the filter log is currently active.
Definition filterlog.cpp:80
void setMaxLogSize(long size=-1)
Sets the maximum size of the log in bytes.
Definition filterlog.cpp:91
void setContentTypeEnabled(ContentType type, bool enabled)
Sets whether a given content type will be enabled for logging.
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
ContentType
Describes the type of content that will be logged.
Definition filterlog.h:50
@ Meta
Log all meta data.
Definition filterlog.h:51
void addSeparator()
Adds a separator line to the log.
void logEntryAdded(const QString &entry)
This signal is emitted whenever a new entry has been added to the log.
void logStateChanged()
This signal is emitted whenever the activity of the filter log has been changed.
void setLogging(bool active)
Sets whether the filter log is currently active.
Definition filterlog.cpp:85
QStringList logEntries() const
Returns the list of log entries.
bool isContentTypeEnabled(ContentType type) const
Returns whether the given content type is enabled for logging.
static QString recode(const QString &plain)
Returns an escaped version of the log which can be used in a HTML document.
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition filterlog.cpp:71
~FilterLog() override
Destroys the filter log.
void dump()
Dumps the log to console.
long maxLogSize() const
Returns the maximum size of the log in bytes.
The filter dialog.
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
virtual void close() override
qint64 write(const QByteArray &data)
iterator begin()
iterator end()
iterator erase(const_iterator begin, const_iterator end)
Q_EMITQ_EMIT
QString & append(QChar ch)
qsizetype length() const const
QString number(double n, char format, int precision)
QString toHtmlEscaped() const const
QByteArray toLocal8Bit() const const
QTime currentTime()
QString toString(QStringView format) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.