help needed in macro syntax

  • Thread starter Raghuveer Pallikonda
  • Start date
R

Raghuveer Pallikonda

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
 
M

Mike Wahler

Raghuveer Pallikonda said:
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
 
D

David Hilsee

Raghuveer Pallikonda said:
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.
 
?

=?ISO-8859-1?Q?Tobias_G=FCntner?=

Raghuveer said:
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?)
 
J

Jerry Coffin

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.
 
R

Raghuveer Pallikonda

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
 
V

Victor Bazarov

Raghuveer said:
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
 

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

Similar Threads

help in constructor needed 5
Boost Logging Lib, v2 6
Problem with creating FileHandler 0
Enhancing the Gateway (Help Needed) 24
help needed in c++ 1
[ANN] main-4.0.0 (for avdi) 0
ANN main-4.4.0 0
[ANN] main-2.8.3 2

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top