-
Notifications
You must be signed in to change notification settings - Fork 2
/
qalogger.cpp
67 lines (52 loc) · 1.87 KB
/
qalogger.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
/*
* Copyright (C) 2024-2024 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "qalogger.h"
#include "params.h"
#include <QFile>
namespace QuasarAppUtils {
#define MESSAGE_PATTERN \
"[%{time MM-dd h:mm:ss.zzz} %{threadid} " \
"%{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] " \
"%{message}"
bool checkLogType(QtMsgType type, VerboseLvl lvl) {
switch (type) {
case QtDebugMsg: return lvl >= Debug;
case QtInfoMsg: return lvl >= Info;
case QtWarningMsg: return lvl >= Warning;
case QtCriticalMsg:
case QtFatalMsg:
return true;
}
return true;
}
void messageHandler(QtMsgType type, const QMessageLogContext &, const QString &msg) {
if (Params::isEndable("fileLog")) {
auto verboselvl = Params::getVerboseLvl();
if (checkLogType(type, verboselvl)) {
QString path = Params::getCurrentExecutable() + ".log";
auto file = Params::getArg("fileLog");
if (file.size()) {
path = file;
}
QFile logFile(path);
if (logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
QTextStream stream(&logFile);
#if QT_VERSION > QT_VERSION_CHECK(5, 14, 0)
stream << msg << Qt::endl;
#else
stream << msg << endl;
#endif
logFile.close();
}
}
}
}
void QALogger::init() {
qSetMessagePattern(MESSAGE_PATTERN);
qInstallMessageHandler(messageHandler);
}
}