Using SDL_LogSetOutputFunction from C++


So we have our own log function that already use, it has groups and loglevel (error, warn, info, debug, …), and is able to output to different targets (file, console, …).

I would like to use SDL_LogSetOutputFunction to make SDL logs go through our system (adding a SDL group for them). Problem is our codebase is in C++ and I am a bit unsure how to tie things.

I am also unsure exactly how the function SDL expect should look. Has anyone used it and has any guidance to offer?

Here is one way to do something like this in C++:

class Logger
void Log(const char *message);

static Logger log;

static void LogSDL(void *userdata, int category, SDL_LogPriority priority, const char *message)
Logger *logger = reinterpret_cast<Logger *>(userdata);

SDL_LogSetOutputFunction(LogSDL, &log);

If your logging object is dynamically allocated, make sure you call SDL_LogGetOutputFunction() to save the original logging function and reset that with SDL_LogSetOutputFunction() before your logging object is deleted.

If you have a logging function instead of a class, then this is even easier, you can just pass that to SDL_LogSetOutputFunction() and set the userdata parameter to nullptr and ignore it.

1 Like

Thanks! I think I understand now. I was under the impression I would need to use some of those extern C commands but it seems it’s not needed. I will try and report if it works!