From e301c7fe01bf125a75f4fa6986557a8602b313ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sat, 3 May 2025 11:55:38 +0200 Subject: [PATCH] Add 'console' command line argument This opens a console window on Windows but does nothing anywhere else. Overrides 'redirect'. Useful on Windows because the only way to access standard streams there is by tail -f'ing stdout/err.txt. --- README.md | 1 + src/PowderToy.cpp | 6 +++++- src/common/platform/Platform.h | 1 + src/common/platform/Posix.cpp | 4 ++++ src/common/platform/Windows.cpp | 11 +++++++++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 88ddac86e..38515a61a 100644 --- a/README.md +++ b/README.md @@ -124,5 +124,6 @@ Command Line | `disable-network` | Disables internet connections | | | `disable-bluescreen` | Disable bluescreen handler | | | `redirect` | Redirects output to stdout.txt / stderr.txt | | +| `console` | Redirects output to a new console on Windows | | | `cafile:CAFILE` | Set certificate bundle path | `cafile:/etc/ssl/certs/ca-certificates.crt` | | `capath:CAPATH` | Set certificate directory path | `capath:/etc/ssl/certs` | diff --git a/src/PowderToy.cpp b/src/PowderToy.cpp index 593f1442d..10437fbdf 100644 --- a/src/PowderToy.cpp +++ b/src/PowderToy.cpp @@ -380,7 +380,11 @@ int Main(int argc, char *argv[]) } auto redirectStd = prefs.Get("RedirectStd", false); - if (true_arg(arguments["redirect"]) || redirectStd) + if (true_arg(arguments["console"])) + { + Platform::AllocConsole(); + } + else if (true_arg(arguments["redirect"]) || redirectStd) { FILE *new_stdout = freopen("stdout.log", "w", stdout); FILE *new_stderr = freopen("stderr.log", "w", stderr); diff --git a/src/common/platform/Platform.h b/src/common/platform/Platform.h index b3f2a92d3..76d9b184d 100644 --- a/src/common/platform/Platform.h +++ b/src/common/platform/Platform.h @@ -74,6 +74,7 @@ namespace Platform std::optional> StackTrace(); void MarkPresentable(); + void AllocConsole(); } extern "C" int Main(int argc, char *argv[]); diff --git a/src/common/platform/Posix.cpp b/src/common/platform/Posix.cpp index 975221bb3..710b3bd6c 100644 --- a/src/common/platform/Posix.cpp +++ b/src/common/platform/Posix.cpp @@ -150,4 +150,8 @@ std::vector DirectoryList(ByteString directory) closedir(directoryHandle); return directoryList; } + +void AllocConsole() +{ +} } diff --git a/src/common/platform/Windows.cpp b/src/common/platform/Windows.cpp index e1c257add..fe16de05d 100644 --- a/src/common/platform/Windows.cpp +++ b/src/common/platform/Windows.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Platform { @@ -409,4 +410,14 @@ void SetupCrt() std::cerr << "failed to set codepage to utf-8, expect breakage" << std::endl; } } + +void AllocConsole() +{ + if (!::AllocConsole()) + { + return; + } + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); +} }