comparison 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
comparison
equal deleted inserted replaced
5239:af8ca17f6b7c 5240:1e28342cb15d
43 int category; 43 int category;
44 SDL_LogPriority priority; 44 SDL_LogPriority priority;
45 struct SDL_LogLevel *next; 45 struct SDL_LogLevel *next;
46 } SDL_LogLevel; 46 } SDL_LogLevel;
47 47
48 /* The default log output function */
49 static void SDL_LogOutput(void *userdata,
50 int category, SDL_LogPriority priority,
51 const char *message);
52
48 static SDL_LogLevel *SDL_loglevels; 53 static SDL_LogLevel *SDL_loglevels;
49 static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY; 54 static SDL_LogPriority SDL_application_priority = DEFAULT_APPLICATION_PRIORITY;
50 static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY; 55 static SDL_LogPriority SDL_default_priority = DEFAULT_PRIORITY;
56 static SDL_LogOutputFunction SDL_log_function = SDL_LogOutput;
57 static void *SDL_log_userdata = NULL;
51 58
52 static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = { 59 static const char *SDL_priority_prefixes[SDL_NUM_LOG_PRIORITIES] = {
60 NULL,
53 "VERBOSE", 61 "VERBOSE",
54 "DEBUG", 62 "DEBUG",
55 "INFO", 63 "INFO",
56 "WARN", 64 "WARN",
57 "ERROR", 65 "ERROR",
233 void 241 void
234 SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap) 242 SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap)
235 { 243 {
236 char *message; 244 char *message;
237 245
246 /* Nothing to do if we don't have an output function */
247 if (!SDL_log_function) {
248 return;
249 }
250
238 /* Make sure we don't exceed array bounds */ 251 /* Make sure we don't exceed array bounds */
239 if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) { 252 if (priority < 0 || priority >= SDL_NUM_LOG_PRIORITIES) {
240 return; 253 return;
241 } 254 }
242 255
248 message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE); 261 message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
249 if (!message) { 262 if (!message) {
250 return; 263 return;
251 } 264 }
252 SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap); 265 SDL_vsnprintf(message, SDL_MAX_LOG_MESSAGE, fmt, ap);
253 266 SDL_log_function(SDL_log_userdata, category, priority, message);
267 SDL_stack_free(message);
268 }
269
270 static void
271 SDL_LogOutput(void *userdata, int category, SDL_LogPriority priority,
272 const char *message)
273 {
254 #if defined(__WIN32__) 274 #if defined(__WIN32__)
255 /* Way too many allocations here, urgh */ 275 /* Way too many allocations here, urgh */
256 { 276 {
257 char *output; 277 char *output;
258 size_t length; 278 size_t length;
275 } 295 }
276 #endif 296 #endif
277 #if HAVE_STDIO_H 297 #if HAVE_STDIO_H
278 fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message); 298 fprintf(stderr, "%s: %s\n", SDL_priority_prefixes[priority], message);
279 #endif 299 #endif
280 SDL_stack_free(message); 300 }
301
302 void
303 SDL_LogGetOutputFunction(SDL_LogOutputFunction *callback, void **userdata)
304 {
305 if (callback) {
306 *callback = SDL_log_function;
307 }
308 if (userdata) {
309 *userdata = SDL_log_userdata;
310 }
311 }
312
313 void
314 SDL_LogSetOutputFunction(SDL_LogOutputFunction callback, void *userdata)
315 {
316 SDL_log_function = callback;
317 SDL_log_userdata = userdata;
281 } 318 }
282 319
283 /* vi: set ts=4 sw=4 expandtab: */ 320 /* vi: set ts=4 sw=4 expandtab: */