Click here to Skip to main content
16,021,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

I am using SDL in my game, which has a nice logging function called SDL_Log().
I would like to use this, but prefer to use a different name for the function (e.g. GameLog()). The reason is that if I ever want to add functionality to this logging function, I can just do it in one place. Also, if I ever switch from SDL to Allegro or another 2D multimedia library, I don't have to change this everywhere in my code.

The problem is, I have no clue how to do this. The function is defined as:
C++
void SDL_Log(const char* fmt, ...)


How would I be able to do this?

What I have tried:

I tried using #defines (but even though I cant get it to work, I prefer to use no proprocessor code for this).

I tried using typedef, using va_list, va_start, va_arg etc.
The code below didn't do the trick.

C++
void GameLog(const char* log, ...)
{
	va_list args;
	va_start(args, log);
	SDL_Log(log, args);
	va_end(args);
}



Is there anyone that has a clue how to do this?
I thought it would be simple to do at first, but I've been struggling with this for many days now.
Posted
Updated 5-May-19 5:53am
Comments
[no name] 5-May-19 10:34am    
Sounds like you want a "wrapper":

https://stackoverflow.com/questions/879408/c-function-wrapper-that-behaves-just-like-the-function-itself
robvleugel 5-May-19 10:51am    
Hm yes, but none of the solutions seem to work. Normal function pointers for instance don't do the trick here, as std::function doesn't seems to accept (...).
[no name] 5-May-19 10:46am    
You first need to look at the documentation for SDL_Log to see what parameters it expects.
robvleugel 5-May-19 10:53am    
I've given it, its:

void SDL_Log(const char* fmt, ...)

The ... means variable number of arguments. That's what makes this a pain in the ass for me.
[no name] 5-May-19 11:21am    
But that is not what you are trying to pass.

1 solution

You can redefine the function name thus:
C++
#define GameLog SDL_Log

Which allows you to use a different name, although not add functionality.
 
Share this answer
 
Comments
robvleugel 5-May-19 12:27pm    
Thanks for the suggestion Richard.

There is one more downside to this: I'd have to include the SDL.h file in every code file that uses the GameLog function.
[no name] 5-May-19 12:52pm    
Yes, I know. Unfortunately there is no other simple way to do it if that is the only SDL_Log function that is defined.
[no name] 5-May-19 12:54pm    
Maybe this would help: SDL_LogMessageV - SDL Wiki'[^].
robvleugel 5-May-19 12:59pm    
Overlooked that one, since it uses the va list this might just work. Ill check it out, thanks Richard!
[no name] 5-May-19 13:08pm    
It seems that SDL_Log is just a wrapper for SDL_LogMessageV, so you just need to create your own handler that provides the same functionality.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900