src/SDL_log.c
changeset 11721 19f948dc5620
parent 11284 3db78361e751
child 11722 27dd67e1c5eb
equal deleted inserted replaced
11720:9cbb45a5874f 11721:19f948dc5620
   301     SDL_log_function(SDL_log_userdata, category, priority, message);
   301     SDL_log_function(SDL_log_userdata, category, priority, message);
   302     SDL_stack_free(message);
   302     SDL_stack_free(message);
   303 }
   303 }
   304 
   304 
   305 #if defined(__WIN32__) && !defined(HAVE_STDIO_H) && !defined(__WINRT__)
   305 #if defined(__WIN32__) && !defined(HAVE_STDIO_H) && !defined(__WINRT__)
   306 /* Flag tracking the attachment of the console: 0=unattached, 1=attached, -1=error */
   306 /* Flag tracking the attachment of the console: 0=unattached, 1=attached to a console, 2=attached to a file, -1=error */
   307 static int consoleAttached = 0;
   307 static int consoleAttached = 0;
   308 
   308 
   309 /* Handle to stderr output of console. */
   309 /* Handle to stderr output of console. */
   310 static HANDLE stderrHandle = NULL;
   310 static HANDLE stderrHandle = NULL;
   311 #endif
   311 #endif
   324 
   324 
   325 #if !defined(HAVE_STDIO_H) && !defined(__WINRT__)
   325 #if !defined(HAVE_STDIO_H) && !defined(__WINRT__)
   326         BOOL attachResult;
   326         BOOL attachResult;
   327         DWORD attachError;
   327         DWORD attachError;
   328         unsigned long charsWritten; 
   328         unsigned long charsWritten; 
       
   329         DWORD consoleMode;
   329 
   330 
   330         /* Maybe attach console and get stderr handle */
   331         /* Maybe attach console and get stderr handle */
   331         if (consoleAttached == 0) {
   332         if (consoleAttached == 0) {
   332             attachResult = AttachConsole(ATTACH_PARENT_PROCESS);
   333             attachResult = AttachConsole(ATTACH_PARENT_PROCESS);
   333             if (!attachResult) {
   334             if (!attachResult) {
   348                     }
   349                     }
   349                 } else {
   350                 } else {
   350                     /* Newly attached */
   351                     /* Newly attached */
   351                     consoleAttached = 1;
   352                     consoleAttached = 1;
   352                 }
   353                 }
   353 			
   354 
   354                 if (consoleAttached == 1) {
   355                 if (consoleAttached == 1) {
   355                         stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
   356                         stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
       
   357 
       
   358                         if (GetConsoleMode(stderrHandle, &consoleMode) == 0) {
       
   359                             /* WriteConsole fails if the output is redirected to a file. Must use WriteFile instead. */
       
   360                             OutputDebugString(TEXT("Console ouput is being redirected\r\n"));
       
   361                             consoleAttached = 2;
       
   362                         }
   356                 }
   363                 }
   357         }
   364         }
   358 #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
   365 #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
   359 
   366 
   360         length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1;
   367         length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1 + 1;
   372                     OutputDebugString(TEXT("Error calling WriteConsole\r\n"));
   379                     OutputDebugString(TEXT("Error calling WriteConsole\r\n"));
   373                     if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
   380                     if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
   374                         OutputDebugString(TEXT("Insufficient heap memory to write message\r\n"));
   381                         OutputDebugString(TEXT("Insufficient heap memory to write message\r\n"));
   375                     }
   382                     }
   376                 }
   383                 }
       
   384 
       
   385         } else if (consoleAttached == 2) {
       
   386             if (!WriteFile(stderrHandle, output, lstrlenA(output), &charsWritten, NULL)) {
       
   387                 OutputDebugString(TEXT("Error calling WriteFile\r\n"));
       
   388             }
   377         }
   389         }
   378 #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
   390 #endif /* !defined(HAVE_STDIO_H) && !defined(__WINRT__) */
   379 
   391 
   380         SDL_free(tstr);
   392         SDL_free(tstr);
   381         SDL_stack_free(output);
   393         SDL_stack_free(output);