@@ -69,21 +69,37 @@ int main(int argc, char* argv[])
6969 g_guiLog = new GuiLogSink (console_verbosity);
7070 g_log_sinks.push_back (unique_ptr<GuiLogSink>(g_guiLog));
7171
72- // If on Windows, and not run from a console
73- // remove the stdout log sink that would otherwise spawn a console on startup
72+ // Windows needs special console handling!
7473 #ifdef _WIN32
74+
75+ // If we have a parent process console, we were probably run from a powershell/cmd.exe session.
76+ // If we had one, we need to attach to it (since as a Win32 subsystem application we aren't connected by default)
77+ // Failing here indicates we were run from explorer, and thus should not be spawning a console window
78+ // (we just log to the GuiLogSink instead)
7579 if (!AttachConsole (ATTACH_PARENT_PROCESS))
7680 {
7781 LogNotice (
7882 " Startup: skipping stdout log sink since not run from a console "
7983 " (AttachConsole reports parent process has no console)\n " );
8084 }
85+
86+ // Once we've attached to the console (if we had one), make sure we had a window for it
8187 else if (GetConsoleWindow () == NULL )
8288 LogNotice (" Startup: skipping stdout log sink since not run from a console (no console window)\n " );
89+
90+ // If we get here, we were run from a Windows shell session and should log to that console
8391 else
8492 {
93+ // We're using the existing parent process console.
94+ // Reopen stdio streams so they point to it
95+ freopen (" CON" , " w" , stdout);
96+ freopen (" CON" , " w" , stderr);
97+ freopen (" CON" , " r" , stdin);
8598 #endif
99+
100+ // Creating the log sink is done on all platforms, windows and otherwise
86101 g_log_sinks.push_back (make_unique<ColoredSTDLogSink>(console_verbosity));
102+
87103 #ifdef _WIN32
88104 LogNotice (" Startup: run from a console, keeping stdout log sink attached\n " );
89105 }
0 commit comments