A helpful macro to debug
August 1st, 2006
This morning I’ve been working on a project I’m developing enterely by my own. It is codenamed “ctempl” it will (would?) be a new CMS enterely written in C. It features a multithreaded server, (will) works under apache or every web server that supports cgi executables and will have (a day, I hope) a gtk GUI as admin interface.
What I’ve been trying to do this morning is implementing a simple debug macro, something that works out in a similar way to printf. So that’s what I did:
#define PDEBUG(format, ...) {
fprintf (stderr, "debug:[%s=%s:%d] "format,
__FILE__, __FUNCTION__, __LINE__,
## __VA_ARGS__);
}
The only thing that hurts is that this macro will compile only with the GNU c compiler (gcc, in poorly words). But, at least now, it is enough to me since for a long time I believe I will use only this compiler :)
The effect of this macro is the following. Let suppose we want to print the address of an integer pointer i:
int *i = (int *)malloc(sizeof(int));
PDEBUG("i has address %pn", i);
Produces:
debug:[main.c=main:14] i has address 0x804b210
Nice, isn’t it?
P.S.: I’m looking for developers who join this project, if you are interested drop me a mail to kratorius at gmail dot com, ok?