Reputation: 85
I need to change a implementation of Macro(LOGGING_MACRO) printf into syslog.
Macro Usage :
LOGGING_MACRO(5,("Value of x,y=%d,%d\n",x,y));
Def1 :
#define LOGGING_MACRO(loglevel,str) printf str;
Def2 :
#define LOGGING_MACRO(loglevel,str) syslog(loglevel,str);
Note : I cannot change the macro format :(
Def2 throws error as syslog will not accept 'str'(2nd arg) with front & back braces. [But working fine in Def1 with printf ]
Kindly suggest how to remove the 1st & last braces from 'str' inside the macro before passing 'str' to syslog.
Upvotes: 1
Views: 61
Reputation: 7441
Example below will work in single thread application:
char* custom_log(const char *fmt, ...) {
static char outputString[200]; //Adjust your size for maximal log value
va_list args;
va_start(args, fmt);
vsprintf(outputString, fmt, args);
va_end(args);
return outputString;
}
Then modify your macro to:
#define LOGGING_MACRO(loglevel,str) syslog(loglevel, custom_log str)
Remember, this works only in single-thread mode and make sure, custom_log
function is visible where custom_log function is called.
For multi-thread, you might update it like this:
#define LOGGING_MACRO(loglevel,str) do {\
mutex_lock(); /*Lock your mutex for debug print*/ \
syslog(loglevel, custom_log str); /*Debug*/ \
mutex_unlock(); /*Unlock mutex*/ \
} while (0)
Keep in mind that you have to write mutex_lock
and mutex_unlock
functions for your requirements in your system to lock only debug functions between multi threads.
Upvotes: 2