From 3fc23f2b2c198dec4551ae1cc9463be86fc75e99 Mon Sep 17 00:00:00 2001 From: Misa Date: Mon, 14 Nov 2022 19:34:48 -0800 Subject: [PATCH] Add `-console` option on Windows This adds the `-console` command-line option (for Win32 only) so the game can spawn an attached console window which will contain all console output. This is to make it easier for people to debug on Windows systems. Otherwise, the only way to get console output would be to either compile the application as a console app (i.e. switch the subsystem to console) - which is undesirable for regular users as this makes it so a console is always spawned even when unwanted - or launch the game with shell arguments that make it so output is redirected to a file. As a result, color checking support is factored out of vlog_init() into its own function, even though we don't support colors on Windows. --- desktop_version/src/Vlogging.c | 43 ++++++++++++++++++++++++++++++++-- desktop_version/src/Vlogging.h | 4 ++++ desktop_version/src/main.cpp | 16 +++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/desktop_version/src/Vlogging.c b/desktop_version/src/Vlogging.c index 6a17f466..666b612b 100644 --- a/desktop_version/src/Vlogging.c +++ b/desktop_version/src/Vlogging.c @@ -2,7 +2,12 @@ #include #include -#if defined(__unix__) || defined(__APPLE__) +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN +# include +# define STDOUT_IS_TTY 0 +# define STDERR_IS_TTY 0 +#elif defined(__unix__) || defined(__APPLE__) # include # define STDOUT_IS_TTY isatty(STDOUT_FILENO) # define STDERR_IS_TTY isatty(STDERR_FILENO) @@ -26,7 +31,7 @@ static int info_enabled = 1; static int warn_enabled = 1; static int error_enabled = 1; -void vlog_init(void) +static void check_color_support(void) { if (STDOUT_IS_TTY && STDERR_IS_TTY) { @@ -34,6 +39,11 @@ void vlog_init(void) } } +void vlog_init(void) +{ + check_color_support(); +} + void vlog_toggle_output(const int enable_output) { output_enabled = enable_output; @@ -147,3 +157,32 @@ SDL_PRINTF_VARARG_FUNC(1) void vlog_error(const char* text, ...) fputc('\n', stderr); } + +#ifdef _WIN32 +void vlog_open_console(void) +{ + static int run_once = 0; + if (run_once) + { + return; + } + run_once = 1; + + const BOOL success = AllocConsole(); + if (!success) + { + /* Debug, not error, because it might not be an error. + * (E.g. there is already an attached console.) */ + vlog_debug( + "Could not open console: AllocConsole() failed with %d", + GetLastError() + ); + return; + } + + freopen("CON", "w", stdout); + freopen("CON", "w", stderr); + + check_color_support(); +} +#endif /* _WIN32 */ diff --git a/desktop_version/src/Vlogging.h b/desktop_version/src/Vlogging.h index 70bc6f09..99d203a8 100644 --- a/desktop_version/src/Vlogging.h +++ b/desktop_version/src/Vlogging.h @@ -10,6 +10,10 @@ extern "C" void vlog_init(void); +#ifdef _WIN32 +void vlog_open_console(void); +#endif + void vlog_toggle_output(int enable_output); void vlog_toggle_color(int enable_color); diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 4632a3d6..25d4779f 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -369,6 +369,9 @@ int main(int argc, char *argv[]) char* baseDir = NULL; char* assetsPath = NULL; bool seed_use_sdl_getticks = false; +#ifdef _WIN32 + bool open_console = false; +#endif vlog_init(); @@ -479,6 +482,12 @@ int main(int argc, char *argv[]) { vlog_toggle_error(0); } +#ifdef _WIN32 + else if (ARG("-console")) + { + open_console = true; + } +#endif else if (ARG("-seed-use-sdl-getticks")) { seed_use_sdl_getticks = true; @@ -492,6 +501,13 @@ int main(int argc, char *argv[]) } } +#ifdef _WIN32 + if (open_console) + { + vlog_open_console(); + } +#endif + if(!FILESYSTEM_init(argv[0], baseDir, assetsPath)) { vlog_error("Unable to initialize filesystem!");