If linked to the windows subsystem (-mwindows gcc arg) then there
will be no console available for stdout/err to send data to. Use
the same approach as SDL by redirecting stdout/err to text files
in the current directory.  If linked to the console subsystem
then leave stdout/err untouched.

The redirect can be disabled with QEMU_NO_STDIO_REDIRECT env
variable

The result is that qemu-system-x86_64.exe can use stdio in the
same manner as on any UNIX platform. The qemu-system-x86_64w.exe
binary will log to text files and options like -monitor stdio
will not be available.

Signed-off-by: Daniel P. Berrange <berra...@redhat.com>
---
 vl.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/vl.c b/vl.c
index bb9ed8b..c9997bf 100644
--- a/vl.c
+++ b/vl.c
@@ -2995,6 +2995,22 @@ int main(int argc, char **argv, char **envp)
     Error *main_loop_err = NULL;
     Error *err = NULL;
 
+#ifdef _WIN32
+    /*
+     * If we're linked with -nwindows GetConsoleWindow returns
+     * NULL, so we know stdout/err are not available, so lets
+     * redirect them to a file. If linked to console subsystem
+     * then we can use stdout/err as normal
+     */
+    if (GetConsoleWindow() == NULL &&
+        getenv("QEMU_NO_STDIO_REDIRECT") == NULL) {
+        freopen("stdout.txt", "w", stdout);
+        freopen("stderr.txt", "w", stderr);
+        setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffering */
+        setbuf(stderr, NULL); /* No buffering */
+    }
+#endif /* _WIN32 */
+
     qemu_init_cpu_loop();
     qemu_mutex_lock_iothread();
 
-- 
2.4.3


Reply via email to