GCC has an extension in this area, but the most portable way to handle it:
#define syslog(priority, ...) printf(__VA_ARGS__)
Priority is required, but is ignored by the macro extension. The remaining arguments (required format plus optional next arguments) are in __VA_ARGS__ used in the argument list, in printf() . This will be OK whether the format string is a constant (literal) or a variable.
If you want to mark the output as a syslog() surrogate, I would call a function other than printf() to complete the task:
#define syslog(priority, ...) syslog_print(__VA_ARGS__)
or even
#define syslog(priority, ...) syslog_print(__FILE__, __LINE__, __func__, __VA_ARGS__)
They will be declared as:
extern void syslog_printf(const char *fmt, ...);
or
extern void syslog_printf(const char *file, int line, const char *func, const char *fmt, ...);
The implementation is the direct use of macros in <stdarg.h> plus the v*printf() function:
void syslog_printf(const char *file, int line, const char *func, const char *fmt, ...) { va_list args; printf("SYSLOG:%s:%d:%s: ", file, line, func); va_start(args, fmt); vprintf(fmt, args); va_end(args); }
You can add timestamps and anything you like; You can also easily organize the output to go to the file instead of the standard output; You can also turn it on or off inside the function. Thus, on average, I would replace syslog() surrogate that allows you to configure your codes to use the tools.
In fact, since you discovered a requirement to change the logging behavior, I would suggest that instead of using syslog() directly in your code, you should use your own function (e.g. syslog_printf() , but possibly under a different name) in your code and have various implementations of this feature available to you. The only drawback to this is that calling a real syslog() now more complicated - there is no vsyslog() AFAIK. That way, the main syslog() call will be made by formatting the line in syslog_printf() with vsnprintf() (or vasprintf() if your available and running out of memory is not a likely problem), and then calling syslog() with a pre-formatted string ( syslog(priority, "%s", buffer); ). Of course, you also want priority to be passed to the surrogate function for relays on syslog() .