C99, CPP vararg macros to add file and line number for debug,trace

29 Nov 2008

Excuse the horrible title, it's every keyword I can think of that describes this.

Here's the problem: You want to define a macro for logging, debugging or tracing that adds file and line number to a printf-style function, for instance LOGIT("The count is %d", count); to "output" [Myfile.c:123] The count is 123"

I forgot the syntax for vararg macros in C99/CPP, so did a search and got back horrible bloated answers that involved new C++ classes or things that use fixed size buffers. The simple way is to do something like this:

#define SYSLOG_TRACE(FMT, ARGS...) syslog(LOG_DEBUG, "[%s:%d] " FMT, __FILE__, __LINE__, ##ARGS)
#define LOG_STDERR(FMT,ARGS...) fprintf(stderr, "[%s:%d] " FMT, __FILE__, __LINE__, ##ARGS)

Some quick notes: Recall, if two quoted strings are next to each such as "a" "b" is considered to be one single string "ab". I think that is standard C. The only GCC-ism is the ## which means "if there are no args, then remove that comma beforehand". If you aren't using GCC, I think you just need to define two macros, one with args, and one without.

Enjoy