--- /-\ wrote:
> In an attempt to do varargs ok() without the magical __VA_ARGS__,
> I've come up with two little example solutions shown below.
> Improvements welcome.
Sorry, but curiosity got the better of me and I took a look at how
the very widely used and portable C++ ACE library does it.
The version below is very loosely based on the ACE library
ACE_ERROR() macro.
Note the funky (( and )) macro hackery in the ok's below. This is
almost as much fun as Perl golf. :-)
AFAICT, in the ACE version, instead of gr_save() below it calls a
setter method on a singleton "logger" object and instead of gr()
it calls the log method of the said singleton logger object.
/* Version very loosely based on ACE C++ library ACE_ERROR() --------- */
#include <stdio.h>
#include <stdarg.h>
#define ok(X) \
do { \
gr_save(__FILE__, __LINE__); \
gr X; \
} while (0)
static const char* g_file;
static unsigned long g_line;
void gr_save(const char* file, unsigned long line) {
g_file = file;
g_line = line;
}
void gr(int cond, const char* fmt, ...) {
va_list t;
printf("cond:%d file:%s line:%lu, ", cond, g_file, g_line);
va_start(t, fmt);
vprintf(fmt, t);
va_end(t);
}
int main() {
ok(( 1==1, "test %s %d\n", "one eq one", 42 ));
ok(( 0==1, "test %s %d\n", "zero eq one", 43 ));
return 0;
}
/* ------------------------------------------------------------------- */
/-\
Find local movie times and trailers on Yahoo! Movies.
http://au.movies.yahoo.com