Mailcommon

filterlog.cpp
1 /*
2  SPDX-FileCopyrightText: 2003 Andreas Gungl <[email protected]>
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 
16 using namespace MailCommon;
17 
18 class Q_DECL_HIDDEN FilterLog::FilterLogPrivate
19 {
20 public:
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 
40 void 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 
58  Q_EMIT q->logShrinked();
59  }
60 }
61 
62 FilterLog *FilterLog::FilterLogPrivate::mSelf = nullptr;
63 
64 FilterLog::FilterLog()
65  : d(new FilterLogPrivate(this))
66 {
67 }
68 
69 FilterLog::~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 
85 void FilterLog::setLogging(bool active)
86 {
87  d->mLogging = active;
89 }
90 
91 void FilterLog::setMaxLogSize(long size)
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 
112 void 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 
128 void 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() + QLatin1String("] ") + 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 
170 bool 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 = QLatin1String("<p>") + entry + QLatin1String("</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"
QString number(int n, int base)
QString toHtmlEscaped() const const
Q_EMITQ_EMIT
virtual bool open(QIODevice::OpenMode mode) override
static QString recode(const QString &plain)
Returns an escaped version of the log which can be used in a HTML document.
Definition: filterlog.cpp:188
KMail Filter Log Collector.
Definition: filterlog.h:32
void setMaxLogSize(long size=-1)
Sets the maximum size of the log in bytes.
Definition: filterlog.cpp:91
QTime currentTime()
~FilterLog() override
Destroys the filter log.
long maxLogSize() const
Returns the maximum size of the log in bytes.
Definition: filterlog.cpp:107
void setLogging(bool active)
Sets whether the filter log is currently active.
Definition: filterlog.cpp:85
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition: filterlog.cpp:71
void addSeparator()
Adds a separator line to the log.
Definition: filterlog.cpp:145
int length() const const
void logStateChanged()
This signal is emitted whenever the activity of the filter log has been changed.
@ Meta
Log all meta data.
Definition: filterlog.h:51
virtual void close() override
typedef Iterator
bool isContentTypeEnabled(ContentType type) const
Returns whether the given content type is enabled for logging.
Definition: filterlog.cpp:123
bool isLogging() const
Returns whether the filter log is currently active.
Definition: filterlog.cpp:80
void logEntryAdded(const QString &entry)
This signal is emitted whenever a new entry has been added to the log.
void clear()
Clears the log.
Definition: filterlog.cpp:150
QString toString(Qt::DateFormat format) const const
void setContentTypeEnabled(ContentType type, bool enabled)
Sets whether a given content type will be enabled for logging.
Definition: filterlog.cpp:112
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
Definition: filterlog.cpp:128
QByteArray toLocal8Bit() const const
ContentType
Describes the type of content that will be logged.
Definition: filterlog.h:50
bool saveToFile(const QString &fileName) const
Saves the log to the file with the given fileName.
Definition: filterlog.cpp:170
void dump()
Dumps the log to console.
Definition: filterlog.cpp:161
QString & append(QChar ch)
QStringList logEntries() const
Returns the list of log entries.
Definition: filterlog.cpp:156
qint64 write(const char *data, qint64 maxSize)
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Nov 28 2023 03:59:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.