printf()-like macro in C90

P

pinkfloydhomer

I want to have debugging macro or function that I can use like:

DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

just as with printf. I want to avoid implementing this functionality
myself with varargs etc.

Now, in my case, I cannot use printf etc to output with. I have to use
some other function that can only take a string. Let's call it
output(char* str). Today, when debugging, I insert something like:

{
char buffer[200];
sprintf(buffer, "bla bla bla %d bla %s bla bla %d %d", 1, "42", 3,
4);
output(buffer);
}

This is tedious. I want to just be able to do

DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

or something similar. But I can't just make the above piece of code
into a macro. I thought about doing something like adding extra
parentheses like:

DEBUG(("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4));

so that the macro receives the entire parenthesis as one argument, but
then I can't just call sprintf with that argument...

How do I solve this problem?

/David
 
S

Simon Biber

I want to have debugging macro or function that I can use like:

DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

just as with printf. I want to avoid implementing this functionality
myself with varargs etc.
....

How do I solve this problem?

Variable arguments are not that scary. Your function need only be six
lines long!

#include <stdio.h>
#include <stdarg.h>

void DEBUG(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
char buffer[200];
vsprintf(buffer, fmt, va);
output(buffer);
va_end(va);
}

All of the hard work was passed off to vsprintf, which is a version of
the sprintf function that operates on a user-supplied va_list object.
It's ready-made for situations like this!
 
S

Simon Biber

Simon said:
I want to have debugging macro or function that I can use like:

DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

just as with printf. I want to avoid implementing this functionality
myself with varargs etc.
...


How do I solve this problem?


Variable arguments are not that scary. Your function need only be six
lines long!

#include <stdio.h>
#include <stdarg.h>

void DEBUG(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
char buffer[200];
vsprintf(buffer, fmt, va);
output(buffer);
va_end(va);
}

Replying to myself. Sorry, for C90 you will need to swap the
va_start(va, fmt); and char buffer[200]; lines above, as all
declarations must come before other statements.
 
C

Chris Hulbert

Simon said:
Simon said:
I want to have debugging macro or function that I can use like:

DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

just as with printf. I want to avoid implementing this functionality
myself with varargs etc.
...


How do I solve this problem?


Variable arguments are not that scary. Your function need only be six
lines long!

#include <stdio.h>
#include <stdarg.h>

void DEBUG(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
char buffer[200];
vsprintf(buffer, fmt, va);
output(buffer);
va_end(va);
}

Replying to myself. Sorry, for C90 you will need to swap the
va_start(va, fmt); and char buffer[200]; lines above, as all
declarations must come before other statements.

You should use vsnprintf instead if available (in the C99 standard).
Calling vsnprintf with available size 0 will return the number of
characters written if n was suffieciently large enough (which you can
use to allocate the proper amount required, or use the static buffer if
it's large enough). 7.19.6.12 in C99. You can find implementations of
vsnprintf on the internet if your C library doesn't have it already.
 
J

Jordan Abel

I want to have debugging macro or function that I can use like:

DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

just as with printf. I want to avoid implementing this functionality
myself with varargs etc.

Now, in my case, I cannot use printf etc to output with. I have to use
some other function that can only take a string. Let's call it
output(char* str). Today, when debugging, I insert something like:

{
char buffer[200];
sprintf(buffer, "bla bla bla %d bla %s bla bla %d %d", 1, "42", 3,
4);
output(buffer);
}

This is tedious. I want to just be able to do

DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

or something similar. But I can't just make the above piece of code
into a macro. I thought about doing something like adding extra
parentheses like:

DEBUG(("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4));

so that the macro receives the entire parenthesis as one argument, but
then I can't just call sprintf with that argument...

How do I solve this problem?

/David

This requires c99.

#define DEBUG(format,...) do {\
char *buf; size_t len;\
len=snprintf(NULL,0,format,__VA_ARGS__);\
buf=malloc(len+1);\
if(buf) {\
snprintf(buf,len+1,format,__VA_ARGS__);\
output(buf);\
free(buf);\
}\
} while(0)

writing a function is starting to look attractive, isn't it?
 
P

pete

Jordan Abel wrote:
This requires c99.

#define DEBUG(format,...) do {\
char *buf; size_t len;\
writing a function is starting to look attractive, isn't it?

When your macro starts defining objects, then it's
time to at least give some thought to writing a function.
 
Z

Zoran Cutura

Simon Biber said:
I want to have debugging macro or function that I can use like:

DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

just as with printf. I want to avoid implementing this functionality
myself with varargs etc.
...

How do I solve this problem?

Variable arguments are not that scary. Your function need only be six
lines long!

#include <stdio.h>
#include <stdarg.h>

void DEBUG(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
char buffer[200];
vsprintf(buffer, fmt, va);
output(buffer);
va_end(va);
}

All of the hard work was passed off to vsprintf, which is a version of
the sprintf function that operates on a user-supplied va_list object.
It's ready-made for situations like this!

One problem when using a function instead of a macro is, that in that
function you cannot hide __LINE__ and __FILE__ usage or other macros,
which is very helpful espesially with DEBUG implementations.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top