Reputation: 24150
I have third party library which calls an macro for logging. Inside that macro they are using fprintf for logging. I am thinking about modifying the code to define a custom callback for logging, but I am not sure what will be best way.
Solution I want is:
So I am looking for some type of static function with default definition which I can override if I want.
What will be the best way of doing this ? As we can't overload static function so everyone needs to define their definition of the logging function.
Upvotes: 1
Views: 2378
Reputation: 129374
Something like this:
typedef void (*logfuncp)(...);
logfuncp callbackfuncp = NULL;
void logfunction(...)
{
if (callbackfuncp)
callbackfuncp(...);
else
fprintf(logfile, ...);
}
void setcustomlogfunction(logfuncp newcallback)
{
callbackfuncp = newcallback;
}
should work. The ...
represent your logging arguments, which of course could be var-args using that real ...
C syntax, but doesn't have to be. All depends on what your logging is.
Upvotes: 1
Reputation: 2350
Last time when I had similar problem, I used function pointers.
Let the logger call the function using function pointer.
By default set the pointer to your default method ( which is calling fprintf ).
If user want to override that behavior they will use their own functions.
....just another solution
Upvotes: 0