# Using SDL\_LogSetOutputFunction from C++

**URL:** <https://discourse.libsdl.org/t/using-sdl-logsetoutputfunction-from-c/32661>\
**Category:** SDL Development\
**Created:** [August 25, 2021, 5:07pm UTC](https://discourse.libsdl.org/t/using-sdl-logsetoutputfunction-from-c/32661 "2021-08-25T17:07:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eri0o](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/eri0o/32/4277_2.png) [@eri0o](https://discourse.libsdl.org/u/eri0o)\
**Post date:** [August 25, 2021, 5:07pm UTC](https://discourse.libsdl.org/t/using-sdl-logsetoutputfunction-from-c/32661/1 "2021-08-25T17:07:53Z")

</div>

Hi,

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?

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [August 26, 2021, 4:33pm UTC](https://discourse.libsdl.org/t/using-sdl-logsetoutputfunction-from-c/32661/2 "2021-08-26T16:33:27Z")

</div>

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

class Logger  
{  
public:  
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);  
logger-\>Log(message);  
}

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.

---

<div class="post-metadata">

**Author:** ![eri0o](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/eri0o/32/4277_2.png) [@eri0o](https://discourse.libsdl.org/u/eri0o)\
**Post date:** [August 26, 2021, 4:48pm UTC](https://discourse.libsdl.org/t/using-sdl-logsetoutputfunction-from-c/32661/3 "2021-08-26T16:48:07Z")

</div>

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!
