From 98dc212d68f050928d8b4a3e78309e7041dbc880 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Fri, 19 Nov 2010 13:31:45 +0100 Subject: [PATCH] Make message handler routine thread-safe. --- res/images/Working.gif | Bin 0 -> 3208 bytes src/Config.h | 2 +- src/Global.cpp | 52 ++++++++++++++++++++++++++++++++++ src/Global.h | 4 ++- src/Main.cpp | 49 +------------------------------- src/Thread_Initialization.cpp | 3 +- 6 files changed, 59 insertions(+), 51 deletions(-) create mode 100644 res/images/Working.gif diff --git a/res/images/Working.gif b/res/images/Working.gif new file mode 100644 index 0000000000000000000000000000000000000000..3288d1035d70bb86517e2c233f1a904e41f06b29 GIT binary patch literal 3208 zcmc(iX;4#H9>pJdFE7h`I{IF)0|5<6L}(j=N}5%L009EB2nYfyF)E0PvIqo$u!IC; z4PgyY5|S9AEh38G)(9eq4TbH7_UHg@yWrlIJ$6smIADL7s^P;_O;ykRc9soXl`UC*LwQJXkii*0rx|*7rI2=x7WaRkx_~XZqFJ8R3c=2Kg zf@aSAv8+BJ8+^hyay>(QR@t*blbKzsf0}bscEqRc5Hd3o(-N5RyW=zWB*zQw6Zh>* z2CROCDAbu#D`)S|J_o(lL9Yn3l*+8RdiRD_>iNz$#_IAzCna&Wl5 zSF_(rRCDD!wi#i8oAm&jYtn2_@VB%2-H*G%bN#|(6R6N?wM)3u`PiGzwuX7qmTgyF zpE)h0kuoxQ9?=kW7Y!=R@DmhU9)vwT*EZWzJ zrt+=2tqFts72yIp?|gvdLhs8Hfku^Z(){gmN%Y=K#P|%fkvgUj~HfIp3CuXqCtYGtJ#me+n+-LmP( z*XNuk%!aH8bIE@_Bj46>M*dSro|7<6vZ7WUHh5YQzN$>IJFqCb|CT!wj~R2C2%=q{ zpt8rzY$aw?W?=Ustv{jo?Ow@ZRkLe<)NItY>Cyhle*wR59dTdF6(@{5^ zAQBOB*hNtc3bkY-8{Cm$nFS@elbTtSqrt7MB{h_4y+~`!mVa}?c&N>&?P}GqdMuhQ z&@T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u7*4qQq%A3Mpf!xspWBSKS@O%r*TSM}?dl(@*%{0Jm_8;(h{R__M Bt #include #include +#include //LameXP includes #include "Resource.h" @@ -107,6 +108,9 @@ static QSystemSemaphore *g_lamexp_semaphore_write_ptr = NULL; //Image formats static const char *g_lamexp_imageformats[] = {"png", "gif", "ico", "svg", NULL}; +//Global locks +static QMutex g_lamexp_message_mutex; + /////////////////////////////////////////////////////////////////////////////// // GLOBAL FUNCTIONS /////////////////////////////////////////////////////////////////////////////// @@ -172,6 +176,54 @@ const QDate &lamexp_version_date(void) return g_lamexp_version_date; } +/* + * Qt message handler + */ +void lamexp_message_handler(QtMsgType type, const char *msg) +{ + static HANDLE hConsole = NULL; + QMutexLocker lock(&g_lamexp_message_mutex); + + if(!hConsole) + { + hConsole = CreateFile(L"CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); + if(hConsole == INVALID_HANDLE_VALUE) hConsole = NULL; + } + + CONSOLE_SCREEN_BUFFER_INFO bufferInfo; + GetConsoleScreenBufferInfo(hConsole, &bufferInfo); + + switch(type) + { + case QtCriticalMsg: + case QtFatalMsg: + fflush(stdout); + fflush(stderr); + SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY); + fprintf(stderr, "\nCRITICAL ERROR !!!\n%s\n\n", msg); + MessageBoxA(NULL, msg, "LameXP - CRITICAL ERROR", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL); + break; + case QtWarningMsg: + SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); + fprintf(stderr, "%s\n", msg); + fflush(stderr); + break; + default: + SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); + fprintf(stderr, "%s\n", msg); + fflush(stderr); + break; + } + + SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); + + if(type == QtCriticalMsg || type == QtFatalMsg) + { + FatalAppExit(0, L"The application has encountered a critical error and will exit now!"); + TerminateProcess(GetCurrentProcess(), -1); + } + } + /* * Initialize the console */ diff --git a/src/Global.h b/src/Global.h index 2d24fb83..393f5609 100644 --- a/src/Global.h +++ b/src/Global.h @@ -31,10 +31,11 @@ //Win32 #include -//Class declarations +//Declarations class QString; class LockedFile; class QDate; +enum QtMsgType; //Types definitions typedef struct @@ -65,6 +66,7 @@ unsigned int lamexp_toolver_neroaac(void); void lamexp_init_console(int argc, char* argv[]); bool lamexp_init_qt(int argc, char* argv[]); int lamexp_init_ipc(void); +void lamexp_message_handler(QtMsgType type, const char *msg); void lamexp_register_tool(const QString &toolName, LockedFile *file, unsigned int version = 0); bool lamexp_check_tool(const QString &toolName); const QString lamexp_lookup_tool(const QString &toolName); diff --git a/src/Main.cpp b/src/Main.cpp index 5280b35d..9cff87e9 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -32,6 +32,7 @@ #include #include #include +#include /////////////////////////////////////////////////////////////////////////////// // Main function @@ -134,54 +135,6 @@ int lamexp_main(int argc, char* argv[]) return iResult; } -/////////////////////////////////////////////////////////////////////////////// -// Message Handler -/////////////////////////////////////////////////////////////////////////////// - -static void lamexp_message_handler(QtMsgType type, const char *msg) -{ - static HANDLE hConsole = NULL; - - if(!hConsole) - { - hConsole = CreateFile(L"CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); - } - - CONSOLE_SCREEN_BUFFER_INFO bufferInfo; - GetConsoleScreenBufferInfo(hConsole, &bufferInfo); - - switch(type) - { - case QtCriticalMsg: - case QtFatalMsg: - fflush(stdout); - fflush(stderr); - SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY); - fprintf(stderr, "\nCRITICAL ERROR !!!\n%s\n\n", msg); - MessageBoxA(NULL, msg, "LameXP - CRITICAL ERROR", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL); - break; - case QtWarningMsg: - SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); - fprintf(stderr, "%s\n", msg); - fflush(stderr); - break; - default: - SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); - fprintf(stderr, "%s\n", msg); - fflush(stderr); - break; - } - - SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); - - if(type == QtCriticalMsg || type == QtFatalMsg) - { - FatalAppExit(0, L"The application has encountered a critical error and will exit now!"); - TerminateProcess(GetCurrentProcess(), -1); - } - } - - /////////////////////////////////////////////////////////////////////////////// // Applicaton entry point /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Thread_Initialization.cpp b/src/Thread_Initialization.cpp index 8f7f6be2..3e2dcf93 100644 --- a/src/Thread_Initialization.cpp +++ b/src/Thread_Initialization.cpp @@ -178,6 +178,7 @@ void InitializationThread::initNeroAac(void) { qWarning("Nero process failed to create!"); qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData()); + qDebug("File '%s' does exist?\n%s\n!", neroFileInfo[0].canonicalFilePath().toUtf8().constData(), (neroFileInfo[0].exists() ? "Yes, it still exists" : "Nope, it disappeared")); process.kill(); process.waitForFinished(-1); for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]); @@ -223,7 +224,7 @@ void InitializationThread::initNeroAac(void) if(!(neroVersion > 0)) { - qWarning("Nero AAC version could not be determined!", neroVersion); + qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!", neroVersion); for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]); return; }