473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how could I write this cpp macro

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 '06 #1
5 1911
Bi*************@gmail.com 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.
Aug 31 '06 #2
L7
Bi*************@gmail.com 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
Aug 31 '06 #3
Ben Pfaff wrote:
Bi*************@gmail.com 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

Aug 31 '06 #4


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.

--
Er*********@sun.com

Aug 31 '06 #5
"L7" <je***********@gmail.comwrites:
#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;}
Aug 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Garibaldi | last post by:
Folks. This began today. I have many macros. Selecting a macro in the macro explorer then either double clicking on the macro or right clicking and selecting run no text is auto-typed into the...
2
by: Edgar | last post by:
I want to go from viewing a form (record number "x") to the same record ("x") in a report. Of course, when I open the report, it opens to the first record, not record "x". I don't know VBA,...
6
by: Clément Collin | last post by:
I working on a GIS project, with Access link which just need a little routine in VBA, but I haven't knowledges in VBA language. It's very simple, and it looks like that in a TPascal way : .......
12
by: Sweety | last post by:
plz reply, thanks in advance. bye,
27
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
7
by: fei.liu | last post by:
#define ONCFILE_ERR1(funcname, name) \ { \ #ifdef DEBUG\ cerr << __FILE__ << ":" << __LINE__ << " duplicated call to " << funcname << " " << name << endl; \ #endif \ } I want to have...
0
by: Oenone | last post by:
I have a VS2005 macro that runs in my BuildEvents.OnBuildDone event handler each time I build my solution. I'd like to get this macro to write to either the Output/Debug window or the Immediate...
2
by: raghunadhs | last post by:
Hi All, i have some simple scripts( which may have some insert statements, update statements etc). these scripts were written in Sql server 2000. now i want to use the same scripts in Access. (since...
3
by: thinktwice | last post by:
i want to define a macro like #define STRING2(x) #x #define STRING(x) STRING2(x) #define PROMPT(x) #pragma message(__FILE__ "(" STRING(__LINE__) "):" STRING(x)) <---- compile failed i...
5
by: Peng Yu | last post by:
Hi, I want a macro #expand that can be expanded to one of the following depending on its argument f(i1) f(i1, i2) f(i1, i2, i3) ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.