J
João Jerónimo
Hi,
I want to wrap around printf using a C99 variadic macro. However, I don't
know how to get rid of the last comma of the printf call. For example:
#define PRINT_DBG_MESS(FORMAT, ...) fprintf(stderr, "DEBUG: %s:%d: "
FORMAT, __FILE__, __LINE__, __VA_ARGS__)
This is all right if I call the macro with at least one optional argument.
However, if the macro has no argument, it's a syntax error, because the
call to fprintf is left with a comma just before the parentecis.
PRINT_DBG_MESS("I'm %d!\n", 19);
// Expands to:
fprintf(stderr, "DEBUG: %s:%d: " "I'm %d!\n" , "pervert.c", 69, 19);
// Which is perfectly alright.
// However:
PRINT_DBG_MESS("Go to the hell!\n");
// Expands to:
fprintf(stderr, "DEBUG: %s:%d: " "Go to the hell!\n" , "silly.c", 42, );
// Which is toxic...
I solved this with a dirty hack. The story began be defining the macro as:
#define PRINT_DBG_MESS(FORMAT, ...) fprintf(stderr, "DEBUG: %s:%d: "
FORMAT, __FILE__, __LINE__, __VA_ARGS__+0)
Now, the later call got expanded to:
fprintf(stderr, "DEBUG: %s:%d: " "Go to the hell!\n" , "silly.c", 42, +0);
Which is syntactically correct. However, the compiler is smart, and as such
it complains about fprintf having more arguments than it was supposed to
have. So, I defing an alternative wrapper function that would call vfprintf
1:1. I mean:
static void PRINT_DBG_MESS_HELPER(char *format, ...) {
va_list list_of_varargs;
va_start(list_of_varargs, format);
vfprintf(stderr, format, list_of_varargs);
va_end(list_of_varargs);
}
#define PRINT_DBG_MESS(FORMAT, ...) PRINT_DBG_MESS_HELPER( "DEBUG: %s:%d:
" FORMAT, __FILE__, __LINE__, __VA_ARGS__+0)
And now it worked. But it's a dirty hack, and I don't like it! Especially
because ten minutes ago I fed by mistake the macro with a wrong argument
list and the compiled ignored it silently...
PS: I DO want to have the filename and the line number printed.
Thanks in advance,
JJ
I want to wrap around printf using a C99 variadic macro. However, I don't
know how to get rid of the last comma of the printf call. For example:
#define PRINT_DBG_MESS(FORMAT, ...) fprintf(stderr, "DEBUG: %s:%d: "
FORMAT, __FILE__, __LINE__, __VA_ARGS__)
This is all right if I call the macro with at least one optional argument.
However, if the macro has no argument, it's a syntax error, because the
call to fprintf is left with a comma just before the parentecis.
PRINT_DBG_MESS("I'm %d!\n", 19);
// Expands to:
fprintf(stderr, "DEBUG: %s:%d: " "I'm %d!\n" , "pervert.c", 69, 19);
// Which is perfectly alright.
// However:
PRINT_DBG_MESS("Go to the hell!\n");
// Expands to:
fprintf(stderr, "DEBUG: %s:%d: " "Go to the hell!\n" , "silly.c", 42, );
// Which is toxic...
I solved this with a dirty hack. The story began be defining the macro as:
#define PRINT_DBG_MESS(FORMAT, ...) fprintf(stderr, "DEBUG: %s:%d: "
FORMAT, __FILE__, __LINE__, __VA_ARGS__+0)
Now, the later call got expanded to:
fprintf(stderr, "DEBUG: %s:%d: " "Go to the hell!\n" , "silly.c", 42, +0);
Which is syntactically correct. However, the compiler is smart, and as such
it complains about fprintf having more arguments than it was supposed to
have. So, I defing an alternative wrapper function that would call vfprintf
1:1. I mean:
static void PRINT_DBG_MESS_HELPER(char *format, ...) {
va_list list_of_varargs;
va_start(list_of_varargs, format);
vfprintf(stderr, format, list_of_varargs);
va_end(list_of_varargs);
}
#define PRINT_DBG_MESS(FORMAT, ...) PRINT_DBG_MESS_HELPER( "DEBUG: %s:%d:
" FORMAT, __FILE__, __LINE__, __VA_ARGS__+0)
And now it worked. But it's a dirty hack, and I don't like it! Especially
because ten minutes ago I fed by mistake the macro with a wrong argument
list and the compiled ignored it silently...
PS: I DO want to have the filename and the line number printed.
Thanks in advance,
JJ