preprocessor and macros

M

mosfet

Hi,

I am using a macro to declare some logging functions in debug mode and
nothing in release, something like :

MYTRACE0( "dsqdqsdqds");
MYTRACE1( "%d", 10);
MYTRACE2( "var1=%d,; var2=%d", 10, 11);
....

As you can see I need to declare a macro for each parameter number.
Would 'nt be possible to declare only one macro that could handle
different number of parameters like :

MYTRACE("qsfqsfqfs");
MYTRACE("var1=%d, var2%d", var1, var2);
 
T

Thad Smith

mosfet said:
Would 'nt be possible to declare only one macro that could handle
different number of parameters like :

MYTRACE("qsfqsfqfs");
MYTRACE("var1=%d, var2%d", var1, var2);

As others have said, C99 provides a means to do this. With C90, the
solution is

#define MYTRACE(s) printf s

MYTRACE(("qsfqsfqfs"));
MYTRACE(("var1=%d, var2%d", var1, var2));
 
A

Army1987

As others have said, C99 provides a means to do this. With C90, the
solution is

#define MYTRACE(s) printf s

MYTRACE(("qsfqsfqfs"));
MYTRACE(("var1=%d, var2%d", var1, var2));

What if what he was trying to do was something like
#define MYTRACE(...) fprintf(stderr, __VA_ARGS__)
?
 
K

Kenneth Brody

Army1987 said:
What if what he was trying to do was something like
#define MYTRACE(...) fprintf(stderr, __VA_ARGS__)
?

You can use vfprintf(). (Or is this also C99-only?)

Call a wrapper function rather than printf(), and have this wrapper
pass the args to vfprintf() along with stderr.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
A

Army1987

As others have said, C99 provides a means to do this. With C90, the
solution is

#define MYTRACE(s) printf s

MYTRACE(("qsfqsfqfs"));
MYTRACE(("var1=%d, var2%d", var1, var2));
What about:
#define MYTRACE printf
MYTRACE("qsfqsfqsf");
MYTRACE("var1=%d, var2%d", var1, var2);
 
T

Thad Smith

Army1987 said:
What about:
#define MYTRACE printf
MYTRACE("qsfqsfqsf");
MYTRACE("var1=%d, var2%d", var1, var2);

I omitted an earlier requirement in my post:
> I am using a macro to declare some logging functions in debug mode and
> nothing in release, something like :

The problem is redefining MYTRACE above to do nothing. Perhaps some
compilers would do a nice job with

#define MYTRACE (void)

although they might still generate storage for strings and give
warnings. Try it with you favorite compiler. What does it do?
 
S

SM Ryan

# Hi,
#
# I am using a macro to declare some logging functions in debug mode and
# nothing in release, something like :
#
# MYTRACE0( "dsqdqsdqds");
# MYTRACE1( "%d", 10);
# MYTRACE2( "var1=%d,; var2=%d", 10, 11);
# ...
#
# As you can see I need to declare a macro for each parameter number.
# Would 'nt be possible to declare only one macro that could handle
# different number of parameters like :
#
# MYTRACE("qsfqsfqfs");
# MYTRACE("var1=%d, var2%d", var1, var2);

#if tracing
#define MYTRACE (tracefile=__FILE__,traceline=__LINE__,tracef)
char *tracefile; int traceline;
void tracef(char *format,...) {
printf("trace %s[%d]: ",tracefile,traceline);
va_list argv; va_start(argv,format); vprintf(format,argv); va_end(argv);
printf("\n");
}
#else
#define MYTRACE (void)
#endif
 

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
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top