OSDN Git Service

Implemented fatal_exit() and exception handler macros.
authorLoRd_MuldeR <mulder2@gmx.de>
Fri, 21 Nov 2014 18:42:39 +0000 (19:42 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Fri, 21 Nov 2014 18:42:39 +0000 (19:42 +0100)
MUtilities_VS2013.vcxproj
MUtilities_VS2013.vcxproj.filters
include/MUtils/Exception.h [new file with mode: 0644]
include/MUtils/OSSupport.h
src/CriticalSection_Win32.h [new file with mode: 0644]
src/Global.cpp
src/OSSupport_Win32.cpp
src/UpdateChecker.cpp

index f8c4cf3..9ea9e71 100644 (file)
     <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>
index b077129..3cb1279 100644 (file)
@@ -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">
     </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>
\ No newline at end of file
diff --git a/include/MUtils/Exception.h b/include/MUtils/Exception.h
new file mode 100644 (file)
index 0000000..ede846c
--- /dev/null
@@ -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)
index b5bcaee..66e03cf 100644 (file)
@@ -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);
        }
 }
 
diff --git a/src/CriticalSection_Win32.h b/src/CriticalSection_Win32.h
new file mode 100644 (file)
index 0000000..e6f7b84
--- /dev/null
@@ -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;
+};
index 8f53d4c..4b3b6f6 100644 (file)
@@ -102,8 +102,8 @@ QString MUtils::rand_str(const bool &bLong)
 ///////////////////////////////////////////////////////////////////////////////
 
 static QReadWriteLock g_temp_folder_lock;
-static QFile *g_temp_folder_file;
-static QString *g_temp_folder_path = NULL;
+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 \
 { \
index ee9e2ba..26bb5d6 100644 (file)
@@ -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);
+       }
+}
+
+///////////////////////////////////////////////////////////////////////////////
index 5725207..490f9e8 100644 (file)
 // 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.");
 }