Added a way to replace the default logging mechanism
authorSam Lantinga <slouken@libsdl.org>
Tue, 08 Feb 2011 23:13:58 -0800
changeset 52351e28342cb15d
parent 5234 af8ca17f6b7c
child 5236 c7be6ca3a0b8
Added a way to replace the default logging mechanism
include/SDL_log.h
src/SDL_log.c
     1.1 --- a/include/SDL_log.h	Tue Feb 08 23:13:28 2011 -0800
     1.2 +++ b/include/SDL_log.h	Tue Feb 08 23:13:58 2011 -0800
     1.3 @@ -177,6 +177,22 @@
     1.4                                               SDL_LogPriority priority,
     1.5                                               const char *fmt, va_list ap);
     1.6  
     1.7 +/**
     1.8 + *  \brief The prototype for the log output function
     1.9 + */
    1.10 +typedef void (*SDL_LogOutputFunction)(void *userdata, int category, SDL_LogPriority priority, const char *message);
    1.11 +
    1.12 +/**
    1.13 + *  \brief Get the current log output function.
    1.14 + */
    1.15 +extern DECLSPEC void SDLCALL SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata);
    1.16 +
    1.17 +/**
    1.18 + *  \brief This function allows you to replace the default log output
    1.19 + *         function with one of your own.
    1.20 + */
    1.21 +extern DECLSPEC void SDLCALL SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata);
    1.22 +
    1.23  
    1.24  /* Ends C function definitions when using C++ */
    1.25  #ifdef __cplusplus
     2.1 --- a/src/SDL_log.c	Tue Feb 08 23:13:28 2011 -0800
     2.2 +++ b/src/SDL_log.c	Tue Feb 08 23:13:58 2011 -0800
     2.3 @@ -45,11 +45,19 @@
     2.4      struct SDL_LogLevel *next;
     2.5  } SDL_LogLevel;
     2.6  
     2.7 +/* The default log output function */
     2.8 +static void SDL_LogOutput(void *userdata,
     2.9 +                          int category, SDL_LogPriority priority,
    2.10 +                          const char *message);
    2.11 +
    2.12  static SDL_LogLevel *SDL_loglevels;
    2.13  static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
    2.14  static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
    2.15 +static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
    2.16 +static void *SDL_log_userdata = NULL;
    2.17  
    2.18  static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
    2.19 +    NULL,
    2.20      "VERBOSE",
    2.21      "DEBUG",
    2.22      "INFO",
    2.23 @@ -235,6 +243,11 @@
    2.24  {
    2.25      char *message;
    2.26  
    2.27 +    /* Nothing to do if we don't have an output function */
    2.28 +    if (!SDL_log_function) {
    2.29 +        return;
    2.30 +    }
    2.31 +
    2.32      /* Make sure we don't exceed array bounds */
    2.33      if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
    2.34          return;
    2.35 @@ -250,7 +263,14 @@
    2.36          return;
    2.37      }
    2.38      SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
    2.39 +    SDL_log_function(SDL_log_userdata, category, priority, message);
    2.40 +    SDL_stack_free(message);
    2.41 +}
    2.42  
    2.43 +static void
    2.44 +SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
    2.45 +              const char *message)
    2.46 +{
    2.47  #if defined(__WIN32__)
    2.48      /* Way too many allocations here, urgh */
    2.49      {
    2.50 @@ -277,7 +297,24 @@
    2.51  #if HAVE_STDIO_H
    2.52      fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
    2.53  #endif
    2.54 -    SDL_stack_free(message);
    2.55 +}
    2.56 +
    2.57 +void
    2.58 +SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
    2.59 +{
    2.60 +    if (callback) {
    2.61 +        *callback = SDL_log_function;
    2.62 +    }
    2.63 +    if (userdata) {
    2.64 +        *userdata = SDL_log_userdata;
    2.65 +    }
    2.66 +}
    2.67 +
    2.68 +void
    2.69 +SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
    2.70 +{
    2.71 +    SDL_log_function = callback;
    2.72 +    SDL_log_userdata = userdata;
    2.73  }
    2.74  
    2.75  /* vi: set ts=4 sw=4 expandtab: */