-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
134 lines (112 loc) · 4.68 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "mainwindow.h"
#include "application.h"
#include "stacktrace.h"
#include <opencv2/opencv.hpp>
MainWindow *w = NULL;
Application *app = NULL;
FILE *logFh = NULL;
void messageLogger(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString formattedMsg = qFormatLogMessage(type, context, msg);
#if defined(Q_OS_MAC)
// Odd bug in Qt, best to make it a debug message so it doesnt annoy the user
if (type == QtWarningMsg && msg.contains(QStringLiteral("Error receiving trust for a CA certificate")))
{
type = QtDebugMsg;
}
#endif
// Open the log file and write the formatted log message
QTextStream out(logFh, QIODevice::WriteOnly);
out << formattedMsg << endl;
out.flush(); // Flush so that the changes are seen immediately
switch (type)
{
case QtDebugMsg:
std::cout << formattedMsg.toStdString() << std::endl;
break;
case QtInfoMsg:
std::cout << formattedMsg.toStdString() << std::endl;
break;
// For warning, critical, and fatal errors, show a message box to the user
// Note: Displaying a message box before the window is initialized causes it to crash
case QtWarningMsg:
std::cerr << formattedMsg.toStdString() << std::endl;
if (w)
QMessageBox::warning(w, QObject::tr("%1:%2").arg(context.function).arg(context.line), msg, QMessageBox::Ok);
break;
case QtCriticalMsg:
std::cerr << formattedMsg.toStdString() << std::endl;
if (w)
QMessageBox::critical(w, QObject::tr("%1:%2").arg(context.function).arg(context.line), msg, QMessageBox::Ok);
break;
case QtFatalMsg:
std::cerr << formattedMsg.toStdString() << std::endl;
if (w)
QMessageBox::critical(w, QObject::tr("%1:%2").arg(context.function).arg(context.line), msg, QMessageBox::Ok);
abort();
}
}
int main(int argc, char *argv[])
{
int ret = -1;
try
{
// Open the log file that will record all information sent to the console
if (!(logFh = fopen("./error.log", "a")))
qWarning() << "Unable to open log file ./error.log. Errors will not be logged in a text file";
qInstallMessageHandler(messageLogger);
qSetMessagePattern("[%{time MM/dd/yyyy h:mm:ss}] [%{if-debug}Debug%{endif}%{if-info}Info%{endif}%{if-warning}Warn%{endif}%{if-critical}Crit%{endif}%{if-fatal}Fatal%{endif}] %{function}:%{line} - %{message}");
// Set globalProgramName for stack trace retriever. Set signal handler to print stack trace on exception
globalProgramName = argv[0];
setSignalHandler();
app = new Application(argc, argv);
QCoreApplication::setOrganizationName("Southern Illinois University Edwardsville");
QCoreApplication::setApplicationName("SIUE Fat Segmentation Tool");
QCoreApplication::setApplicationVersion(APP_VERSION);
QSurfaceFormat format;
#ifdef Q_OS_MACOS
// Mac OS X only allows core profiles to be used
// QPainter is used in this application which is based off OpenGL 2.0 but the actual OpenGL used in the shaders
// for drawing slices is OpenGL 3.3.
// Luckily, Qt 5.9 provides support where it brings QPainter up to date in OpenGL (I think)
// To build on Mac, YOU MUST USE AT LEAST Qt 5.9!
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(3, 3);
#else // Q_OS_MACOS
format.setProfile(QSurfaceFormat::CompatibilityProfile);
#endif // Q_OS_MACOS
QSurfaceFormat::setDefaultFormat(format);
w = new MainWindow();
w->show();
ret = app->exec();
}
catch (const std::logic_error &e)
{
qCritical().nospace().noquote() << "Logic Error: " << e.what() << "\n" << printStackTrace();
}
catch (const std::runtime_error &e)
{
qCritical().nospace().noquote() << "Runtime Error: " << e.what() << "\n" << printStackTrace();
}
catch (const Exception &e)
{
qCritical().nospace().noquote() << "Error " << e.title() << " : " << e.message() << "\n" << printStackTrace();
}
catch (const cv::Exception &e)
{
qCritical().nospace().noquote() << "OpenCV Error: " << e.what() << "\n" << printStackTrace();
}
catch (...)
{
qCritical().nospace().noquote() << "An unknown error was thrown\n" << printStackTrace();
}
// Only delete the pointers if they were allocated in the first place,
// sometimes errors happen before they were allocated
if (w)
delete w;
if (app)
delete app;
if (logFh)
fclose(logFh);
return ret;
}