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.
This commit is contained in:
Tamás Bálint Misius
2025-05-03 11:55:38 +02:00
parent afe35312e1
commit e301c7fe01
5 changed files with 22 additions and 1 deletions

View File

@@ -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` |

View File

@@ -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);

View File

@@ -74,6 +74,7 @@ namespace Platform
std::optional<std::vector<String>> StackTrace();
void MarkPresentable();
void AllocConsole();
}
extern "C" int Main(int argc, char *argv[]);

View File

@@ -150,4 +150,8 @@ std::vector<ByteString> DirectoryList(ByteString directory)
closedir(directoryHandle);
return directoryList;
}
void AllocConsole()
{
}
}

View File

@@ -10,6 +10,7 @@
#include <windows.h>
#include <crtdbg.h>
#include <memory>
#include <cstdlib>
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);
}
}