Implemented startup and error handling functions.

This commit is contained in:
LoRd_MuldeR 2014-11-26 02:37:08 +01:00
parent d42da03bf1
commit cfbf1e0747
9 changed files with 290 additions and 6 deletions

View File

@ -17,9 +17,11 @@
<ItemGroup>
<ClCompile Include="src\CPUFeatures_Win32.cpp" />
<ClCompile Include="src\DLLMain.cpp" />
<ClCompile Include="src\ErrorHandler_Win32.cpp" />
<ClCompile Include="src\Global.cpp" />
<ClCompile Include="src\KeccakHash.cpp" />
<ClCompile Include="src\OSSupport_Win32.cpp" />
<ClCompile Include="src\Startup.cpp" />
<ClCompile Include="src\Terminal_Win32.cpp" />
<ClCompile Include="src\UpdateChecker.cpp" />
<ClCompile Include="src\Version.cpp" />
@ -27,10 +29,12 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\MUtils\CPUFeatures.h" />
<ClInclude Include="include\MUtils\ErrorHandler.h" />
<ClInclude Include="include\MUtils\Exception.h" />
<ClInclude Include="include\MUtils\Global.h" />
<ClInclude Include="include\MUtils\KeccakHash.h" />
<ClInclude Include="include\MUtils\OSSupport.h" />
<ClInclude Include="include\MUtils\Startup.h" />
<ClInclude Include="include\MUtils\Terminal.h" />
<ClInclude Include="src\DirLocker.h" />
<CustomBuild Include="include\Mutils\UpdateChecker.h">

View File

@ -48,6 +48,12 @@
<ClCompile Include="src\Terminal_Win32.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Startup.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\ErrorHandler_Win32.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\CriticalSection_Win32.h">
@ -77,6 +83,12 @@
<ClInclude Include="include\MUtils\Terminal.h">
<Filter>Public Headers</Filter>
</ClInclude>
<ClInclude Include="include\MUtils\Startup.h">
<Filter>Public Headers</Filter>
</ClInclude>
<ClInclude Include="include\MUtils\ErrorHandler.h">
<Filter>Public Headers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="include\Mutils\UpdateChecker.h">

View File

@ -0,0 +1,38 @@
///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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 <MUtils/Global.h>
///////////////////////////////////////////////////////////////////////////////
namespace MUtils
{
namespace ErrorHandler
{
//Setup Error Handlers
MUTILS_API void initialize(void);
}
}
///////////////////////////////////////////////////////////////////////////////

41
include/MUtils/Startup.h Normal file
View File

@ -0,0 +1,41 @@
///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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 <MUtils/Global.h>
///////////////////////////////////////////////////////////////////////////////
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);
}
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -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);

View File

@ -0,0 +1,78 @@
///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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 <MUtils/ErrorHandler.h>
#include <MUtils/OSSupport.h>
//Win32 API
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h>
//CRT
#include <csignal>
///////////////////////////////////////////////////////////////////////////////
// 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);
}
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -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;
}

107
src/Startup.cpp Normal file
View File

@ -0,0 +1,107 @@
///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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 <MUtils/Startup.h>
#include <MUtils/OSSupport.h>
#include <MUtils/Terminal.h>
#include <MUtils/ErrorHandler.h>
#include <MUtils/Exception.h>
//Qt
#include <QtGlobal>
///////////////////////////////////////////////////////////////////////////////
// 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;
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -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<std::filebuf> g_filebufStdOut;
static QScopedPointer<std::filebuf> g_filebufStdErr;
static QScopedPointer<QFile> 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;
}