From 68f7e11978a3dd3fbcf4f982dea64522aa8da60b Mon Sep 17 00:00:00 2001 From: Matt Borgerson <contact@mborgerson.com> Date: Mon, 24 May 2021 01:20:41 -0700 Subject: [PATCH] ui: Log std{out,err} to file, or console if available on Windows --- build.sh | 12 ++++-------- ui/xemu.c | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/build.sh b/build.sh index e35d8494e9..5466aa039c 100755 --- a/build.sh +++ b/build.sh @@ -9,23 +9,19 @@ project_source_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd package_windows() { rm -rf dist mkdir -p dist - cp build/qemu-system-i386.exe dist/xemu.exe - cp build/qemu-system-i386w.exe dist/xemuw.exe + cp build/qemu-system-i386w.exe dist/xemu.exe # cp -r "${project_source_dir}/data" dist/ python3 "${project_source_dir}/get_deps.py" dist/xemu.exe dist strip dist/xemu.exe - strip dist/xemuw.exe } package_wincross() { STRIP=${CROSSPREFIX}strip rm -rf dist mkdir -p dist - cp build/qemu-system-i386.exe dist/xemu.exe - cp build/qemu-system-i386w.exe dist/xemuw.exe + cp build/qemu-system-i386w.exe dist/xemu.exe # cp -r "${project_source_dir}/data" dist/ $STRIP dist/xemu.exe - $STRIP dist/xemuw.exe } package_macos() { @@ -196,7 +192,7 @@ case "$platform" in # Adjust compilation options based on platform sys_cflags='-Wno-error' opts="$opts --disable-fortify-source" postbuild='package_windows' # set the above function to be called after build - target="qemu-system-i386.exe qemu-system-i386w.exe" + target="qemu-system-i386w.exe" ;; win64-cross) echo 'Cross-compiling for Windows...' @@ -204,7 +200,7 @@ case "$platform" in # Adjust compilation options based on platform sys_cflags='-Wno-error' opts="$opts --cross-prefix=$CROSSPREFIX --static --disable-fortify-source" postbuild='package_wincross' # set the above function to be called after build - target="qemu-system-i386.exe qemu-system-i386w.exe" + target="qemu-system-i386w.exe" ;; *) echo "Unsupported platform $platform, aborting" >&2 diff --git a/ui/xemu.c b/ui/xemu.c index c615c8dcee..0da047a1f2 100644 --- a/ui/xemu.c +++ b/ui/xemu.c @@ -1475,6 +1475,28 @@ int main(int argc, char **argv) { QemuThread thread; +#ifdef _WIN32 + if (AttachConsole(ATTACH_PARENT_PROCESS)) { + // Launched with a console. If stdout and stderr are not associated with + // an output stream, redirect to parent console. + if (_fileno(stdout) == -2) { + freopen("CONOUT$", "w+", stdout); + } + if (_fileno(stderr) == -2) { + freopen("CONOUT$", "w+", stderr); + } + } else { + // Launched without a console. Redirect stdout and stderr to a log file. + HANDLE logfile = CreateFileA("xemu.log", + GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, + NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (logfile != INVALID_HANDLE_VALUE) { + freopen("xemu.log", "a", stdout); + freopen("xemu.log", "a", stderr); + } + } +#endif + DPRINTF("Entered main()\n"); gArgc = argc; gArgv = argv;