Disable macro - Programmer

This is a discussion on Disable macro - Programmer ; I have a macro that allows a variable number of arguments and it works fine. I also to be able to completely disable the function call by putting a comment in front of the line. However the #define LOG // ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Disable macro

  1. Disable macro

    I have a macro that allows a variable number of arguments and it works fine.
    I also to be able to completely disable the function call by putting a
    comment in front of the line.
    However the #define LOG // does not work as the compiler takes it for a
    "real" comment.
    Neither works #define LOG #pragma comment

    Someone got a solution for this?

    Regards
    Peter

    #undef LOGGING
    #if defined LOGGING
    #define LOG printf(
    #else
    #define LOG //
    #endif

    void main()
    {
    char *str = "test";

    LOG "this is 'str' %s\n", str);
    }



  2. Re: Disable macro

    There are a couple of ways of doing this.

    One way:
    #if defined(LOGGING)
    #define LOG printf
    #else
    int donothing(const char*, ...)
    {
    return 0;
    }
    #define LOG donothing
    #endif
    LOG("hello %s", "world");

    Another way:
    #if defined(LOGGING)
    #define LOG(args) printf args
    #else
    #define LOG(args)
    #endif

    LOG(("hello %s", "world"));

    --
    Tim Robinson (MVP, Windows SDK)
    http://www.themobius.co.uk/

    "Peak" wrote in message
    news:ZfGzb.3$zf2.61@news1.global-one.fi...
    > I have a macro that allows a variable number of arguments and it works

    fine.
    > I also to be able to completely disable the function call by putting a
    > comment in front of the line.
    > However the #define LOG // does not work as the compiler takes it for a
    > "real" comment.
    > Neither works #define LOG #pragma comment
    >
    > Someone got a solution for this?
    >
    > Regards
    > Peter
    >
    > #undef LOGGING
    > #if defined LOGGING
    > #define LOG printf(
    > #else
    > #define LOG //
    > #endif
    >
    > void main()
    > {
    > char *str = "test";
    >
    > LOG "this is 'str' %s\n", str);
    > }
    >
    >




  3. Re: Disable macro

    Thanks Tim, the second one was nice. In pursuing my original idea of having
    the macro commented out I stumbled on this; LOG /##/, works too.

    Again, thanks
    Peter

    "Tim Robinson" wrote in message
    news:bqo19h$255l6m$3@ID-103400.news.uni-berlin.de...
    > There are a couple of ways of doing this.
    >
    > One way:
    > #if defined(LOGGING)
    > #define LOG printf
    > #else
    > int donothing(const char*, ...)
    > {
    > return 0;
    > }
    > #define LOG donothing
    > #endif
    > LOG("hello %s", "world");
    >
    > Another way:
    > #if defined(LOGGING)
    > #define LOG(args) printf args
    > #else
    > #define LOG(args)
    > #endif
    >
    > LOG(("hello %s", "world"));
    >
    > --
    > Tim Robinson (MVP, Windows SDK)
    > http://www.themobius.co.uk/
    >
    > "Peak" wrote in message
    > news:ZfGzb.3$zf2.61@news1.global-one.fi...
    > > I have a macro that allows a variable number of arguments and it works

    > fine.
    > > I also to be able to completely disable the function call by putting a
    > > comment in front of the line.
    > > However the #define LOG // does not work as the compiler takes it for a
    > > "real" comment.
    > > Neither works #define LOG #pragma comment
    > >
    > > Someone got a solution for this?
    > >
    > > Regards
    > > Peter
    > >
    > > #undef LOGGING
    > > #if defined LOGGING
    > > #define LOG printf(
    > > #else
    > > #define LOG //
    > > #endif
    > >
    > > void main()
    > > {
    > > char *str = "test";
    > >
    > > LOG "this is 'str' %s\n", str);
    > > }
    > >
    > >

    >
    >




+ Reply to Thread