OSDN Git Service

Implemented startup and error handling functions.
[mutilities/MUtilities.git] / src / ErrorHandler_Win32.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 #pragma once
23
24 //MUtils
25 #include <MUtils/ErrorHandler.h>
26 #include <MUtils/OSSupport.h>
27
28 //Win32 API
29 #define WIN32_LEAN_AND_MEAN 1
30 #include <Windows.h>
31
32 //CRT
33 #include <csignal>
34
35 ///////////////////////////////////////////////////////////////////////////////
36 // CALLBACK FUNCTIONS
37 ///////////////////////////////////////////////////////////////////////////////
38
39 // Invalid parameters handler
40 static void my_invalid_param_handler(const wchar_t* exp, const wchar_t* fun, const wchar_t* fil, unsigned int, uintptr_t)
41 {
42         MUtils::OS::fatal_exit(L"Invalid parameter handler invoked, application will exit!");
43 }
44
45 // Signal handler
46 static void my_signal_handler(int signal_num)
47 {
48         signal(signal_num, my_signal_handler);
49         MUtils::OS::fatal_exit(L"Signal handler invoked, application will exit!");
50 }
51
52 // Global exception handler
53 static LONG WINAPI my_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
54 {
55         MUtils::OS::fatal_exit(L"Unhandeled exception handler invoked, application will exit!");
56         return LONG_MAX;
57 }
58
59 ///////////////////////////////////////////////////////////////////////////////
60 // SETUP ERROR HANDLERS
61 ///////////////////////////////////////////////////////////////////////////////
62
63 void MUtils::ErrorHandler::initialize(void)
64 {
65         SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
66         SetUnhandledExceptionFilter(my_exception_handler);
67         SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
68         _set_invalid_parameter_handler(my_invalid_param_handler);
69         
70         static const int signal_num[6] = { SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
71
72         for(size_t i = 0; i < 6; i++)
73         {
74                 signal(signal_num[i], my_signal_handler);
75         }
76 }
77
78 ///////////////////////////////////////////////////////////////////////////////