Mac and Linux SDL2 binary snapshots
Edward Rudd
2018-08-19 561f0d614098a95527367cc3f911e476f35643d6
source/src/SDL_log.c
@@ -1,6 +1,6 @@
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
@@ -50,9 +50,7 @@
} SDL_LogLevel;
/* The default log output function */
static void SDL_LogOutput(void *userdata,
                          int category, SDL_LogPriority priority,
                          const char *message);
static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message);
static SDL_LogLevel *SDL_loglevels;
static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
@@ -304,15 +302,15 @@
    SDL_stack_free(message);
}
#if defined(__WIN32__)
/* Flag tracking the attachment of the console: 0=unattached, 1=attached, -1=error */
#if defined(__WIN32__) && !defined(HAVE_STDIO_H) && !defined(__WINRT__)
/* 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. */
static HANDLE stderrHandle = NULL;
#endif
static void
static void SDLCALL
SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
              const char *message)
{
@@ -328,6 +326,7 @@
        BOOL attachResult;
        DWORD attachError;
        unsigned long charsWritten; 
        DWORD consoleMode;
        /* Maybe attach console and get stderr handle */
        if (consoleAttached == 0) {
@@ -335,7 +334,8 @@
            if (!attachResult) {
                    attachError = GetLastError();
                    if (attachError == ERROR_INVALID_HANDLE) {
                        OutputDebugString(TEXT("Parent process has no console\r\n"));
                        /* This is expected when running from Visual Studio */
                        /*OutputDebugString(TEXT("Parent process has no console\r\n"));*/
                        consoleAttached = -1;
                    } else if (attachError == ERROR_GEN_FAILURE) {
                         OutputDebugString(TEXT("Could not attach to console of parent process\r\n"));
@@ -351,9 +351,14 @@
                    /* 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. */
                            consoleAttached = 2;
                        }
                }
        }
#endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
@@ -375,6 +380,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__) */