Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how could I write this cpp macro

Reply
Thread Tools

how could I write this cpp macro

 
 
Bilgehan.Balban@gmail.com
Guest
Posts: n/a
 
      08-31-2006
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

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      08-31-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
L7
Guest
Posts: n/a
 
      08-31-2006
(E-Mail Removed) 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


 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      08-31-2006
Ben Pfaff wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-31-2006


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.

--
(E-Mail Removed)

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-31-2006
"L7" <(E-Mail Removed)> writes:

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


That's GCC-specific.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Free online test in C, CPP / Placement papers / CPP,C Interview Questions www.hitechskill.com C++ 0 04-09-2006 10:53 AM
when i compile the cpp file(cmdargs.cpp) int main(int argc, wchar_t* argv[]) Vinu C++ 9 05-05-2005 04:11 AM
Method inlined in source1.cpp and called in source2.cpp Alex Vinokur C++ 7 11-15-2004 09:14 PM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM



Advertisments