Re: variadic macros and and empty replacement for __VA_ARGS__
- From: "Richard G. Riley" <rgrdev@xxxxxxxxx>
- Date: Mon, 27 Mar 2006 17:27:34 +0200
On 2006-03-27, John Devereux <jdREMOVE@xxxxxxxxxxxxxxxxxx> wrote:
Urs Thuermann <urs@xxxxxxxxxxxxxxxxx> writes:
I want to write a macro that produces debug output and has a variable<SNIP>
number of arguments, so that I can use like this:
int i;
char *s;
DBG("simple message\n");
DBG("message with an int argument, value is %d\n", i);
DBG("two arguments: int %d, strings %s\n", i, s);
The output should be a fixed prefix, say "DEBUG ", followed by the
function name the macros is used in, and then by the string passed to
the macro, including further arguments.
The best solution I have found so far, is
#define DBG(fmt, ...) printf("DEBUG %s:" fmt, __func__, __VA_ARGS__)
Is there a better way to write the DBG() macro to avoid this?
There was a related thread a couple of weeks ago:
<http://groups.google.co.uk/group/comp.lang.c/browse_frm/thread/ac0bda529a91d754/949239c624c9fe71?lnk=st&q=logging+group%3Acomp.lang.c&rnum=3&hl=en#949239c624c9fe71>
This basically used a *function* instead of a macro. The logging
function accepts variadic arguments, and these can then be modified
and passed on to a varargs form of printf (e.g. vfprintf).
One little thing you might consider at an early stage : have an
importance or category enum as the first/second parameter : very
useful for filtering/enabling/disabling runtime logging for different
subsets in a non trivial system. You might even use the function name
and have settings to enable/disable logging for the particular function.
.
- References:
- variadic macros and and empty replacement for __VA_ARGS__
- From: Urs Thuermann
- Re: variadic macros and and empty replacement for __VA_ARGS__
- From: John Devereux
- variadic macros and and empty replacement for __VA_ARGS__
- Prev by Date: Re: variadic macros and and empty replacement for __VA_ARGS__
- Next by Date: Re: General question
- Previous by thread: Re: variadic macros and and empty replacement for __VA_ARGS__
- Next by thread: Re: variadic macros and and empty replacement for __VA_ARGS__
- Index(es):
Relevant Pages
|