-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
67 lines (52 loc) · 2.35 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <QApplication>
#include <QtCore>
#include <QLocale>
#include "mainwindow.h"
#include "globals.h"
#include <stdlib.h>
#include <stdio.h>
// message handler for formatting Qt log messages
void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QDateTime dateTime(QDateTime::currentDateTime());
QString timeStr(dateTime.toString("dd-MM-yyyy HH:mm:ss:zzz"));
QString contextString(QString("(%1, %2, %3): ").arg(context.file).arg(context.function).arg(context.line));
QFile outFile("runtime.log");
if( outFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
QTextStream stream(&outFile);
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
stream << timeStr << " - DEBUG - " << contextString << localMsg.constData() << endl;
break;
case QtInfoMsg:
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
stream << timeStr << " - INFO - " << contextString << localMsg.constData() << endl;
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
stream << timeStr << " - WARNING - " << contextString << localMsg.constData() << endl;
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
stream << timeStr << " - CRITICAL - " << contextString << localMsg.constData() << endl;
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
stream << timeStr << " - FATAL - " << contextString << localMsg.constData() << endl;
}
}
else
fprintf(stderr, "Unable to open log file for writing!");
}
int main(int argc, char *argv[])
{
qInfo("Application started");
QLocale::setDefault(QLocale::c());
QApplication a(argc, argv);
qInstallMessageHandler(customMessageHandler);
MainWindow w;
w.show();
return a.exec();
}