Unused __VA_ARGS__

I

Ian Pilcher

I am trying to create a macro which can be used in place of a return
statement to (optionally) log a function's result. My initial version
was:

#define TRACE_RETURN(rv, ...) \
\
do \
{ \
TRACE("%s returning", __func__); \
TRACE(__VA_ARGS__); \
return rv; \
} \
while (0)

For example,

return rv;

would be replaced by

TRACE_RETURN(rv, "returning %d", rv);

Similarly,

return;

is replaced by

TRACE_RETURN(/* void */, "success");

This works, but I'd like to get rid of the "success" or similar
redundant string (which causes a line of useless output in my log file).

I had the bright(?) idea of modifying my logging function to simply
suppress all output if the format string is blank, and I redefined the
TRACE_RETURN macro as follows:

#define TRACE_RETURN(rv, ...) \
\
do \
{ \
TRACE("%s returning", __func__); \
TRACE("" __VA_ARGS__); \
return rv; \
} \
while (0)

The idea is to be able to do:

TRACE_RETURN();

or

TRACE_RETURN(0);

when there's nothing interesting to log.

Unfortunately, this causes my compiler (GCC 3.4.2) to complain about the
variable arguments not being used. I can get rid of the warning/error
by using

TRACE_RETURN(,);

but this is arguably uglier than what I'm trying to get rid of.

Any ideas?

Thanks!
 
R

REH

Ian Pilcher said:
I am trying to create a macro which can be used in place of a return
statement to (optionally) log a function's result. My initial version
was:

#define TRACE_RETURN(rv, ...) \
\
do \
{ \
TRACE("%s returning", __func__); \
TRACE(__VA_ARGS__); \
return rv; \
} \
while (0)

For example,

return rv;

would be replaced by

TRACE_RETURN(rv, "returning %d", rv);

Similarly,

return;

is replaced by

TRACE_RETURN(/* void */, "success");

This works, but I'd like to get rid of the "success" or similar
redundant string (which causes a line of useless output in my log file).

I had the bright(?) idea of modifying my logging function to simply
suppress all output if the format string is blank, and I redefined the
TRACE_RETURN macro as follows:

#define TRACE_RETURN(rv, ...) \
\
do \
{ \
TRACE("%s returning", __func__); \
TRACE("" __VA_ARGS__); \
return rv; \
} \
while (0)

The idea is to be able to do:

TRACE_RETURN();

or

TRACE_RETURN(0);

when there's nothing interesting to log.

Unfortunately, this causes my compiler (GCC 3.4.2) to complain about the
variable arguments not being used. I can get rid of the warning/error
by using

TRACE_RETURN(,);

but this is arguably uglier than what I'm trying to get rid of.

Any ideas?

Thanks!

How about another (!) macro for success. For example, if you used "OK":

#define OK ,

TRACE_RETURN(OK);

Nice great, but it may suit your needed.
 
P

Peter Nilsson

[Some whitespace editing performed in quoting...]

Ian Pilcher wrote:
-> I am trying to create a macro which can be used in place of a
-> return statement to (optionally) log a function's result. My
-> initial version was:
->
-> #define TRACE_RETURN(rv, ...) \
-> do \
-> { \
-> TRACE("%s returning", __func__); \
-> TRACE(__VA_ARGS__); \
-> return rv; \
-> } \
-> while (0)
->
-> For example,
->
-> return rv;
->
-> would be replaced by
->
-> TRACE_RETURN(rv, "returning %d", rv);
->
-> Similarly,
->
-> return;
->
-> is replaced by
->
-> TRACE_RETURN(/* void */, "success");
->
-> This works, but I'd like to get rid of the "success" or
-> similar redundant string (which causes a line of useless
-> output in my log file).

#define TRACE_RETURN_VOID ...whatever...

return TRACE_RETURN_VOID;

-> I had the bright(?) idea of modifying my logging function to
-> simply suppress all output if the format string is blank, and
-> I redefined the TRACE_RETURN macro as follows:
->
-> #define TRACE_RETURN(rv, ...) \
-> do \
-> { \
-> TRACE("%s returning", __func__); \
-> TRACE("" __VA_ARGS__); \
-> return rv; \
-> } \
-> while (0)
->
-> The idea is to be able to do:
->
-> TRACE_RETURN();
-> or
-> TRACE_RETURN(0);
->
-> when there's nothing interesting to log.

Presumably you still want "foobar returning"?

#define NOTHING
#define VOID NOTHING, ""

return TRACE_RETURN(VOID);
return TRACE_RETURN(NOTHING, "but say something interesting");
 
T

those who know me have no need of my name

in comp.lang.c i read:
Similarly,

return;

is replaced by

TRACE_RETURN(/* void */, "success");

This works, but I'd like to get rid of the "success" or similar
redundant string (which causes a line of useless output in my log file).

seems a horrid replacement given that you wish to, in fact, not trace the
return.
 
I

Ian Pilcher

those said:
seems a horrid replacement given that you wish to, in fact, not trace the
return.

Actually, I do want to trace the fact that the function returned. I
just don't have anything else interesting to say. With the original
version of my macro, the above construct would log two lines -- one
that the function is returning, and with "success".

I've since modified the macro to do something like:

#define TRACE_RETURN(rv, ...) \
\
do \
{ \
TRACE("%s returning", __func__); \
TRACE("" __VA_ARGS__); \
return rv; \
} \
while (0)

I modified the underlying logging function to simply output nothing if
the format string is an empty string. Now I can do:

TRACE_RETURN(/* void */, /* success */);

I am restricted to using string literals for the format strings of my
TRACE messages, but it's hard to imagine a situation in which the format
string for a degugging message wouldn't be a literal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top