From e116cc07015d12972ce3f41db26d4fe18dab4e55 Mon Sep 17 00:00:00 2001 From: Hyunjin Song Date: Thu, 10 Jan 2019 15:13:30 +0900 Subject: [PATCH] Allow console output on Windows if available (#4719) --- src/core/main.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/core/main.cpp b/src/core/main.cpp index 0289f0c53..c51be6f97 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -105,6 +105,21 @@ static inline QString baseName( const QString & file ) } +#ifdef LMMS_BUILD_WIN32 +// Workaround for old MinGW +#ifdef __MINGW32__ +extern "C" _CRTIMP errno_t __cdecl freopen_s(FILE** _File, + const char *_Filename, const char *_Mode, FILE *_Stream); +#endif + +// For qInstallMessageHandler +void consoleMessageHandler(QtMsgType type, + const QMessageLogContext &context, const QString &msg) +{ + QByteArray localMsg = msg.toLocal8Bit(); + fprintf(stderr, "%s\n", localMsg.constData()); +} +#endif inline void loadTranslation( const QString & tname, @@ -243,6 +258,33 @@ int main( int argc, char * * argv ) signal(SIGFPE, signalHandler); #endif +#ifdef LMMS_BUILD_WIN32 + // Don't touch redirected streams here + // GetStdHandle should be called before AttachConsole + HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE); + HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); + HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE); + FILE *fIn, *fOut, *fErr; + // Enable console output if available + if (AttachConsole(ATTACH_PARENT_PROCESS)) + { + if (!hStdIn) + { + freopen_s(&fIn, "CONIN$", "r", stdin); + } + if (!hStdOut) + { + freopen_s(&fOut, "CONOUT$", "w", stdout); + } + if (!hStdErr) + { + freopen_s(&fErr, "CONOUT$", "w", stderr); + } + } + // Make Qt's debug message handlers work + qInstallMessageHandler(consoleMessageHandler); +#endif + // initialize memory managers NotePlayHandleManager::init(); @@ -930,5 +972,15 @@ int main( int argc, char * * argv ) printf( "\n" ); } +#ifdef LMMS_BUILD_WIN32 + // Cleanup console + HWND hConsole = GetConsoleWindow(); + if (hConsole) + { + SendMessage(hConsole, WM_CHAR, (WPARAM)VK_RETURN, (LPARAM)0); + FreeConsole(); + } +#endif + return ret; }