src/SDL_log.c
changeset 5235 1e28342cb15d
parent 5223 ab0d7cecc0f6
child 5262 b530ef003506
     1.1 --- a/src/SDL_log.c	Tue Feb 08 23:13:28 2011 -0800
     1.2 +++ b/src/SDL_log.c	Tue Feb 08 23:13:58 2011 -0800
     1.3 @@ -45,11 +45,19 @@
     1.4      struct SDL_LogLevel *next;
     1.5  } SDL_LogLevel;
     1.6  
     1.7 +/* The default log output function */
     1.8 +static void SDL_LogOutput(void *userdata,
     1.9 +                          int category, SDL_LogPriority priority,
    1.10 +                          const char *message);
    1.11 +
    1.12  static SDL_LogLevel *SDL_loglevels;
    1.13  static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
    1.14  static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
    1.15 +static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
    1.16 +static void *SDL_log_userdata = NULL;
    1.17  
    1.18  static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
    1.19 +    NULL,
    1.20      "VERBOSE",
    1.21      "DEBUG",
    1.22      "INFO",
    1.23 @@ -235,6 +243,11 @@
    1.24  {
    1.25      char *message;
    1.26  
    1.27 +    /* Nothing to do if we don't have an output function */
    1.28 +    if (!SDL_log_function) {
    1.29 +        return;
    1.30 +    }
    1.31 +
    1.32      /* Make sure we don't exceed array bounds */
    1.33      if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
    1.34          return;
    1.35 @@ -250,7 +263,14 @@
    1.36          return;
    1.37      }
    1.38      SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
    1.39 +    SDL_log_function(SDL_log_userdata, category, priority, message);
    1.40 +    SDL_stack_free(message);
    1.41 +}
    1.42  
    1.43 +static void
    1.44 +SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
    1.45 +              const char *message)
    1.46 +{
    1.47  #if defined(__WIN32__)
    1.48      /* Way too many allocations here, urgh */
    1.49      {
    1.50 @@ -277,7 +297,24 @@
    1.51  #if HAVE_STDIO_H
    1.52      fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
    1.53  #endif
    1.54 -    SDL_stack_free(message);
    1.55 +}
    1.56 +
    1.57 +void
    1.58 +SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
    1.59 +{
    1.60 +    if (callback) {
    1.61 +        *callback = SDL_log_function;
    1.62 +    }
    1.63 +    if (userdata) {
    1.64 +        *userdata = SDL_log_userdata;
    1.65 +    }
    1.66 +}
    1.67 +
    1.68 +void
    1.69 +SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
    1.70 +{
    1.71 +    SDL_log_function = callback;
    1.72 +    SDL_log_userdata = userdata;
    1.73  }
    1.74  
    1.75  /* vi: set ts=4 sw=4 expandtab: */