Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > help needed in macro syntax

Reply
Thread Tools

help needed in macro syntax

 
 
Raghuveer Pallikonda
Guest
Posts: n/a
 
      08-25-2004
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      08-25-2004

"Raghuveer Pallikonda" <> wrote in message
news: 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


 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      08-25-2004
"Raghuveer Pallikonda" <> wrote in message
news: 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


 
Reply With Quote
 
=?ISO-8859-1?Q?Tobias_G=FCntner?=
Guest
Posts: n/a
 
      08-26-2004
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
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      08-26-2004
(Raghuveer Pallikonda) wrote in message news:<. 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.
 
Reply With Quote
 
Raghuveer Pallikonda
Guest
Posts: n/a
 
      08-26-2004
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

(Raghuveer Pallikonda) wrote in message news:<. 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-26-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dedicated Macro or Normal Macro? John Ortt Digital Photography 5 11-22-2005 12:43 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57