Skip to main content
gamedev.net
🔒 Locked

Macro with variable args ?

Started by Howard Roark 5 replies 1.3k views
Original Post
Howard Roark
Howard Roark
I have a printf() like function which I use to print debug messages to a log file and to the debugger. Now, I want all calls to this func to be removed in the release mode. So what I have to do is check whether NDEBUG is defined and then #define the function call away. But, the function has a variable number of arguments, so how do I define a macro for it with multiple args ?? I have : void func(char* msg, ...); what I want to do : #ifdef DEBUG #define macro(X,Y) func(X,Y) #else #define macro(X,Y) #endif But func can have any number of args, not only two. So what do i do?
Paradigm Shifter
Paradigm Shifter
You can''t have variable numbers of arguments for a macro.

If you are using MSVC, check out how MFC does their TRACE macro. The answer is not to have ANY brackets or a parameter list on the macro.

... just checked, they do it like this:

#ifdef _DEBUG
#define TRACE ::AfxTrace
#else
#define TRACE 1 ? (void)0 : ::AfxTrace
#endif

Here, ::AfxTrace is a function which is very similar to :rintf

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
mossmoss
mossmoss
Simple answer: You cannot have a C preprocessor macro with a variable number of arguments.

Complex answer: The upcoming C standard, if I recall correctly, does provide variable argument macros. I expect most compilers have not extended themselves to this feature at this time. Perhaps GNU, though.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
alvaro
alvaro
What I use is

#define DEBUG_BLOCK(X) X

and then

DEBUG_BLOCK(
fprintf(stderr,"At this point t=%d\n",t);
fprintf(stderr," and x=%g\n",x);
)

You can then remove the X when you are not debugging.

Arild Fines
Arild Fines
quote:
Original post by mossmoss
The upcoming C standard,


Upcoming? After C99?



When I look upon seamen, men of physical science, and philosophers, man is the wisest of all beings. When I look upon priests, prophets, and interpreters of dreams, nothing is so contemptible as man.


  Diogenes
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Howard Roark
Howard Roark
Thanks Everybody !
Paradigm Shifter : I saw that technique in the "Using ASSERT,VERIFY,TRACE..." article on the front page.I read it just after posting in the forum.
Guess thats what I will do too.
Thanks.
Igilima
Igilima
Hi all. I ran into the very same problem while trying to implement variable params in a macro call.

I guess C99 allows you to do it but...

Unfortunately MS VC 6.0 does not implement C99 and the service pack doesn''t fix that at all.

Does anyone know if MS VC 7.0 (.net?) implements C99 or whether it allows variable argument lists in macros?

Basically if it will compile the following:

#define LOG(...) \
{ \
LogText(__FILE__, __LINE__, __VA_ARGS__); \
}

I think I can do it this way:
#define LOG(params) \
{ \
LogText params \
}

where a LOG() call would look like this:
LOG((__FILE__, __LINE__, "My age is %d.\n", myAge));

But I would like to avoid having double parentheses everywhere as well as __FILE__, __LINE__, since that was the whole point of wrapping the function call with a macro.

I know someone is going to say use gcc or something other than MS VC and I am considering that but I''d like to know if MS VC 7 will do what I want.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.