how could I write this cpp macro

Discussion in 'C Programming' started by Bilgehan.Balban@gmail.com, Aug 31, 2006.

  1. Guest

    Hi,

    I'm trying to convert this:

    printf("format str %s, %s", "str arg 1", "str arg 2 etc.");

    to this:

    printf("%s: " "format str %s, %s", __FUNCTION__, "str arg 1", "str arg2
    etc.");

    All sol'ns I came up had various problems in various printf formats.
    The closest I had was something like:

    #if DEBUG > 0
    #define pdebug(fmt ...) printf("%s: " fmt, \
    __FUNCTION__, __VA_ARGS__); \
    #endif

    But this wont work if there's a printf with no va_args, such as:

    printf("fmt string and nothing else\n");

    because the comma after __FUNCTION__ will be a syntax error.

    Any ideas?

    Thanks,
    Bahadir
     
    , Aug 31, 2006
    #1
    1. Advertising

  2. Ben Pfaff Guest

    writes:

    > I'm trying to convert this:
    >
    > printf("format str %s, %s", "str arg 1", "str arg 2 etc.");
    >
    > to this:
    >
    > printf("%s: " "format str %s, %s", __FUNCTION__, "str arg 1", "str arg2
    > etc.");


    It's ghastly, but
    #define pdebug printf("%s:", __FUNCTION__), printf
    might do what you want.

    There are probably C99-specific solutions, and definitely
    GCC-specific solutions, as well.
    --
    Just another C hacker.
     
    Ben Pfaff, Aug 31, 2006
    #2
    1. Advertising

  3. L7 Guest

    wrote:
    > Hi,
    >
    > I'm trying to convert this:
    >
    > printf("format str %s, %s", "str arg 1", "str arg 2 etc.");
    >
    > to this:
    >
    > printf("%s: " "format str %s, %s", __FUNCTION__, "str arg 1", "str arg2
    > etc.");
    >
    > All sol'ns I came up had various problems in various printf formats.
    > The closest I had was something like:
    >
    > #if DEBUG > 0
    > #define pdebug(fmt ...) printf("%s: " fmt, \
    > __FUNCTION__, __VA_ARGS__); \
    > #endif
    >
    > But this wont work if there's a printf with no va_args, such as:
    >
    > printf("fmt string and nothing else\n");
    >
    > because the comma after __FUNCTION__ will be a syntax error.
    >
    > Any ideas?
    >


    #define pdebug(args...) do { \
    printf ("%s:", __FUNCTION__); \
    printf (args); \
    } while (0);

    Should do what you want.

    > Thanks,
    > Bahadir
     
    L7, Aug 31, 2006
    #3
  4. Ben Pfaff wrote:
    > writes:
    >
    > > I'm trying to convert this:
    > >
    > > printf("format str %s, %s", "str arg 1", "str arg 2 etc.");
    > >
    > > to this:
    > >
    > > printf("%s: " "format str %s, %s", __FUNCTION__, "str arg 1", "str arg2
    > > etc.");

    >
    > It's ghastly, but
    > #define pdebug printf("%s:", __FUNCTION__), printf
    > might do what you want.
    >
    > There are probably C99-specific solutions, and definitely
    > GCC-specific solutions, as well.


    I think Ben's solution or a variant of it may be what you want. Your
    method of concatenating string literals precludes people from using a
    variable as the format string, not ideal if this is to be widely used.
    e.g.

    const char *format = "%s %d %s";
    pdebug(format, foo, bar, bas);

    would break.

    -David
     
    David Resnick, Aug 31, 2006
    #4
  5. Eric Sosman Guest

    L7 wrote On 08/31/06 13:58,:
    >
    > #define pdebug(args...) do { \
    > printf ("%s:", __FUNCTION__); \
    > printf (args); \
    > } while (0);
    >
    > Should do what you want.


    Lose the final semicolon.

    --
     
    Eric Sosman, Aug 31, 2006
    #5
  6. Ben Pfaff Guest

    "L7" <> writes:

    > #define pdebug(args...) do { \
    > printf ("%s:", __FUNCTION__); \
    > printf (args); \
    > } while (0);


    That's GCC-specific.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Aug 31, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Artie Gold

    macro usage - cpp faq example

    Artie Gold, Aug 16, 2003, in forum: C++
    Replies:
    1
    Views:
    575
    =?UTF-8?b?IuCkhuCksuCli+CklSDgpJXgpYHgpK7gpL7gpLAi
    Aug 16, 2003
  2. DrUg13
    Replies:
    1
    Views:
    496
    DrUg13
    Feb 10, 2004
  3. Alex Vinokur
    Replies:
    7
    Views:
    428
    Greg Comeau
    Nov 15, 2004
  4. Vinu
    Replies:
    9
    Views:
    683
  5. www.hitechskill.com
    Replies:
    0
    Views:
    1,392
    www.hitechskill.com
    Apr 9, 2006
Loading...

Share This Page