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 #include "messagecomposer/util.h"
11 
12 #include <QFile>
13 #include <QTime>
14 
15 #include <sys/stat.h>
16 
17 using namespace MailCommon;
18 
19 class Q_DECL_HIDDEN FilterLog::FilterLogPrivate
20 {
21 public:
22  FilterLogPrivate(FilterLog *qq)
23  : q(qq)
24  , mMaxLogSize(512 * 1024)
25  , mAllowedTypes(FilterLog::Meta | FilterLog::PatternDescription | FilterLog::RuleResult | FilterLog::PatternResult | FilterLog::AppliedAction)
26  {
27  }
28 
29  static FilterLog *mSelf;
30 
31  FilterLog *const q;
32  QStringList mLogEntries;
33  long mMaxLogSize;
34  long mCurrentLogSize = 0;
35  int mAllowedTypes;
36  bool mLogging = false;
37 
38  void checkLogSize();
39 };
40 
41 void FilterLog::FilterLogPrivate::checkLogSize()
42 {
43  if (mCurrentLogSize > mMaxLogSize && mMaxLogSize > -1) {
44  qCDebug(MAILCOMMON_LOG) << "Filter log: memory limit reached, starting to discard old items, size =" << QString::number(mCurrentLogSize);
45 
46  // avoid some kind of hysteresis, shrink the log to 90% of its maximum
47  while (mCurrentLogSize > (mMaxLogSize * 0.9)) {
48  QStringList::Iterator it = mLogEntries.begin();
49  if (it != mLogEntries.end()) {
50  mCurrentLogSize -= (*it).length();
51  mLogEntries.erase(it);
52  qCDebug(MAILCOMMON_LOG) << "Filter log: new size =" << QString::number(mCurrentLogSize);
53  } else {
54  qCDebug(MAILCOMMON_LOG) << "Filter log: size reduction disaster!";
55  q->clear();
56  }
57  }
58 
59  Q_EMIT q->logShrinked();
60  }
61 }
62 
63 FilterLog *FilterLog::FilterLogPrivate::mSelf = nullptr;
64 
65 FilterLog::FilterLog()
66  : d(new FilterLogPrivate(this))
67 {
68 }
69 
70 FilterLog::~FilterLog() = default;
71 
73 {
74  if (!FilterLog::FilterLogPrivate::mSelf) {
75  FilterLog::FilterLogPrivate::mSelf = new FilterLog();
76  }
77 
78  return FilterLog::FilterLogPrivate::mSelf;
79 }
80 
82 {
83  return d->mLogging;
84 }
85 
86 void FilterLog::setLogging(bool active)
87 {
88  d->mLogging = active;
90 }
91 
92 void FilterLog::setMaxLogSize(long size)
93 {
94  if (size < -1) {
95  size = -1;
96  }
97 
98  // do not allow less than 1 KByte except unlimited (-1)
99  if (size >= 0 && size < 1024) {
100  size = 1024;
101  }
102 
103  d->mMaxLogSize = size;
105  d->checkLogSize();
106 }
107 
109 {
110  return d->mMaxLogSize;
111 }
112 
113 void FilterLog::setContentTypeEnabled(ContentType contentType, bool enable)
114 {
115  if (enable) {
116  d->mAllowedTypes |= contentType;
117  } else {
118  d->mAllowedTypes &= ~contentType;
119  }
120 
122 }
123 
125 {
126  return d->mAllowedTypes & contentType;
127 }
128 
129 void FilterLog::add(const QString &logEntry, ContentType contentType)
130 {
131  if (isLogging() && (d->mAllowedTypes & contentType)) {
132  QString timedLog;
133  if (contentType & ~Meta) {
134  timedLog = QLatin1Char('[') + QTime::currentTime().toString() + QLatin1String("] ") + logEntry;
135  } else {
136  timedLog = logEntry;
137  }
138 
139  d->mLogEntries.append(timedLog);
140  Q_EMIT logEntryAdded(timedLog);
141  d->mCurrentLogSize += timedLog.length();
142  d->checkLogSize();
143  }
144 }
145 
147 {
148  add(QStringLiteral("------------------------------"), Meta);
149 }
150 
152 {
153  d->mLogEntries.clear();
154  d->mCurrentLogSize = 0;
155 }
156 
158 {
159  return d->mLogEntries;
160 }
161 
163 {
164  qCDebug(MAILCOMMON_LOG) << "----- starting filter log -----";
165  for (const QString &entry : std::as_const(d->mLogEntries)) {
166  qCDebug(MAILCOMMON_LOG) << entry;
167  }
168  qCDebug(MAILCOMMON_LOG) << "------ end of filter log ------";
169 }
170 
171 bool FilterLog::saveToFile(const QString &fileName) const
172 {
173  QFile file(fileName);
174  if (!file.open(QIODevice::WriteOnly)) {
175  return false;
176  }
177 
178  file.write("<html>\n<body>\n");
179  file.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n");
180  for (const QString &entry : std::as_const(d->mLogEntries)) {
181  const QString line = QLatin1String("<p>") + entry + QLatin1String("</p>") + QLatin1Char('\n');
182  file.write(line.toLocal8Bit());
183  }
184  file.write("</body>\n</html>\n");
185  file.close();
186  return true;
187 }
188 
190 {
191  return plain.toHtmlEscaped();
192 }
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:189
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:92
QTime currentTime()
~FilterLog() override
Destroys the filter log.
long maxLogSize() const
Returns the maximum size of the log in bytes.
Definition: filterlog.cpp:108
void setLogging(bool active)
Sets whether the filter log is currently active.
Definition: filterlog.cpp:86
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition: filterlog.cpp:72
void addSeparator()
Adds a separator line to the log.
Definition: filterlog.cpp:146
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:124
bool isLogging() const
Returns whether the filter log is currently active.
Definition: filterlog.cpp:81
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:151
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:113
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
Definition: filterlog.cpp:129
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:171
void dump()
Dumps the log to console.
Definition: filterlog.cpp:162
QString & append(QChar ch)
QStringList logEntries() const
Returns the list of log entries.
Definition: filterlog.cpp:157
qint64 write(const char *data, qint64 maxSize)
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.