-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
51 lines (48 loc) · 1.34 KB
/
main.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
#include "mainwindow.h"
#include <QApplication>
#include <QMutex>
#include <QDateTime>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
QMutex mutex;
void MessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString log_type;
mutex.lock();
switch (type) {
case QtDebugMsg:
log_type="[Debug] : ";
break;
case QtInfoMsg:
log_type="[Info] : ";
break;
case QtWarningMsg:
log_type="[Warning] : ";
break;
case QtCriticalMsg:
log_type="[Critical] : ";
break;
case QtFatalMsg:
log_type="[Fatal] : ";
break;
}
QDateTime dateTime(QDateTime::currentDateTime());
QString timeStr(dateTime.toString("dd-MM-yyyy HH:mm:ss:zzz"));
QString contextString(QString("(%1, %2)").arg(context.file).arg(context.line));
QFile out_file("logfile");
out_file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream stream(&out_file);
stream << log_type << " " << timeStr << " " << contextString << ": " << msg << endl;
out_file.flush();
out_file.close();
mutex.unlock();
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(MessageHandler);
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}