From 4973aeca68a0eb57018b205039a40bec975755f4 Mon Sep 17 00:00:00 2001 From: lordmulder Date: Thu, 10 Mar 2011 02:07:51 +0100 Subject: [PATCH] Implemented a workaround to pass UTF-8 strings through qDebug(): As the argument of qDebug() is defined as char*, we cannot pass UTF-16 strings. And as qDebug() mangles UTF-8 strings, we cannot pass UTF-8 encoded strings directly either. Consequently we will now encode UTF-8 strings as Base64 before passing them to qDebug(). A special prefix ("@BASE64@") is used to indicate Base64 encoded strings. --- src/Config.h | 2 +- src/Global.cpp | 39 +++++++++++++++++++++++------------ src/Thread_FileAnalyzer.cpp | 4 ++-- src/Thread_Initialization.cpp | 2 +- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/Config.h b/src/Config.h index 30ce2205..b29b7ff0 100644 --- a/src/Config.h +++ b/src/Config.h @@ -25,7 +25,7 @@ #define VER_LAMEXP_MAJOR 4 #define VER_LAMEXP_MINOR_HI 0 #define VER_LAMEXP_MINOR_LO 1 -#define VER_LAMEXP_BUILD 360 +#define VER_LAMEXP_BUILD 363 #define VER_LAMEXP_SUFFIX Beta-8 /* diff --git a/src/Global.cpp b/src/Global.cpp index 8536215e..58d39cbb 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -263,6 +263,14 @@ void lamexp_message_handler(QtMsgType type, const char *msg) { static HANDLE hConsole = NULL; QMutexLocker lock(&g_lamexp_message_mutex); + const char *buffer = NULL, *text = msg; + char temp[1024]; + + if(!strncmp(msg, "@BASE64@", 8)) + { + buffer = _strdup(QByteArray::fromBase64(msg + 8).constData()); + if(buffer) text = buffer; + } if(!hConsole) { @@ -275,6 +283,8 @@ void lamexp_message_handler(QtMsgType type, const char *msg) if(hConsole) { + int len = sprintf_s(temp, 1024, "%s\n", text); + CONSOLE_SCREEN_BUFFER_INFO bufferInfo; GetConsoleScreenBufferInfo(hConsole, &bufferInfo); SetConsoleOutputCP(CP_UTF8); @@ -286,18 +296,18 @@ void lamexp_message_handler(QtMsgType type, const char *msg) fflush(stdout); fflush(stderr); SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY); - fwprintf(stderr, L"\nGURU MEDITATION !!!\n%S\n\n", msg); - MessageBoxW(NULL, (wchar_t*) QString::fromUtf8(msg).utf16(), L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL); + WriteFile(hConsole, temp, len, NULL, NULL); + FlushFileBuffers(hConsole); break; case QtWarningMsg: SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); - fwprintf(stderr, L"%S\n", msg); - fflush(stderr); + WriteFile(hConsole, temp, len, NULL, NULL); + FlushFileBuffers(hConsole); break; default: SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); - fwprintf(stderr, L"%S\n", msg); - fflush(stderr); + WriteFile(hConsole, temp, len, NULL, NULL); + FlushFileBuffers(hConsole); break; } @@ -305,32 +315,35 @@ void lamexp_message_handler(QtMsgType type, const char *msg) } else { - char temp_buffer[1024]; - switch(type) { case QtCriticalMsg: case QtFatalMsg: - sprintf_s(temp_buffer, 1024, "[LameXP][C] %s", msg); + sprintf_s(temp, 1024, "[LameXP][C] %s", text); break; case QtWarningMsg: - sprintf_s(temp_buffer, 1024, "[LameXP][W] %s", msg); + sprintf_s(temp, 1024, "[LameXP][W] %s", text); break; default: - sprintf_s(temp_buffer, 1024, "[LameXP][I] %s", msg); + sprintf_s(temp, 1024, "[LameXP][I] %s", text); break; } - OutputDebugStringA(temp_buffer); + while(char *ptr = strchr(temp, '\n')) *ptr = '\t'; + strcat_s(temp, 1024, "\n"); + OutputDebugStringA(temp); } if(type == QtCriticalMsg || type == QtFatalMsg) { lock.unlock(); + MessageBoxW(NULL, QWCHAR(QString::fromUtf8(text)), L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL); FatalAppExit(0, L"The application has encountered a critical error and will exit now!"); TerminateProcess(GetCurrentProcess(), -1); } - } + + if(buffer) free((void*) buffer); +} /* * Initialize the console diff --git a/src/Thread_FileAnalyzer.cpp b/src/Thread_FileAnalyzer.cpp index 5b71db3f..48962482 100644 --- a/src/Thread_FileAnalyzer.cpp +++ b/src/Thread_FileAnalyzer.cpp @@ -74,7 +74,7 @@ void FileAnalyzer::run() while(!m_inputFiles.isEmpty()) { QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst()); - qDebug("Analyzing: %s", currentFile.toUtf8().constData()); + qDebug("@BASE64@%s", QString("Analyzing: %1").arg(currentFile).toUtf8().toBase64().constData()); emit fileSelected(QFileInfo(currentFile).fileName()); AudioFileModel file = analyzeFile(currentFile); if(file.fileName().isEmpty() || file.formatContainerType().isEmpty() || file.formatAudioType().isEmpty()) @@ -82,7 +82,7 @@ void FileAnalyzer::run() if(!PlaylistImporter::importPlaylist(m_inputFiles, currentFile)) { m_filesRejected++; - qDebug("Skipped: %s", file.filePath().toUtf8().constData()); + qDebug("@BASE64@%s", QString("Skipped: %1").arg(file.filePath()).toUtf8().toBase64().constData()); } continue; } diff --git a/src/Thread_Initialization.cpp b/src/Thread_Initialization.cpp index 631c88f7..c3bb80ee 100644 --- a/src/Thread_Initialization.cpp +++ b/src/Thread_Initialization.cpp @@ -228,7 +228,7 @@ void InitializationThread::initTranslations(void) if(lamexp_translation_register(langId, qmFile, langName, systemId)) { - qDebug("Registering translation: %s = %s (%u)", qmFile.toLatin1().constData(), langName.toLatin1().constData(), systemId); + qDebug("@BASE64@%s", QString("Registering translation: %1 = %2 (%3)").arg(qmFile, langName, QString::number(systemId)).toUtf8().toBase64().constData()); } else {