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); +} }