twain
April 6th, 1999, 01:25 PM
I would like to create a macro (#define xxx(x,x) etc.) that will accept a varable number of arguments. I then want to send the variable number of arguments to a printf/FormatMessage style code. Can I do this in a macro? It's use would be:
#define SENDLOG(x,...) SendLog(x, __LINE__, __FILE__, ...)
Doesn't work. Anyone have any tricks?
Shawn Wildermuth
NetSuite Development Corporation
Torkel
April 6th, 1999, 08:32 PM
I would write a function for it
Jerry Coffin
April 6th, 1999, 10:42 PM
There are a couple ways of handling this. First of all, you can write a function instead of a macro. Since this sort of thing is often done for debugging, you usually use a pair of functions, one of which is a nop, and choose between them based on whether something is defined or not.
Although it's a little ugly, you can also get a macro to treat a number of things as a single argument by enclosing them in parentheses. For example, the following has been tested and runs:
#include <stdio.h>
#define SENDLOG(fmt, args) \
fprintf(stderr, "%d %s: " fmt, __LINE__, __FILE__, args)
// ...
int main() {
extern int errno;
char errname[20] ="A mock error string";
SENDLOG("%d %s", (errno, errname));
return 0;
}
The universe is a figment of its own imagination.