src/SDL_log.c
changeset 7274 d9ef44d91559
parent 7215 41e739a52f69
child 7292 fdf42e4cdcec
equal deleted inserted replaced
7273:449ec3248b61 7274:d9ef44d91559
    36 
    36 
    37 #define DEFAULT_PRIORITY                SDL_LOG_PRIORITY_CRITICAL
    37 #define DEFAULT_PRIORITY                SDL_LOG_PRIORITY_CRITICAL
    38 #define DEFAULT_ASSERT_PRIORITY         SDL_LOG_PRIORITY_WARN
    38 #define DEFAULT_ASSERT_PRIORITY         SDL_LOG_PRIORITY_WARN
    39 #define DEFAULT_APPLICATION_PRIORITY    SDL_LOG_PRIORITY_INFO
    39 #define DEFAULT_APPLICATION_PRIORITY    SDL_LOG_PRIORITY_INFO
    40 #define DEFAULT_TEST_PRIORITY           SDL_LOG_PRIORITY_VERBOSE
    40 #define DEFAULT_TEST_PRIORITY           SDL_LOG_PRIORITY_VERBOSE
       
    41 
       
    42 /* Forward definition of error function */
       
    43 extern int SDL_SetError(const char *fmt, ...);
    41 
    44 
    42 typedef struct SDL_LogLevel
    45 typedef struct SDL_LogLevel
    43 {
    46 {
    44     int category;
    47     int category;
    45     SDL_LogPriority priority;
    48     SDL_LogPriority priority;
   286     SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
   289     SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
   287     SDL_log_function(SDL_log_userdata, category, priority, message);
   290     SDL_log_function(SDL_log_userdata, category, priority, message);
   288     SDL_stack_free(message);
   291     SDL_stack_free(message);
   289 }
   292 }
   290 
   293 
       
   294 #if defined(__WIN32__)
       
   295 /* Flag tracking the attachment of the console: 0=unattached, 1=attached, -1=error */
       
   296 static int consoleAttached = 0;
       
   297 
       
   298 /* Handle to stderr output of console. */
       
   299 static HANDLE stderrHandle = NULL;
       
   300 #endif
       
   301 
   291 static void
   302 static void
   292 SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
   303 SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
   293               const char *message)
   304               const char *message)
   294 {
   305 {
   295 #if defined(__WIN32__)
   306 #if defined(__WIN32__)
   296     /* Way too many allocations here, urgh */
   307     /* Way too many allocations here, urgh */
   297     {
   308     {
   298         char *output;
   309         char *output;
   299         size_t length;
   310         size_t length;
   300         LPTSTR tstr;
   311         LPTSTR tstr;
       
   312         BOOL pbRemoteDebuggerPresent;        
       
   313         BOOL attachResult;
       
   314         DWORD attachError;
       
   315         unsigned long charsWritten; 
       
   316 
       
   317         /* Maybe attach console and get stderr handle */
       
   318         if (consoleAttached == 0) {
       
   319             attachResult = AttachConsole(ATTACH_PARENT_PROCESS);
       
   320             if (!attachResult) {
       
   321                     attachError = GetLastError();
       
   322                     if (attachError == ERROR_INVALID_HANDLE) {
       
   323                         SDL_SetError("Parent process has no console");
       
   324                         consoleAttached = -1;
       
   325                     } else if (attachError == ERROR_GEN_FAILURE) {
       
   326                          SDL_SetError("Could not attach to console of parent process");
       
   327                          consoleAttached = -1;
       
   328                     } else if (attachError == ERROR_ACCESS_DENIED) {  
       
   329                          /* Already attached */
       
   330                         consoleAttached = 1;
       
   331                     } else {
       
   332                         SDL_SetError("Error %d attaching console", attachError);
       
   333                         consoleAttached = -1;
       
   334                     }
       
   335                 } else {
       
   336                     /* Newly attached */
       
   337                     consoleAttached = 1;
       
   338                 }
       
   339 			
       
   340                 if (consoleAttached == 1) {
       
   341                         stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
       
   342                 }
       
   343         }
   301 
   344 
   302         length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1;
   345         length = SDL_strlen(SDL_priority_prefixes[priority]) + 2 + SDL_strlen(message) + 1 + 1;
   303         output = SDL_stack_alloc(char, length);
   346         output = SDL_stack_alloc(char, length);
   304         SDL_snprintf(output, length, "%s: %s\n", SDL_priority_prefixes[priority], message);
   347         SDL_snprintf(output, length, "%s: %s\n", SDL_priority_prefixes[priority], message);
   305         tstr = WIN_UTF8ToString(output);
   348         tstr = WIN_UTF8ToString(output);
   306         OutputDebugString(tstr);
   349         
       
   350         /* Debugger output, if attached. Check each time since debugger can be attached at runtime. */
       
   351         CheckRemoteDebuggerPresent(GetCurrentProcess(), &pbRemoteDebuggerPresent);
       
   352         if (pbRemoteDebuggerPresent || IsDebuggerPresent()) {
       
   353             OutputDebugString(tstr);
       
   354         }
       
   355        
       
   356         /* Screen output to stderr, if console was attached. */
       
   357         if (consoleAttached == 1) {
       
   358                 if (!WriteConsole(stderrHandle, tstr, lstrlen(tstr), &charsWritten, NULL)) {
       
   359                     SDL_SetError("Error %d calling WriteConsole", GetLastError());
       
   360                 }
       
   361                 if (charsWritten == ERROR_NOT_ENOUGH_MEMORY) {
       
   362                     SDL_SetError("Insufficient heap memory to write message of size %d", length);
       
   363                 }
       
   364         }
       
   365 
   307         SDL_free(tstr);
   366         SDL_free(tstr);
   308         SDL_stack_free(output);
   367         SDL_stack_free(output);
   309     }
   368     }
   310 #elif defined(__ANDROID__)
   369 #elif defined(__ANDROID__)
   311     {
   370     {