By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

help needed in macro syntax

P: n/a
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");

will be a NO-OP if the Logger::Enabled() returns false, else the
message will be logged appropriately by the function call to
Logger::LogMsg().

My compilation fails with errors (g++ 3.3 compiler). I have seen
similar macro definition in another working program. Should I be
passing some compile options to g++ compiler to get this working?

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

Thank you in advance for help.
--Raghu
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Raghuveer Pallikonda" <pa******@rediffmail.com> wrote in message
news:81**************************@posting.google.c om...
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");

will be a NO-OP if the Logger::Enabled() returns false, else the
message will be logged appropriately by the function call to
Logger::LogMsg().

My compilation fails with errors (g++ 3.3 compiler). I have seen
similar macro definition in another working program. Should I be
passing some compile options to g++ compiler to get this working?

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

Thank you in advance for help.


Why are macros necessary? Can't you just add that call to Logger::Enabled()
to Logger::LogMsg()?

If you need the macro, then wouldn't you want something like this?

#define LOGMSG(x) if (Logger::Enabled()) Logger::LogMsg((x));

I'm not a heavy macro user, so that may not be the best way to do it. Is
that ternary operator necessary? BTW, if you want better help, you should
post complete code that demonstrates the problem, and include the compiler's
messages in your post.

--
David Hilsee
Jul 22 '05 #2

P: n/a

"Raghuveer Pallikonda" <pa******@rediffmail.com> wrote in message
news:81**************************@posting.google.c om...
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");


So where are your macro parameters?

-Mike
Jul 22 '05 #3

P: n/a
Raghuveer Pallikonda wrote:
My compilation fails with errors (g++ 3.3 compiler).


What errors do you get? How is Logger::LogMsg defined?
Does your compiler complain because it cannot determine the correct
return type for the ?: operator? (e.g. the return type of Logger::LogMsg
is void?)

--
Regards,
Tobias
Jul 22 '05 #4

P: n/a
pa******@rediffmail.com (Raghuveer Pallikonda) wrote in message news:<81**************************@posting.google. com>...
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");


Hmm...how about:

#define LOGMSG(MSG) (Logger::Enabled() && Logger::LogMsg(MSG))

I'm feeling too lazy at the moment to put together enough code to test
it with, but if at least if I'm understanding your intent correctly, I
believe it should work.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #5

P: n/a
I apologize for posting in haste. The OP syntax compiles fine. I am
still a bit puzzled as to why the OP syntax works ( example code
below)

#include <iostream>

#define IDIAG !LogEnabled() ? false : printf

bool LogEnabled() { return false; }

int main() {
IDIAG("this is to be logged\n");
return 0;
}
and not this

#include <iostream>

#define IDIAG false

int main() {
IDIAG("this is to be logged\n");
return 0;
}
$ g++ -o testfalse testfalse.cc
testfalse.cc: In function `int main()':
testfalse.cc:6: error: `false' cannot be used as a function
My understanding of Macro definitions is that it is a inplace
substitution. So that in both examples above the line IDIAG("this is
to be logged\n"); is actually translated to false("this is to be
logged\n");

I guess my understanding is incorrect.. so would greatly appreciate to
be corrected and enlightened,

Thank you for all the replies and your valuable time.

Regards,
--Raghu

pa******@rediffmail.com (Raghuveer Pallikonda) wrote in message news:<81**************************@posting.google. com>...
Hi,
I am trying to stub out debug log messages in my application, if
the logging subsystem is not enabled.. For e.g a invocation

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

so that
LOGMSG("Log Me\n");

will be a NO-OP if the Logger::Enabled() returns false, else the
message will be logged appropriately by the function call to
Logger::LogMsg().

My compilation fails with errors (g++ 3.3 compiler). I have seen
similar macro definition in another working program. Should I be
passing some compile options to g++ compiler to get this working?

#define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg

Thank you in advance for help.
--Raghu

Jul 22 '05 #6

P: n/a
Raghuveer Pallikonda wrote:
I apologize for posting in haste. The OP syntax compiles fine. I am
still a bit puzzled as to why the OP syntax works ( example code
below)
Well, just substitute the 'IDIAG' token with what the macro will
expand into:

#include <iostream>

#define IDIAG !LogEnabled() ? false : printf

bool LogEnabled() { return false; }

int main() {
IDIAG("this is to be logged\n");
Expands into

!LogEnabled() ? false : printf ("this is to be logged\n");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ~~~~~~~~~~~~~~~~~~~~~~~~~
This is the actual substitution This was already there

(I put a space between the substitution and the parentheses for more
clarity) which is a statement that contains a single expression with
a ternary operator. Similar to

if (!LogEnabled()) false;
else printf("this is to be logged\n");
return 0;
}
and not this

#include <iostream>

#define IDIAG false

int main() {
IDIAG("this is to be logged\n");
And what this expands into?

false("this is to be logged\n");

Nonsense, isn't it?
return 0;
}
$ g++ -o testfalse testfalse.cc
testfalse.cc: In function `int main()':
testfalse.cc:6: error: `false' cannot be used as a function
My understanding of Macro definitions is that it is a inplace
substitution. So that in both examples above the line IDIAG("this is
to be logged\n"); is actually translated to false("this is to be
logged\n");
Nope.

I guess my understanding is incorrect.. so would greatly appreciate to
be corrected and enlightened,


See above

Victor
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.