[Commits] SDL: Fixed SDL_Log not working on windows if the output is being...

libsdl.org revision control commits-owner at libsdl.org
Mon Dec 4 20:26:37 PST 2017


details:   https://hg.libsdl.org/SDL/rev/19f948dc5620
changeset: 11721:19f948dc5620
user:      Olli Kallioinen <olli.kallioinen at iki.fi>
date:      Mon Dec 04 19:28:03 2017 +0200
description:
Fixed SDL_Log not working on windows if the output is being redirected.

diffstat:

 src/SDL_log.c |  16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)

diffs (49 lines):

diff -r 9cbb45a5874f -r 19f948dc5620 src/SDL_log.c
--- a/src/SDL_log.c	Mon Dec 04 20:21:52 2017 -0800
+++ b/src/SDL_log.c	Mon Dec 04 19:28:03 2017 +0200
@@ -303,7 +303,7 @@
 }
 
 #if defined(__WIN32__) && !defined(HAVE_STDIO_H) && !defined(__WINRT__)
-/* Flag tracking the attachment of the console: 0=unattached, 1=attached, -1=error */
+/* Flag tracking the attachment of the console: 0=unattached, 1=attached to a console, 2=attached to a file, -1=error */
 static int consoleAttached = 0;
 
 /* Handle to stderr output of console. */
@@ -326,6 +326,7 @@
         BOOL attachResult;
         DWORD attachError;
         unsigned long charsWritten; 
+        DWORD consoleMode;
 
         /* Maybe attach console and get stderr handle */
         if (consoleAttached == 0) {
@@ -350,9 +351,15 @@
                     /* Newly attached */
                     consoleAttached = 1;
                 }
-			
+
                 if (consoleAttached == 1) {
                         stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
+
+                        if (GetConsoleMode(stderrHandle, &consoleMode) == 0) {
+                            /* WriteConsole fails if the output is redirected to a file. Must use WriteFile instead. */
+                            OutputDebugString(TEXT("Console ouput is being redirected\r\n"));
+                            consoleAttached = 2;
+                        }
                 }
         }
 #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
@@ -374,6 +381,11 @@
                         OutputDebugString(TEXT("Insufficient heap memory to write message\r\n"));
                     }
                 }
+
+        } else if (consoleAttached == 2) {
+            if (!WriteFile(stderrHandle, output, lstrlenA(output), &charsWritten, NULL)) {
+                OutputDebugString(TEXT("Error calling WriteFile\r\n"));
+            }
         }
 #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
 


More information about the commits mailing list