diff src/SDL_log.c @ 5240:1e28342cb15d

Added a way to replace the default logging mechanism
author Sam Lantinga <slouken@libsdl.org>
date Tue, 08 Feb 2011 23:13:58 -0800
parents ab0d7cecc0f6
children b530ef003506
line wrap: on
line diff
--- a/src/SDL_log.c	Tue Feb 08 23:13:28 2011 -0800
+++ b/src/SDL_log.c	Tue Feb 08 23:13:58 2011 -0800
@@ -45,11 +45,19 @@
     struct SDL_LogLevel *next;
 } SDL_LogLevel;
 
+/* The default log output function */
+static void SDL_LogOutput(void *userdata,
+                          int category, SDL_LogPriority priority,
+                          const char *message);
+
 static SDL_LogLevel *SDL_loglevels;
 static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
 static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
+static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
+static void *SDL_log_userdata = NULL;
 
 static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
+    NULL,
     "VERBOSE",
     "DEBUG",
     "INFO",
@@ -235,6 +243,11 @@
 {
     char *message;
 
+    /* Nothing to do if we don't have an output function */
+    if (!SDL_log_function) {
+        return;
+    }
+
     /* Make sure we don't exceed array bounds */
     if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
         return;
@@ -250,7 +263,14 @@
         return;
     }
     SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
+    SDL_log_function(SDL_log_userdata, category, priority, message);
+    SDL_stack_free(message);
+}
 
+static void
+SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
+              const char *message)
+{
 #if defined(__WIN32__)
     /* Way too many allocations here, urgh */
     {
@@ -277,7 +297,24 @@
 #if HAVE_STDIO_H
     fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
 #endif
-    SDL_stack_free(message);
+}
+
+void
+SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
+{
+    if (callback) {
+        *callback = SDL_log_function;
+    }
+    if (userdata) {
+        *userdata = SDL_log_userdata;
+    }
+}
+
+void
+SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
+{
+    SDL_log_function = callback;
+    SDL_log_userdata = userdata;
 }
 
 /* vi: set ts=4 sw=4 expandtab: */