static void SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message) { #if defined(__WIN32__) /* Way too many allocations here, urgh */ { char *output; size_t length; LPTSTR tstr; BOOL attachResult; DWORD attachError; unsigned long charsWritten; /* Maybe attach console and get stderr handle */ if (consoleAttached == 0) { attachResult = AttachConsole(ATTACH_PARENT_PROCESS); if (!attachResult) { attachError = GetLastError(); if (attachError == ERROR_INVALID_HANDLE) { SDL_SetError("Parent process has no console"); consoleAttached = -1; } else if (attachError == ERROR_GEN_FAILURE) { SDL_SetError("Could not attach to console of parent process"); consoleAttached = -1; } else if (attachError == ERROR_ACCESS_DENIED) { /* Already attached */ consoleAttached = 1; } else { SDL_SetError("Error %d attaching console", attachError); consoleAttached = -1; } } else { /* Newly attached */ consoleAttached = 1; } if (consoleAttached == 1) { stderrHandle = GetStdHandle(STD_ERROR_HANDLE); } } length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1; output = SDL_stack_alloc(char, length); SDL_snprintf(output, length, "%s: %s\n", SDL_priority_prefixes[priority], message); tstr = WIN_UTF8ToString(output); /* Output to debugger */ OutputDebugString(tstr); /* Screen output to stderr, if console was attached. */ if (consoleAttached == 1) { if (!WriteConsole(stderrHandle, tstr, lstrlen(tstr), &charsWritten, NULL)) { SDL_SetError("Error %d calling WriteConsole", GetLastError()); } if (charsWritten == ERROR_NOT_ENOUGH_MEMORY) { SDL_SetError("Insufficient heap memory to write message of size %d", length); } } SDL_free(tstr); SDL_stack_free(output); } #elif defined(__ANDROID__) { char tag[32]; SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category)); __android_log_write(SDL_android_priority[priority], tag, message); } #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) /* Technically we don't need SDL_VIDEO_DRIVER_COCOA, but that's where this function is defined for now. */ extern void SDL_NSLog(const char *text); { char *text; text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); if (text) { SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message); SDL_NSLog(text); SDL_stack_free(text); return; } } #elif defined(__PSP__) { unsigned int length; char* output; FILE* pFile; length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1; output = SDL_stack_alloc(char, length); SDL_snprintf(output, length, "%s: %s", SDL_priority_prefixes[priority], message); pFile = fopen ("SDL_Log.txt", "a"); fwrite (output, strlen (output), 1, pFile); SDL_stack_free(output); fclose (pFile); } #endif #if HAVE_STDIO_H fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message); #endif }
static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority, const char *message) { #if defined(__WIN32__) || defined(__WINRT__) /* Way too many allocations here, urgh */ /* Note: One can't call SDL_SetError here, since that function itself logs. */ { char *output; size_t length; LPTSTR tstr; SDL_bool isstack; #if !defined(HAVE_STDIO_H) && !defined(__WINRT__) BOOL attachResult; DWORD attachError; unsigned long charsWritten; DWORD consoleMode; /* Maybe attach console and get stderr handle */ if (consoleAttached == 0) { attachResult = AttachConsole(ATTACH_PARENT_PROCESS); if (!attachResult) { attachError = GetLastError(); if (attachError == ERROR_INVALID_HANDLE) { /* 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")); consoleAttached = -1; } else if (attachError == ERROR_ACCESS_DENIED) { /* Already attached */ consoleAttached = 1; } else { OutputDebugString(TEXT("Error attaching console\r\n")); consoleAttached = -1; } } else { /* 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__) */ length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1; output = SDL_small_alloc(char, length, &isstack); SDL_snprintf(output, length, "%s: %s\r\n", SDL_priority_prefixes[priority], message); tstr = WIN_UTF8ToString(output); /* Output to debugger */ OutputDebugString(tstr); #if !defined(HAVE_STDIO_H) && !defined(__WINRT__) /* Screen output to stderr, if console was attached. */ if (consoleAttached == 1) { if (!WriteConsole(stderrHandle, tstr, lstrlen(tstr), &charsWritten, NULL)) { OutputDebugString(TEXT("Error calling WriteConsole\r\n")); if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) { 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__) */ SDL_free(tstr); SDL_small_free(output, isstack); } #elif defined(__ANDROID__) { char tag[32]; SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category)); __android_log_write(SDL_android_priority[priority], tag, message); } #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) /* Technically we don't need SDL_VIDEO_DRIVER_COCOA, but that's where this function is defined for now. */ extern void SDL_NSLog(const char *text); { char *text; /* !!! FIXME: why not just "char text[SDL_MAX_LOG_MESSAGE];" ? */ text = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); if (text) { SDL_snprintf(text, SDL_MAX_LOG_MESSAGE, "%s: %s", SDL_priority_prefixes[priority], message); SDL_NSLog(text); SDL_stack_free(text); return; } } #elif defined(__PSP__) { FILE* pFile; pFile = fopen ("SDL_Log.txt", "a"); fprintf(pFile, "%s: %s\n", SDL_priority_prefixes[priority], message); fclose (pFile); } #endif #if HAVE_STDIO_H fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message); #if __NACL__ fflush(stderr); #endif #endif }