Skip to content

Commit

Permalink
Fix Windows console setup so it works in an MSYS2 window.
Browse files Browse the repository at this point in the history
This change adds checks to see if a console is already attached
and avoids reopening stdout & stderr if so. This allows console
logging to work properly in the typical MSYS2 bash window. For
some reason the freopen() calls in the old code cause console
printing to stop working.
  • Loading branch information
acolwell committed Jul 15, 2023
1 parent cfbef37 commit 0f15835
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions App/NatronApp_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,28 @@ int main(int argc, char *argv[])

#ifdef Q_OS_WIN
// Setup Windows console output
bool hasConsole = false;
QSettings settings( QString::fromUtf8(NATRON_ORGANIZATION_NAME), QString::fromUtf8(NATRON_APPLICATION_NAME) );
bool enableConsoleWindow = settings.value( QString::fromUtf8("enableConsoleWindow"), false ).toBool();
if (enableConsoleWindow) { // output to console window
hasConsole = ( AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole() );
} else { // output to parent console
hasConsole = AttachConsole(ATTACH_PARENT_PROCESS);
HANDLE stdOutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdOutHandle == INVALID_HANDLE_VALUE) {
// Already having an invalid handle when starting likely means something is terribly wrong.
OutputDebugStringA("Invalid STD_OUTPUT_HANDLE.");
return 1;
}
if (hasConsole) {
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
if (stdOutHandle == NULL) {
// A console is not already attached to this process. Try attaching to our parent's console.
bool hasConsole = AttachConsole(ATTACH_PARENT_PROCESS);
if (!hasConsole && enableConsoleWindow) {
// Failed to attach to the parent console. Try creating a console window.
hasConsole = AllocConsole();
}

if (hasConsole) {
// Reopen stdout & stderr streams so that writing to
// them will display on the attached/allocated console.
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
}
#endif

Expand Down

0 comments on commit 0f15835

Please sign in to comment.