4/9 Right now I have tons of snippets like
sprintf(errmsg, "Error %d: invalid type %d at %d\n", 1, myType, time);
QueueError(errmsg);
Is it possible to define a macro to take an arbitrary # of arguments--
#define ERROR(x) sprintf(globErrMsg, x); QueueError(globErrMsg);
Or will the x in the macro just take everything up to the first comma?
\_ #define ERR(args...) {sprintf(g_errMsg, args);QueueError(g_errMsg);}
\_ Yes, if you use recent gcc or other C99-ish compilers, no for
older ones.
\_ A reliable method for doing this is to write a function that
uses a va_list and vsnprintf:
#include <stdarg.h>
void Error (char *err, size_t sz, const char *fmt, ...) {
va_list vl;
if (err == null || sz <= 0)
return;
memset(err,'\0',sz);
va_start(vl,fmt);
vsnprintf(err,sz,fmt,vl);
va_end(vl);
QueueError(err);
}
BTW, You should really avoid sprintf since it doesn't have a
good way to check thesize of the input buffer.
\_ thanks for the code, I'll give that a try today. One question--
this is a real-time application, so there are a few common stdio
calls I use regularly and the rest are viewed warily. Are there
any non-constant performance implications for using vsnprintf?
I can handle a +foo hit, but foo*strlen(err) might be pushing it.
thanks.
\_ that only matters if the content of your variables comes from
input.
\_ thanks for the code, I'll give that a try today. One
question-- this is a real-time application, so there are
a few common stdio calls I use regularly and the rest
are viewed warily. Are there any non-constant
performance implications for using vsnprintf? I can
handle a +foo hit, but foo*strlen(err) might be pushing
it. thanks.
\_ vsnprintf calls vfprintf internally. In most cases
sprintf also calls vfprintf internally. The same
non-constant performance issues you may have with
your implementation of sprintf will most likely
apply to your implementation of vsnprintf.
\_ that only matters if the content of your variables comes
from input.
\_ Program defensively. |