Re: variadic macros and and empty replacement for __VA_ARGS__



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
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__)

<SNIP>
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.
.



Relevant Pages

  • Re: to give files unique no
    ... int my_api { ... ....in the source file t1_main.c. ... In the code, calls to a logging ... making a lot of the manual macro ...
    (comp.lang.c)
  • Re: some advice
    ... It seems too I copy most of these macro ... I commenti su line che iniziano (dopo eventuali ... char *nome_file; ... int catstr ...
    (alt.lang.asm)
  • Re: A Problem With GD
    ... What does a compiler know? ... long long int are often larger than int. ... It leads to a MACRO! ... void, signed char, unsigned char, char, short, ...
    (comp.lang.perl.misc)
  • Re: alternate function names
    ... So I have a macro ... int { ... # char *_strjoin ... there will be no confusion in the compiler. ...
    (comp.lang.c)
  • Re: A sample RosAsm macro (actually a whole set)
    ... Macro in C are dangerous because they are free to make disasters ... #define UC unsigned char ... void free_node ... int undef ...
    (alt.lang.asm)