Implemented fatal_exit() and exception handler macros.
This commit is contained in:
parent
7ecdb2f57e
commit
c10ede3ada
@ -20,9 +20,11 @@
|
||||
<ClCompile Include="src\UpdateChecker.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="include\MUtils\Exception.h" />
|
||||
<ClInclude Include="include\MUtils\Global.h" />
|
||||
<ClInclude Include="include\MUtils\OSSupport.h" />
|
||||
<ClInclude Include="include\Mutils\UpdateChecker.h" />
|
||||
<ClInclude Include="src\CriticalSection_Win32.h" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{55405FE1-149F-434C-9D72-4B64348D2A08}</ProjectGuid>
|
||||
|
@ -13,6 +13,9 @@
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Public Headers">
|
||||
<UniqueIdentifier>{d47fbdba-5e24-460a-bba8-824ea0fe874c}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\UpdateChecker.cpp">
|
||||
@ -26,14 +29,20 @@
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="include\Mutils\UpdateChecker.h">
|
||||
<ClInclude Include="src\CriticalSection_Win32.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\MUtils\Exception.h">
|
||||
<Filter>Public Headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\MUtils\Global.h">
|
||||
<Filter>Header Files</Filter>
|
||||
<Filter>Public Headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\MUtils\OSSupport.h">
|
||||
<Filter>Header Files</Filter>
|
||||
<Filter>Public Headers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\Mutils\UpdateChecker.h">
|
||||
<Filter>Public Headers</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
57
include/MUtils/Exception.h
Normal file
57
include/MUtils/Exception.h
Normal file
@ -0,0 +1,57 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// 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
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#define MUTILS_PRINT_ERROR(FORMAT, ...) do \
|
||||
{ \
|
||||
fflush(stdout); \
|
||||
fprintf(stderr, (FORMAT), __VA_ARGS__); \
|
||||
fflush(stderr); \
|
||||
} \
|
||||
while(0)
|
||||
|
||||
#define MUTILS_EXCEPTION_HANDLER(COMMAND) do \
|
||||
{ \
|
||||
try \
|
||||
{ \
|
||||
do { COMMAND; } while(0); \
|
||||
} \
|
||||
catch(const std::exception &error) \
|
||||
{ \
|
||||
MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what()); \
|
||||
MUtils::OS::fatal_exit("Unhandeled C++ exception error, application will exit!"); \
|
||||
} \
|
||||
catch(...) \
|
||||
{ \
|
||||
MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n"); \
|
||||
MUtils::OS::fatal_exit("Unhandeled C++ exception error, application will exit!"); \
|
||||
} \
|
||||
} \
|
||||
while(0)
|
||||
|
||||
#define MUTILS_THROW(MESSAGE) do \
|
||||
{ \
|
||||
throw std::runtime_error((MESSAGE)); \
|
||||
} \
|
||||
while(0)
|
@ -41,6 +41,9 @@ namespace MUtils
|
||||
|
||||
//Get known Folder
|
||||
const QString &known_folder(known_folder_t folder_id);
|
||||
|
||||
//Error handling
|
||||
void fatal_exit(const char* const errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
98
src/CriticalSection_Win32.h
Normal file
98
src/CriticalSection_Win32.h
Normal file
@ -0,0 +1,98 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// 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
|
||||
|
||||
//Win32 API
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <Windows.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// CRITICAL SECTION
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
* wrapper for native Win32 critical sections
|
||||
*/
|
||||
class CriticalSection
|
||||
{
|
||||
public:
|
||||
inline CriticalSection(void)
|
||||
{
|
||||
InitializeCriticalSection(&m_win32criticalSection);
|
||||
}
|
||||
|
||||
inline ~CriticalSection(void)
|
||||
{
|
||||
DeleteCriticalSection(&m_win32criticalSection);
|
||||
}
|
||||
|
||||
inline void enter(void)
|
||||
{
|
||||
EnterCriticalSection(&m_win32criticalSection);
|
||||
}
|
||||
|
||||
inline bool tryEnter(void)
|
||||
{
|
||||
return TryEnterCriticalSection(&m_win32criticalSection);
|
||||
}
|
||||
|
||||
inline void leave(void)
|
||||
{
|
||||
LeaveCriticalSection(&m_win32criticalSection);
|
||||
}
|
||||
|
||||
protected:
|
||||
CRITICAL_SECTION m_win32criticalSection;
|
||||
};
|
||||
|
||||
/*
|
||||
* RAII-style critical section locker
|
||||
*/
|
||||
class CSLocker
|
||||
{
|
||||
public:
|
||||
inline CSLocker(CriticalSection &criticalSection)
|
||||
:
|
||||
m_locked(false),
|
||||
m_criticalSection(criticalSection)
|
||||
{
|
||||
m_criticalSection.enter();
|
||||
m_locked = true;
|
||||
}
|
||||
|
||||
inline ~CSLocker(void)
|
||||
{
|
||||
forceUnlock();
|
||||
}
|
||||
|
||||
inline void forceUnlock(void)
|
||||
{
|
||||
if(m_locked)
|
||||
{
|
||||
m_criticalSection.leave();
|
||||
m_locked = false;
|
||||
}
|
||||
}
|
||||
protected:
|
||||
volatile bool m_locked;
|
||||
CriticalSection &m_criticalSection;
|
||||
};
|
@ -102,7 +102,7 @@ QString MUtils::rand_str(const bool &bLong)
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static QReadWriteLock g_temp_folder_lock;
|
||||
static QFile *g_temp_folder_file;
|
||||
static QFile* g_temp_folder_file = NULL;
|
||||
static QString* g_temp_folder_path = NULL;
|
||||
|
||||
#define INIT_TEMP_FOLDER_RAND(OUT_PTR, FILE_PTR, BASE_DIR) do \
|
||||
|
@ -24,6 +24,7 @@
|
||||
//Internal
|
||||
#include <MUtils/Global.h>
|
||||
#include <MUtils/OSSupport.h>
|
||||
#include "CriticalSection_Win32.h"
|
||||
|
||||
//Win32 API
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
@ -36,6 +37,9 @@
|
||||
#include <QLibrary>
|
||||
#include <QDir>
|
||||
|
||||
//Main thread ID
|
||||
static const DWORD g_main_thread_id = GetCurrentThreadId();
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// KNWON FOLDERS
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -155,3 +159,46 @@ const QString &MUtils::OS::known_folder(known_folder_t folder_id)
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// FATAL EXIT
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static CriticalSection g_fatal_exit_lock;
|
||||
static volatile bool g_fatal_exit_flag = true;
|
||||
|
||||
static DWORD WINAPI fatal_exit_helper(LPVOID lpParameter)
|
||||
{
|
||||
MessageBoxA(NULL, ((LPCSTR) lpParameter), "LameXP - Guru Meditation", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_TOPMOST | MB_SETFOREGROUND);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MUtils::OS::fatal_exit(const char* const errorMessage)
|
||||
{
|
||||
g_fatal_exit_lock.enter();
|
||||
|
||||
if(!g_fatal_exit_flag)
|
||||
{
|
||||
return; /*prevent recursive invocation*/
|
||||
}
|
||||
|
||||
g_fatal_exit_flag = false;
|
||||
|
||||
if(g_main_thread_id != GetCurrentThreadId())
|
||||
{
|
||||
if(HANDLE hThreadMain = OpenThread(THREAD_SUSPEND_RESUME, FALSE, g_main_thread_id))
|
||||
{
|
||||
SuspendThread(hThreadMain); /*stop main thread*/
|
||||
}
|
||||
}
|
||||
|
||||
if(HANDLE hThread = CreateThread(NULL, 0, fatal_exit_helper, (LPVOID) errorMessage, 0, NULL))
|
||||
{
|
||||
WaitForSingleObject(hThread, INFINITE);
|
||||
}
|
||||
|
||||
for(;;)
|
||||
{
|
||||
TerminateProcess(GetCurrentProcess(), 666);
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -19,8 +19,10 @@
|
||||
// http://www.gnu.org/licenses/lgpl-2.1.txt
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <MUtils/UpdateChecker.h>
|
||||
#include <MUtils/Global.h>
|
||||
#include <MUtils/UpdateChecker.h>
|
||||
#include <MUtils/OSSupport.h>
|
||||
#include <MUtils/Exception.h>
|
||||
|
||||
#include <QStringList>
|
||||
#include <QFile>
|
||||
@ -213,7 +215,7 @@ UpdateChecker::UpdateChecker(const QString &binWGet, const QString &binGnuPG, co
|
||||
|
||||
if(m_binaryWGet.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
|
||||
{
|
||||
THROW("Tools not initialized correctly!");
|
||||
MUTILS_THROW("Tools not initialized correctly!");
|
||||
}
|
||||
}
|
||||
|
||||
@ -229,22 +231,7 @@ UpdateChecker::~UpdateChecker(void)
|
||||
void UpdateChecker::run(void)
|
||||
{
|
||||
qDebug("Update checker thread started!");
|
||||
|
||||
try
|
||||
{
|
||||
m_testMode ? testKnownHosts() : checkForUpdates();
|
||||
}
|
||||
catch(const std::exception &error)
|
||||
{
|
||||
PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
|
||||
lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
|
||||
lamexp_fatal_exit("Unhandeled C++ exception error, application will exit!");
|
||||
}
|
||||
|
||||
MUTILS_EXCEPTION_HANDLER(m_testMode ? testKnownHosts() : checkForUpdates());
|
||||
qDebug("Update checker thread completed.");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user