From 3feb864691cf855f0fcd0240731eb05206ec2254 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sun, 28 Apr 2019 16:59:51 -0700 Subject: [PATCH] Add an optionally-compiled diagnostic console on Windows. --- gnucash/gnucash-bin.c | 57 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/gnucash/gnucash-bin.c b/gnucash/gnucash-bin.c index 3278d3ae61..659f17381f 100644 --- a/gnucash/gnucash-bin.c +++ b/gnucash/gnucash-bin.c @@ -58,11 +58,17 @@ #include "guile-mappings.h" #ifdef __MINGW32__ #include +#include #endif /* This static indicates the debugging module that this .o belongs to. */ static QofLogModule log_module = GNC_MOD_GUI; +/* Change the following to have a console window attached to GnuCash + * for displaying stdout and stderr on Windows. + */ +#define __MSWIN_CONSOLE__ 0 + #ifdef HAVE_GETTEXT # include # include @@ -809,6 +815,53 @@ set_win32_thread_locale() } #endif +/* Creates a console window on MSWindows to display stdout and stderr + * when __MSWIN_CONSOLE__ is defined at the top of the file. + * + * Useful for displaying the diagnostics printed before logging is + * started and if logging is redirected with --logto=stderr. + */ +static void +redirect_stdout (void) +{ +#ifdef __MINGW32__ && __MSWIN_CONSOLE__ + static const WORD MAX_CONSOLE_LINES = 500; + int hConHandle; + long lStdHandle; + CONSOLE_SCREEN_BUFFER_INFO coninfo; + FILE *fp; + + // allocate a console for this app + AllocConsole(); + + // set the screen buffer to be big enough to let us scroll text + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); + coninfo.dwSize.Y = MAX_CONSOLE_LINES; + SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); + + // redirect unbuffered STDOUT to the console + lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); + hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); + fp = _fdopen( hConHandle, "w" ); + *stdout = *fp; + setvbuf( stdout, NULL, _IONBF, 0 ); + + // redirect unbuffered STDIN to the console + lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); + hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); + fp = _fdopen( hConHandle, "r" ); + *stdin = *fp; + setvbuf( stdin, NULL, _IONBF, 0 ); + + // redirect unbuffered STDERR to the console + lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); + hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); + fp = _fdopen( hConHandle, "w" ); + *stderr = *fp; + setvbuf( stderr, NULL, _IONBF, 0 ); +#endif +} + int main(int argc, char ** argv) { @@ -825,6 +878,7 @@ main(int argc, char ** argv) } } #endif + redirect_stdout (); /* This should be called before gettext is initialized * The user may have configured a different language via @@ -846,7 +900,8 @@ main(int argc, char ** argv) g_setenv ("LC_ALL", "C", TRUE); setlocale (LC_ALL, "C"); } -#endif + else + printf ("Locale set to %s\n", sys_locale); #ifdef HAVE_GETTEXT { gchar *localedir = gnc_path_get_localedir();