* Preprocessor macro question
@ 2001-10-29 7:18 Joe MacDonald
2001-10-29 8:29 ` John Love-Jensen
0 siblings, 1 reply; 2+ messages in thread
From: Joe MacDonald @ 2001-10-29 7:18 UTC (permalink / raw)
To: gcc-help
Hi folks,
I looked around in the archives but couldn't find a conclusive answer to
my question. I have the following macro defined:
#ifdef _J_DEBUG_
#define debugDump(format, ...) \
fprintf(stderr, "%s:%d> ", __FILE__, __LINE__); \
fprintf(stderr, format, ##__VA_ARGS__ )
#else
#define debugDump(format, ...)
#endif
And I'm defining _J_DEBUG_ at compile time. Now in a file called cmd.c
I have the following:
17 debugDump("not a command node");
18 !xmlnode->name ?
19 debugDump("xmlnode->name == NULL\n") :
20 debugDump("xmlnode->name == \"%s\"\n", "foo") ;
And when I compile I see this:
cmd.c:20:54: warning: pasting would not give a valid preprocessing token
cmd.c:25:67: warning: pasting would not give a valid preprocessing token
cmd.c: In function `addCommand':
cmd.c:19: parse error before `;'
cmd.c:19: parse error before `:'
make: *** [cmd-d] Error 1
So here's my question. Have I done something particularly wrong, or is
this related to a bug back in September 2000 discussed in the thread at
http://gcc.gnu.org/ml/gcc/2000-09/msg00138.html by Zack Weinberg among
others? I suspect it is since I'm currently using gcc 2.96 (from a Red
Hat 7.0 install through no fault of my own. ;-) ) but before I start
asking for folks on the team to upgrade the compiler, I'd like some
advice.
Thanks,
-Joe.
--
Joe MacDonald
:wq
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Preprocessor macro question
2001-10-29 7:18 Preprocessor macro question Joe MacDonald
@ 2001-10-29 8:29 ` John Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2001-10-29 8:29 UTC (permalink / raw)
To: Joe MacDonald, gcc-help
Hi Joe,
That's not a well formed macro. Do this instead...
#define debugDump(x) \
printf("%s:%d> ", __FILE__, __LINE__); \
printf x
void Foo(const char* inName) {
debugDump(("%s %s %s\n", "Hi", "there", inName));
}
Note: If you want to do stderr, you'll need a stub routine which uses
vfprintf coded to spit out to stderr.
Also, you need the double-parens to contain the parms. Icky.
But there's an even better C++ way anyway.
// Caution: macro magery.
#define debugDump(x) \
cerr << __FILE__ << ":" << __LINE__ << "> " << x << endl;
void Foo(const string& inName) {
debugDump("Hi" << " " << "there" << " " << inName);
}
And if you have some sort of log levels, you can add that into the
macro-magic. (Note: macro-magic is usually considered bad, this is one of
the few cases where I find it acceptable in C++. I wish invoking a macro
REQUIRED an octalthorpe ('#') character on the end of a macro name. *sigh*)
namespace Log {
enum Level_t { kEmergency, kAlert, kCritical, kError, kWarning, kNotice,
kInfo, kDebug };
Level_t gLevel = kError;
} // namespace Log
// Caution: macro magery.
#define debugDump(log, x) \
if(log < Log::gLevel) { \
std::ostringstream debugDump_theOutput; \
debugDump_theOutput << __FILE__ << ":" << __LINE__ << "> " << x <<
std::endl; \
gLoggingFacility.logMessage(debugDump_theOutput.str()); \
}
void Foo(const string& inName) {
debugDump(kLogInfo, "Hi" << " " << "there" << " " << inName);
}
Happy debugging!
--Eljay
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2001-10-29 8:29 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-29 7:18 Preprocessor macro question Joe MacDonald
2001-10-29 8:29 ` John Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).