A helpful macro to debug

This morn­ing I’ve been work­ing on a project I’m devel­op­ing enterely by my own. It is code­named “ctempl” it will (would?) be a new CMS enterely writ­ten in C. It fea­tures a mul­ti­threaded server, (will) works under apache or every web server that sup­ports cgi exe­cuta­bles and will have (a day, I hope) a gtk GUI as admin interface.

What I’ve been trying to do this morn­ing is imple­ment­ing a simple debug macro, some­thing that works out in a sim­i­lar 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 com­pile only with the GNU c com­piler (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 fol­low­ing. Let sup­pose we want to print the address of an inte­ger pointer i:

int *i = (int *)malloc(sizeof(int));
PDEBUG("i has address %pn", i);

Pro­duces:

debug:[main.c=main:14] i has address 0x804b210

Nice, isn’t it?

P.S.: I’m look­ing for devel­op­ers who join this project, if you are inter­ested drop me a mail to kra­to­rius at gmail dot com, ok?

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">