-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
117 lines (91 loc) · 3.73 KB
/
main.cpp
File metadata and controls
117 lines (91 loc) · 3.73 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
#include "mainwindow.h"
#include <QApplication>
#include <QMutex>
#include <QThread>
static const QStringList s_logLetters = {"D", "W", "C", "F", "I"};
static QMutex s_logMutex;
static QFile s_logFile;
#ifdef _WIN32
#include <Windows.h>
#include <DbgHelp.h>
#include <strsafe.h>
static QString s_dumpsDirPath;
int generateDump(EXCEPTION_POINTERS *pExceptionPointers) {
WCHAR szPath[MAX_PATH];
WCHAR szFileName[MAX_PATH];
DWORD dwBufferSize = MAX_PATH;
HANDLE hDumpFile;
SYSTEMTIME stLocalTime;
MINIDUMP_EXCEPTION_INFORMATION ExpParam;
GetLocalTime(&stLocalTime);
GetTempPath(dwBufferSize, szPath);
StringCchPrintf(szFileName,
MAX_PATH,
L"%s/%02d%02d%02d-%02d%02d%02d-%s.dmp",
s_dumpsDirPath.toStdWString().data(),
stLocalTime.wYear,
stLocalTime.wMonth,
stLocalTime.wDay,
stLocalTime.wHour,
stLocalTime.wMinute,
stLocalTime.wSecond,
QCoreApplication::applicationVersion().toStdWString().data());
hDumpFile = CreateFile(szFileName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
nullptr,
CREATE_ALWAYS,
0,
nullptr);
ExpParam.ThreadId = GetCurrentThreadId();
ExpParam.ExceptionPointers = pExceptionPointers;
ExpParam.ClientPointers = TRUE;
MiniDumpWriteDump(GetCurrentProcess(),
GetCurrentProcessId(),
hDumpFile,
MiniDumpWithDataSegs,
&ExpParam,
nullptr,
nullptr);
return EXCEPTION_EXECUTE_HANDLER;
}
LONG WINAPI crashHandler(EXCEPTION_POINTERS *ExceptionInfo) {
generateDump(ExceptionInfo);
return EXCEPTION_CONTINUE_SEARCH;
}
#endif
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message) {
QMutexLocker mutexLocker(&s_logMutex);
Q_UNUSED(context)
const QString dateTimeString = QDateTime::currentDateTime().toString("yy-MM-dd - hh:mm:ss.zzz");
const QString threadIdString = QString::number(reinterpret_cast<qint64>(QThread::currentThreadId())).rightJustified(5, QLatin1Char('0'));
const QString threadName = !QThread::currentThread()->objectName().isEmpty()
? QThread::currentThread()->objectName()
: QLatin1String("Qt internal ");
const QString _message = QString("%1 | %2 - %3 | %4 | %5")
.arg(dateTimeString, threadIdString, threadName, s_logLetters.at(type), message);
QTextStream logTextStream(&s_logFile);
logTextStream << _message << Qt::endl;
s_logFile.flush();
}
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
#ifdef _WIN32
s_dumpsDirPath = QString("%1/dumps").arg(qApp->applicationDirPath());
QDir().mkpath(s_dumpsDirPath);
::SetErrorMode(SEM_NOGPFAULTERRORBOX);
::SetUnhandledExceptionFilter(reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>(crashHandler));
#endif
const QString logsDirPath = QString("%1/logs").arg(qApp->applicationDirPath());
QDir().mkpath(logsDirPath);
const QString logFileName = QString("%1/%2.log").arg(logsDirPath, QDateTime::currentDateTime().toString("yyMMdd-hhmmss"));
s_logFile.setFileName(logFileName);
s_logFile.open(QFile::WriteOnly | QFile::Append | QFile::Text);
qInstallMessageHandler(messageHandler);
MainWindow w;
w.show();
const int result = a.exec();
qInstallMessageHandler(nullptr);
s_logFile.close();
return result;
}