-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
49 lines (40 loc) · 1.29 KB
/
Copy pathlogger.cpp
File metadata and controls
49 lines (40 loc) · 1.29 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
#include "logger.h"
QFile Logger::logFile;
QMutex Logger::mutex;
void Logger::install(const QString &filePath)
{
logFile.setFileName(filePath);
if(!logFile.open(QIODevice::Append | QIODevice::Text))
qWarning() << "could not open logFile.";
qInstallMessageHandler(Logger::messageHandler);
}
void Logger::messageHandler(QtMsgType type, const QMessageLogContext &ctx, const QString &msg)
{
QMutexLocker locker(&mutex);
if(!logFile.isOpen())
{
qWarning() << "could not open logFile to write log message..";
return;
}
QString level;
switch(type)
{
case QtDebugMsg: level="DEBUG"; break;
case QtInfoMsg: level="INFO"; break;
case QtWarningMsg: level="WARNING"; break;
case QtCriticalMsg: level="CRITICAL"; break;
case QtFatalMsg: level="FATAL"; break;
default: level="Unknown QtMsg";
}
QString line = QString("[%1] [%2] (%3:%4): %5\n")
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"))
.arg(level)
.arg(ctx.file ? ctx.file : "")
.arg(ctx.line)
.arg(msg);
QTextStream out(&logFile);
out << line;
out.flush();
if(type== QtFatalMsg)
abort();
}