forked from francescmaestre/QLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQLogger.cpp
More file actions
272 lines (218 loc) · 7.1 KB
/
QLogger.cpp
File metadata and controls
272 lines (218 loc) · 7.1 KB
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
#include "QLogger.h"
#include <QDateTime>
#include <QDir>
#include <QTextStream>
#include <QVector>
/****************************************************************************************
** QLogger is a library to register and print logs into a file.
** Copyright (C) 2018 Francesc Martinez <es.linkedin.com/in/cescmm/en>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
***************************************************************************************/
namespace QLogger
{
void QLog_Trace(const QString &module, const QString &message)
{
QLog_(module, LogLevel::Trace, message);
}
void QLog_Debug(const QString &module, const QString &message)
{
QLog_(module, LogLevel::Debug, message);
}
void QLog_Info(const QString &module, const QString &message)
{
QLog_(module, LogLevel::Info, message);
}
void QLog_Warning(const QString &module, const QString &message)
{
QLog_(module, LogLevel::Warning, message);
}
void QLog_Error(const QString &module, const QString &message)
{
QLog_(module, LogLevel::Error, message);
}
void QLog_Fatal(const QString &module, const QString &message)
{
QLog_(module, LogLevel::Fatal, message);
}
void QLog_(const QString &module, LogLevel level, const QString &message)
{
const auto manager = QLoggerManager::getInstance();
QMutexLocker(&manager->mutex);
const auto logWriter = manager->getLogWriter(module);
if (logWriter && !logWriter->isStop() && logWriter->getLevel() <= level)
{
manager->writeAndDequeueMessages(module);
logWriter->write(module, message, level);
}
else if (!logWriter)
manager->queueMessage(
module,
{ message, static_cast<int>(level), QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz") });
}
static const int QUEUE_LIMIT = 100;
// QLoggerManager
QLoggerManager *QLoggerManager::INSTANCE = nullptr;
bool QLoggerManager::mIsStop = false;
QLoggerManager::QLoggerManager()
: QThread()
, mutex(QMutex::Recursive)
{
start();
QDir dir(QDir::currentPath());
if (!dir.exists("logs"))
dir.mkdir("logs");
}
QLoggerManager *QLoggerManager::getInstance()
{
if (!INSTANCE)
INSTANCE = new QLoggerManager();
return INSTANCE;
}
QString QLoggerManager::levelToText(const LogLevel &level)
{
switch (level)
{
case LogLevel::Trace:
return "Trace";
case LogLevel::Debug:
return "Debug";
case LogLevel::Info:
return "Info";
case LogLevel::Warning:
return "Warning";
case LogLevel::Error:
return "Error";
case LogLevel::Fatal:
return "Fatal";
}
return QString();
}
bool QLoggerManager::addDestination(const QString &fileDest, const QString &module, LogLevel level)
{
if (!moduleDest.contains(module))
{
const auto log = new QLoggerWriter(fileDest, level);
log->stop(mIsStop);
moduleDest.insert(module, log);
return true;
}
return false;
}
bool QLoggerManager::addDestination(const QString &fileDest, const QStringList &modules, LogLevel level)
{
bool allAdded = false;
for (const auto &module : modules)
{
if (!moduleDest.contains(module))
{
const auto log = new QLoggerWriter(fileDest, level);
log->stop(mIsStop);
moduleDest.insert(module, log);
allAdded = true;
}
}
return allAdded;
}
void QLoggerManager::queueMessage(const QString module, const QVector<QVariant> &logData)
{
if (mNonWriterQueue.count(module) < QUEUE_LIMIT)
mNonWriterQueue.insert(module, logData);
}
void QLoggerManager::writeAndDequeueMessages(const QString &module)
{
auto element = mNonWriterQueue.find(module);
const auto queueEnd = mNonWriterQueue.end();
const auto logWriter = getLogWriter(module);
if (element != queueEnd && logWriter && !logWriter->isStop())
{
const auto module = element.key();
for (; element != queueEnd; ++element)
{
const auto message = element.value().at(0).toString();
const auto level = static_cast<LogLevel>(element.value().at(1).toInt());
const auto dt = element.value().at(2).toString();
if (logWriter->getLevel() <= level)
logWriter->write(module, message, level, dt);
}
mNonWriterQueue.remove(module);
}
}
void QLoggerManager::closeLogger()
{
deleteLater();
exit(0);
}
void QLoggerManager::pause()
{
mIsStop = true;
for (auto &logWriter : moduleDest)
logWriter->stop(mIsStop);
}
void QLoggerManager::resume()
{
mIsStop = false;
for (auto &logWriter : moduleDest)
logWriter->stop(mIsStop);
}
void QLoggerManager::overwriteLogLevel(LogLevel level)
{
for (auto &logWriter : moduleDest)
logWriter->setLogLevel(level);
}
QLoggerWriter::QLoggerWriter(const QString &fileDestination, LogLevel level)
{
mFileDestination = "logs/" + fileDestination;
mLevel = level;
}
QString QLoggerWriter::renameFileIfFull()
{
const auto MAX_SIZE = 1024 * 1024;
const auto toRemove = mFileDestination.section('.', -1);
const auto fileNameAux = mFileDestination.left(mFileDestination.size() - toRemove.size() - 1);
auto renamed = false;
auto newName = QString("%1%2").arg(fileNameAux, "_%1__%2.log");
QFile file(mFileDestination);
// Rename file if it's full
if (file.size() >= MAX_SIZE)
{
const auto currentTime = QDateTime::currentDateTime();
newName = newName.arg(currentTime.date().toString("dd_MM_yy"), currentTime.time().toString("hh_mm_ss"));
renamed = file.rename(mFileDestination, newName);
}
return renamed ? newName : QString();
}
void QLoggerWriter::write(const QString &module, const QString &message, const LogLevel &messageLogLevel)
{
const auto dtFormat = QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm:ss.zzz");
write(module, message, messageLogLevel, dtFormat);
}
void QLoggerWriter::write(const QString &module, const QString &message, const LogLevel &messageLogLevel,
const QString &dt)
{
QFile file(mFileDestination);
const auto newName = renameFileIfFull();
if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
{
QTextStream out(&file);
if (!newName.isEmpty())
out << QString("%1 - Previous log %2\n").arg(dt, newName);
const auto logLevel = QLoggerManager::levelToText(messageLogLevel);
const auto text = QString("[%1] [%2] {%3} %4\n").arg(dt, logLevel, module, message);
out << text;
file.close();
}
}
}