OSDN Git Service

Implemented startup and error handling functions.
[mutilities/MUtilities.git] / src / Startup.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 //MUtils
23 #include <MUtils/Startup.h>
24 #include <MUtils/OSSupport.h>
25 #include <MUtils/Terminal.h>
26 #include <MUtils/ErrorHandler.h>
27 #include <MUtils/Exception.h>
28
29 //Qt
30 #include <QtGlobal>
31
32 ///////////////////////////////////////////////////////////////////////////////
33 // MESSAGE HANDLER
34 ///////////////////////////////////////////////////////////////////////////////
35
36 static void qt_message_handler(QtMsgType type, const char *msg)
37 {
38         if((!msg) || (!(msg[0])))
39         {
40                 return;
41         }
42
43         MUtils::Terminal::write(type, msg);
44
45         if((type == QtCriticalMsg) || (type == QtFatalMsg))
46         {
47                 MUtils::OS::fatal_exit(MUTILS_WCHR(QString::fromUtf8(msg)));
48         }
49 }
50
51 ///////////////////////////////////////////////////////////////////////////////
52 // STARTUP FUNCTION
53 ///////////////////////////////////////////////////////////////////////////////
54
55 static int startup_main(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point)
56 {
57         qInstallMsgHandler(qt_message_handler);
58         MUtils::Terminal::setup(argc, argv, MUTILS_DEBUG);
59         return entry_point(argc, argv);
60 }
61
62 static int startup_helper(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point)
63 {
64         int iResult = -1;
65         try
66         {
67                 iResult = startup_main(argc, argv, entry_point);
68         }
69         catch(const std::exception &error)
70         {
71                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
72                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
73         }
74         catch(...)
75         {
76                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
77                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
78         }
79         return iResult;
80 }
81
82 int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entry_point)
83 {
84         int iResult = -1;
85 #if(MUTILS_DEBUG)
86         iResult = startup_main(argc, argv, entry_point);
87 #else //MUTILS_DEBUG
88 #ifdef _MSC_VER
89         __try
90         {
91                 MUtils::ErrorHandler::initialize();
92                 iResult = startup_helper(argc, argv, entry_point);
93         }
94         __except(1)
95         {
96                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
97                 MUtils::OS::fatal_exit(L"Unhandeled structured exception error, application will exit!");
98         }
99 #else //_MSCVER
100         MUtils::ErrorHandler::initialize();
101         iResult = startup_helper(argc, argv, entry_point);
102 #endif //_MSCVER
103 #endif //MUTILS_DEBUG
104         return iResult;
105 }
106
107 ///////////////////////////////////////////////////////////////////////////////