From cfbf1e0747c2f3150f4b2ebfe258a637904f4458 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Wed, 26 Nov 2014 02:37:08 +0100 Subject: [PATCH] Implemented startup and error handling functions. --- MUtilities_VS2013.vcxproj | 4 ++ MUtilities_VS2013.vcxproj.filters | 12 ++++ include/MUtils/ErrorHandler.h | 38 +++++++++++ include/MUtils/Startup.h | 41 ++++++++++++ include/MUtils/Terminal.h | 2 +- src/ErrorHandler_Win32.cpp | 78 ++++++++++++++++++++++ src/OSSupport_Win32.cpp | 2 +- src/Startup.cpp | 107 ++++++++++++++++++++++++++++++ src/Terminal_Win32.cpp | 12 ++-- 9 files changed, 290 insertions(+), 6 deletions(-) create mode 100644 include/MUtils/ErrorHandler.h create mode 100644 include/MUtils/Startup.h create mode 100644 src/ErrorHandler_Win32.cpp create mode 100644 src/Startup.cpp diff --git a/MUtilities_VS2013.vcxproj b/MUtilities_VS2013.vcxproj index 2158329..ef947ea 100644 --- a/MUtilities_VS2013.vcxproj +++ b/MUtilities_VS2013.vcxproj @@ -17,9 +17,11 @@ + + @@ -27,10 +29,12 @@ + + diff --git a/MUtilities_VS2013.vcxproj.filters b/MUtilities_VS2013.vcxproj.filters index d4fd3a3..a031eeb 100644 --- a/MUtilities_VS2013.vcxproj.filters +++ b/MUtilities_VS2013.vcxproj.filters @@ -48,6 +48,12 @@ Source Files + + Source Files + + + Source Files + @@ -77,6 +83,12 @@ Public Headers + + Public Headers + + + Public Headers + diff --git a/include/MUtils/ErrorHandler.h b/include/MUtils/ErrorHandler.h new file mode 100644 index 0000000..dee25de --- /dev/null +++ b/include/MUtils/ErrorHandler.h @@ -0,0 +1,38 @@ +/////////////////////////////////////////////////////////////////////////////// +// MuldeR's Utilities for Qt +// Copyright (C) 2004-2014 LoRd_MuldeR +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// +// http://www.gnu.org/licenses/lgpl-2.1.txt +////////////////////////////////////////////////////////////////////////////////// + +#pragma once + +//MUtils +#include + +/////////////////////////////////////////////////////////////////////////////// + +namespace MUtils +{ + namespace ErrorHandler + { + //Setup Error Handlers + MUTILS_API void initialize(void); + } +} + +/////////////////////////////////////////////////////////////////////////////// diff --git a/include/MUtils/Startup.h b/include/MUtils/Startup.h new file mode 100644 index 0000000..17239c7 --- /dev/null +++ b/include/MUtils/Startup.h @@ -0,0 +1,41 @@ +/////////////////////////////////////////////////////////////////////////////// +// MuldeR's Utilities for Qt +// Copyright (C) 2004-2014 LoRd_MuldeR +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// +// http://www.gnu.org/licenses/lgpl-2.1.txt +////////////////////////////////////////////////////////////////////////////////// + +#pragma once + +//MUtils +#include + +/////////////////////////////////////////////////////////////////////////////// + +namespace MUtils +{ + namespace Startup + { + //Main Function + typedef int (main_function_t)(int &argc, char **argv); + + //Startup Application + MUTILS_API int startup(int &argc, char **argv, main_function_t *const entry_point); + } +} + +/////////////////////////////////////////////////////////////////////////////// diff --git a/include/MUtils/Terminal.h b/include/MUtils/Terminal.h index d2e40e4..41b2ac7 100644 --- a/include/MUtils/Terminal.h +++ b/include/MUtils/Terminal.h @@ -35,7 +35,7 @@ namespace MUtils namespace Terminal { //Setup terminal - MUTILS_API void setup(const QStringList &argv, const bool forceEnabled); + MUTILS_API void setup(int &argc, char **argv, const bool forceEnabled); //Terminal output MUTILS_API void write(const int &type, const char *const message); diff --git a/src/ErrorHandler_Win32.cpp b/src/ErrorHandler_Win32.cpp new file mode 100644 index 0000000..7a4222e --- /dev/null +++ b/src/ErrorHandler_Win32.cpp @@ -0,0 +1,78 @@ +/////////////////////////////////////////////////////////////////////////////// +// MuldeR's Utilities for Qt +// Copyright (C) 2004-2014 LoRd_MuldeR +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// +// http://www.gnu.org/licenses/lgpl-2.1.txt +////////////////////////////////////////////////////////////////////////////////// + +#pragma once + +//MUtils +#include +#include + +//Win32 API +#define WIN32_LEAN_AND_MEAN 1 +#include + +//CRT +#include + +/////////////////////////////////////////////////////////////////////////////// +// CALLBACK FUNCTIONS +/////////////////////////////////////////////////////////////////////////////// + +// Invalid parameters handler +static void my_invalid_param_handler(const wchar_t* exp, const wchar_t* fun, const wchar_t* fil, unsigned int, uintptr_t) +{ + MUtils::OS::fatal_exit(L"Invalid parameter handler invoked, application will exit!"); +} + +// Signal handler +static void my_signal_handler(int signal_num) +{ + signal(signal_num, my_signal_handler); + MUtils::OS::fatal_exit(L"Signal handler invoked, application will exit!"); +} + +// Global exception handler +static LONG WINAPI my_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo) +{ + MUtils::OS::fatal_exit(L"Unhandeled exception handler invoked, application will exit!"); + return LONG_MAX; +} + +/////////////////////////////////////////////////////////////////////////////// +// SETUP ERROR HANDLERS +/////////////////////////////////////////////////////////////////////////////// + +void MUtils::ErrorHandler::initialize(void) +{ + SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); + SetUnhandledExceptionFilter(my_exception_handler); + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL); + _set_invalid_parameter_handler(my_invalid_param_handler); + + static const int signal_num[6] = { SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM }; + + for(size_t i = 0; i < 6; i++) + { + signal(signal_num[i], my_signal_handler); + } +} + +/////////////////////////////////////////////////////////////////////////////// diff --git a/src/OSSupport_Win32.cpp b/src/OSSupport_Win32.cpp index 8aab56a..e03eae7 100644 --- a/src/OSSupport_Win32.cpp +++ b/src/OSSupport_Win32.cpp @@ -481,7 +481,7 @@ static volatile bool g_fatal_exit_flag = true; static DWORD WINAPI fatal_exit_helper(LPVOID lpParameter) { - MUtils::OS::system_message_err((LPWSTR) lpParameter, L"GURU MEDITATION"); + MUtils::OS::system_message_err(L"GURU MEDITATION", (LPWSTR) lpParameter); return 0; } diff --git a/src/Startup.cpp b/src/Startup.cpp new file mode 100644 index 0000000..32c80bd --- /dev/null +++ b/src/Startup.cpp @@ -0,0 +1,107 @@ +/////////////////////////////////////////////////////////////////////////////// +// MuldeR's Utilities for Qt +// Copyright (C) 2004-2014 LoRd_MuldeR +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// +// http://www.gnu.org/licenses/lgpl-2.1.txt +////////////////////////////////////////////////////////////////////////////////// + +//MUtils +#include +#include +#include +#include +#include + +//Qt +#include + +/////////////////////////////////////////////////////////////////////////////// +// MESSAGE HANDLER +/////////////////////////////////////////////////////////////////////////////// + +static void qt_message_handler(QtMsgType type, const char *msg) +{ + if((!msg) || (!(msg[0]))) + { + return; + } + + MUtils::Terminal::write(type, msg); + + if((type == QtCriticalMsg) || (type == QtFatalMsg)) + { + MUtils::OS::fatal_exit(MUTILS_WCHR(QString::fromUtf8(msg))); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// STARTUP FUNCTION +/////////////////////////////////////////////////////////////////////////////// + +static int startup_main(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point) +{ + qInstallMsgHandler(qt_message_handler); + MUtils::Terminal::setup(argc, argv, MUTILS_DEBUG); + return entry_point(argc, argv); +} + +static int startup_helper(int &argc, char **argv, MUtils::Startup::main_function_t *const entry_point) +{ + int iResult = -1; + try + { + iResult = startup_main(argc, argv, entry_point); + } + catch(const std::exception &error) + { + MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what()); + MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!"); + } + catch(...) + { + MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n"); + MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!"); + } + return iResult; +} + +int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entry_point) +{ + int iResult = -1; +#if(MUTILS_DEBUG) + iResult = startup_main(argc, argv, entry_point); +#else //MUTILS_DEBUG +#ifdef _MSC_VER + __try + { + MUtils::ErrorHandler::initialize(); + iResult = startup_helper(argc, argv, entry_point); + } + __except(1) + { + MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n"); + MUtils::OS::fatal_exit(L"Unhandeled structured exception error, application will exit!"); + } +#else //_MSCVER + MUtils::ErrorHandler::initialize(); + iResult = startup_helper(argc, argv, entry_point); +#endif //_MSCVER +#endif //MUTILS_DEBUG + return iResult; +} + +/////////////////////////////////////////////////////////////////////////////// diff --git a/src/Terminal_Win32.cpp b/src/Terminal_Win32.cpp index 298f7d6..eff8630 100644 --- a/src/Terminal_Win32.cpp +++ b/src/Terminal_Win32.cpp @@ -46,6 +46,10 @@ //Lock static MUtils::Internal::CriticalSection g_terminal_lock; +#ifdef _MSC_VER +#define stricmp(X,Y) _stricmp((X),(Y)) +#endif + /////////////////////////////////////////////////////////////////////////////// // HELPER FUNCTIONS /////////////////////////////////////////////////////////////////////////////// @@ -105,7 +109,7 @@ static QScopedPointer g_filebufStdOut; static QScopedPointer g_filebufStdErr; static QScopedPointer g_log_file; -void MUtils::Terminal::setup(const QStringList &argv, const bool forceEnabled) +void MUtils::Terminal::setup(int &argc, char **argv, const bool forceEnabled) { MUtils::Internal::CSLocker lock(g_terminal_lock); bool enableConsole = (MUTILS_DEBUG) || forceEnabled; @@ -130,13 +134,13 @@ void MUtils::Terminal::setup(const QStringList &argv, const bool forceEnabled) if(!MUTILS_DEBUG) { - for(int i = 0; i < argv.count(); i++) + for(int i = 0; i < argc; i++) { - if(!argv.at(i).compare("--console", Qt::CaseInsensitive)) + if(!stricmp(argv[i], "--console")) { enableConsole = true; } - else if(!argv.at(i).compare("--no-console", Qt::CaseInsensitive)) + else if(!stricmp(argv[i], "--no-console")) { enableConsole = false; }