help needed in macro syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raghuveer Pallikonda

    help needed in macro syntax

    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::Enable d() ? 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::Enable d() ? false : Logger::LogMsg

    Thank you in advance for help.
    --Raghu
  • David Hilsee

    #2
    Re: help needed in macro syntax

    "Raghuveer Pallikonda" <pallikon@redif fmail.com> wrote in message
    news:81b7d650.0 408251431.30d6b dd1@posting.goo gle.com...[color=blue]
    > 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::Enable d() ? 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::Enable d() ? false : Logger::LogMsg
    >
    > Thank you in advance for help.[/color]

    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::Enable d()) 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


    Comment

    • Mike Wahler

      #3
      Re: help needed in macro syntax


      "Raghuveer Pallikonda" <pallikon@redif fmail.com> wrote in message
      news:81b7d650.0 408251431.30d6b dd1@posting.goo gle.com...[color=blue]
      > 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::Enable d() ? false : Logger::LogMsg
      >
      > so that
      > LOGMSG("Log Me\n");[/color]

      So where are your macro parameters?

      -Mike


      Comment

      • Tobias Güntner

        #4
        Re: help needed in macro syntax

        Raghuveer Pallikonda wrote:[color=blue]
        > My compilation fails with errors (g++ 3.3 compiler).[/color]

        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

        Comment

        • Jerry Coffin

          #5
          Re: help needed in macro syntax

          pallikon@rediff mail.com (Raghuveer Pallikonda) wrote in message news:<81b7d650. 0408251431.30d6 bdd1@posting.go ogle.com>...[color=blue]
          > 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::Enable d() ? false : Logger::LogMsg
          >
          > so that
          > LOGMSG("Log Me\n");[/color]

          Hmm...how about:

          #define LOGMSG(MSG) (Logger::Enable d() && 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.

          Comment

          • Raghuveer Pallikonda

            #6
            Re: help needed in macro syntax

            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

            pallikon@rediff mail.com (Raghuveer Pallikonda) wrote in message news:<81b7d650. 0408251431.30d6 bdd1@posting.go ogle.com>...[color=blue]
            > 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::Enable d() ? 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::Enable d() ? false : Logger::LogMsg
            >
            > Thank you in advance for help.
            > --Raghu[/color]

            Comment

            • Victor Bazarov

              #7
              Re: help needed in macro syntax

              Raghuveer Pallikonda wrote:[color=blue]
              > 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)[/color]

              Well, just substitute the 'IDIAG' token with what the macro will
              expand into:
              [color=blue]
              >
              > #include <iostream>
              >
              > #define IDIAG !LogEnabled() ? false : printf
              >
              > bool LogEnabled() { return false; }
              >
              > int main() {
              > IDIAG("this is to be logged\n");[/color]

              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");
              [color=blue]
              > return 0;
              > }
              >
              >
              > and not this
              >
              > #include <iostream>
              >
              > #define IDIAG false
              >
              > int main() {
              > IDIAG("this is to be logged\n");[/color]

              And what this expands into?

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

              Nonsense, isn't it?
              [color=blue]
              > 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");[/color]

              Nope.
              [color=blue]
              >
              > I guess my understanding is incorrect.. so would greatly appreciate to
              > be corrected and enlightened,[/color]

              See above

              Victor

              Comment

              Working...