Reputation: 102
I have ERROR_OUT function, which uses vfprintf inside to log errors in console. When im trying to log some epoll errors with this function ERROR_OUT(L_NOTICE, "Epoll error: %d", errno);
, i'm getting strange output sometimes:
Epoll error: -1079274856
I'm using it after epoll_wait
and epoll_ctl
only and it's beeing called only in case of negative return value.
Code of ERROR_OUT:
void ERROR_OUT(int level, char *template, ...){
va_list ap;
va_start(ap, template);
if ( level <= verbosity ){
if (output_handler == NULL){
vfprintf(stderr, template, ap);
}else{
(*output_handler)(level, template, ap);
}
}
va_end(ap);
}
Code of output handler:
void log_both(int level, const char *fmt, ...){
va_list argp;
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
printf("\n");
if (globalArgs.db_verbosity >= level)
globalArgs.db_log->log_error(format(fmt, argp));
va_end(argp);
}
Upvotes: 1
Views: 255
Reputation: 606
You pass a va_list
in the call to log_both
via *output_handler
and wrap that va_list
in the va_list
named argp
.
So you actually pass a va_list
that contains a va_list
to vfprintf
as third argument which expects a decimal due to %d
to be the first element in the va_list
.
It should therefore be void log_both(int level, const char *fmt, va_list argp){}
with va_list argp;
, va_start(argp, fmt);
and va_end(argp);
removed from the function body.
void log_both(int level, const char *fmt, va_list argp){
vfprintf(stderr, fmt, argp);
printf("\n");
if (globalArgs.db_verbosity >= level)
globalArgs.db_log->log_error(format(fmt, argp));
}
Upvotes: 1