-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathQLogger.cpp
292 lines (220 loc) · 8.49 KB
/
QLogger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "QLogger.h"
#include "QLoggerWriter.h"
#include <QDateTime>
#include <QDir>
Q_DECLARE_METATYPE(QLogger::LogLevel)
Q_DECLARE_METATYPE(QLogger::LogMode)
Q_DECLARE_METATYPE(QLogger::LogFileDisplay)
Q_DECLARE_METATYPE(QLogger::LogMessageDisplay)
namespace QLogger
{
void QLog_(const QString &module, LogLevel level, const QString &message, const QString &function, const QString &file,
int line)
{
QLoggerManager::getInstance()->enqueueMessage(module, level, message, function, file, line);
}
static const int QUEUE_LIMIT = 100;
QLoggerManager *QLoggerManager::getInstance()
{
static QLoggerManager INSTANCE;
return &INSTANCE;
}
bool QLoggerManager::addDestination(const QString &fileDest, const QString &module, LogLevel level,
const QString &fileFolderDestination, LogMode mode, LogFileDisplay fileSuffixIfFull,
LogMessageDisplays messageOptions, bool notify)
{
QMutexLocker lock(&mMutex);
if (!mModuleDest.contains(module))
{
const auto log = createWriter(fileDest, level, fileFolderDestination, mode, fileSuffixIfFull, messageOptions);
mModuleDest.insert(module, log);
startWriter(module, log, mode, notify);
return true;
}
return false;
}
bool QLoggerManager::addDestination(const QString &fileDest, const QStringList &modules, LogLevel level,
const QString &fileFolderDestination, LogMode mode, LogFileDisplay fileSuffixIfFull,
LogMessageDisplays messageOptions, bool notify)
{
QMutexLocker lock(&mMutex);
bool allAdded = false;
for (const auto &module : modules)
{
if (!mModuleDest.contains(module))
{
const auto log = createWriter(fileDest, level, fileFolderDestination, mode, fileSuffixIfFull, messageOptions);
mModuleDest.insert(module, log);
startWriter(module, log, mode, notify);
allAdded = true;
}
}
return allAdded;
}
QLoggerWriter *QLoggerManager::createWriter(const QString &fileDest, LogLevel level,
const QString &fileFolderDestination, LogMode mode,
LogFileDisplay fileSuffixIfFull, LogMessageDisplays messageOptions) const
{
const auto lFileDest = fileDest.isEmpty() ? mDefaultFileDestination : fileDest;
const auto lLevel = level == LogLevel::Warning ? mDefaultLevel : level;
const auto lFileFolderDestination = fileFolderDestination.isEmpty()
? mDefaultFileDestinationFolder
: QDir::fromNativeSeparators(fileFolderDestination);
const auto lMode = mode == LogMode::OnlyFile ? mDefaultMode : mode;
const auto lFileSuffixIfFull
= fileSuffixIfFull == LogFileDisplay::DateTime ? mDefaultFileSuffixIfFull : fileSuffixIfFull;
const auto lMessageOptions
= messageOptions.testFlag(LogMessageDisplay::Default) ? mDefaultMessageOptions : messageOptions;
const auto log
= new QLoggerWriter(lFileDest, lLevel, lFileFolderDestination, lMode, lFileSuffixIfFull, lMessageOptions);
log->setMaxFileSize(mDefaultMaxFileSize);
log->stop(mIsStop);
return log;
}
void QLoggerManager::startWriter(const QString &module, QLoggerWriter *log, LogMode mode, bool notify)
{
if (notify)
{
const auto threadId = QString("%1").arg((quintptr)QThread::currentThread(), QT_POINTER_SIZE * 2, 16, QChar('0'));
log->enqueue(QDateTime::currentDateTime(), threadId, module, LogLevel::Info, "", "", -1, "Adding destination!");
}
if (mode != LogMode::Disabled)
log->start();
}
void QLoggerManager::clearFileDestinationFolder(const QString &fileFolderDestination, int days)
{
QDir dir(fileFolderDestination + QStringLiteral("/logs"));
if (!dir.exists())
return;
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
const auto list = dir.entryInfoList();
const auto now = QDateTime::currentDateTime();
for (const auto &fileInfoIter : list)
{
if (fileInfoIter.lastModified().daysTo(now) >= days)
{
// remove file
dir.remove(fileInfoIter.fileName());
}
}
}
void QLoggerManager::setDefaultFileDestinationFolder(const QString &fileDestinationFolder)
{
mDefaultFileDestinationFolder = QDir::fromNativeSeparators(fileDestinationFolder);
}
void QLoggerManager::writeAndDequeueMessages(const QString &module)
{
QMutexLocker lock(&mMutex);
const auto logWriter = mModuleDest.value(module, nullptr);
if (logWriter && !logWriter->isStop())
{
const auto values = mNonWriterQueue.values(module);
for (const auto &vals : values)
{
const auto level = qvariant_cast<LogLevel>(vals.at(2).toInt());
if (logWriter->getLevel() <= level)
{
const auto datetime = vals.at(0).toDateTime();
const auto threadId = vals.at(1).toString();
const auto function = vals.at(3).toString();
const auto file = vals.at(4).toString();
const auto line = vals.at(5).toInt();
const auto message = vals.at(6).toString();
logWriter->enqueue(datetime, threadId, module, level, function, file, line, message);
}
}
mNonWriterQueue.remove(module);
}
}
void QLoggerManager::enqueueMessage(const QString &module, LogLevel level, const QString &message,
const QString &function, const QString &file, int line)
{
QMutexLocker lock(&mMutex);
const auto logWriter = mModuleDest.value(module, nullptr);
const auto isLogEnabled = logWriter && logWriter->getMode() != LogMode::Disabled && !logWriter->isStop();
if (isLogEnabled && logWriter->getLevel() <= level)
{
const auto threadId = QString("%1").arg((quintptr)QThread::currentThread(), QT_POINTER_SIZE * 2, 16, QChar('0'));
const auto fileName = file.mid(file.lastIndexOf('/') + 1);
writeAndDequeueMessages(module);
logWriter->enqueue(QDateTime::currentDateTime(), threadId, module, level, function, fileName, line, message);
}
else if (!logWriter && mNonWriterQueue.count(module) < QUEUE_LIMIT)
{
const auto threadId = QString("%1").arg((quintptr)QThread::currentThread(), QT_POINTER_SIZE * 2, 16, QChar('0'));
const auto fileName = file.mid(file.lastIndexOf('/') + 1);
mNonWriterQueue.insert(module,
{ QDateTime::currentDateTime(), threadId, QVariant::fromValue<LogLevel>(level), function,
fileName, line, message });
}
}
void QLoggerManager::pause()
{
QMutexLocker lock(&mMutex);
mIsStop = true;
for (auto &logWriter : mModuleDest)
logWriter->stop(mIsStop);
}
void QLoggerManager::resume()
{
QMutexLocker lock(&mMutex);
mIsStop = false;
for (auto &logWriter : mModuleDest)
logWriter->stop(mIsStop);
}
void QLoggerManager::overwriteLogMode(LogMode mode)
{
QMutexLocker lock(&mMutex);
setDefaultMode(mode);
for (auto &logWriter : mModuleDest)
logWriter->setLogMode(mode);
}
void QLoggerManager::overwriteLogLevel(LogLevel level)
{
QMutexLocker lock(&mMutex);
setDefaultLevel(level);
for (auto &logWriter : mModuleDest)
logWriter->setLogLevel(level);
}
void QLoggerManager::overwriteMaxFileSize(int maxSize)
{
QMutexLocker lock(&mMutex);
setDefaultMaxFileSize(maxSize);
for (auto &logWriter : mModuleDest)
logWriter->setMaxFileSize(maxSize);
}
QLoggerManager::~QLoggerManager()
{
QMutexLocker locker(&mMutex);
for (const auto &dest : mModuleDest.toStdMap())
writeAndDequeueMessages(dest.first);
QVector<QString> oldFiles;
for (auto dest : std::as_const(mModuleDest))
{
dest->closeDestination();
dest->wait();
oldFiles.append(dest->getFileDestinationFolder());
}
for (auto dest : std::as_const(mModuleDest))
delete dest;
mModuleDest.clear();
if (!mNewLogsFolder.isEmpty() && mNewLogsFolder != mDefaultFileDestinationFolder)
{
for (const auto &oldDestination : oldFiles)
{
QDir dir(oldDestination);
auto entryList = dir.entryList(QDir::Files | QDir::System | QDir::Hidden);
for (const auto &filePath : std::as_const(entryList))
{
auto destination = mNewLogsFolder;
if (!destination.endsWith("/"))
destination.append("/");
destination.append(filePath.split("/").last());
QDir().rename(oldDestination + "/" + filePath, destination);
if (dir.isEmpty())
dir.removeRecursively();
}
}
}
}
}