Skip to content

Instantly share code, notes, and snippets.

@ctrezevant ctrezevant/debugf.h
Created Nov 29, 2018

Embed
What would you like to do?
A tiny little macro to help debug your C code.
// debugf.h
// Charlton Trezevant - 2018
// MIT license
/* USAGE:
* To use this macro, simply paste it into your source file (you may also
* include debugf.h if you have many source files). Doing this will define
* the function debugf(), which acts as a thin wrapper around fprintf() with
* a little added pizazz.
*
* When DEBUG is defined, any calls made to debugf() will behave in exactly the
* same way as calls to printf() or fprintf() (because there's full support for variadic
* arguments :). Output will be sent to stderr instead of stdout to ensure that any test
* cases relying on stdout will pass if debugging is enabled. Additionally, a call is made
* to fflush() after each line of debugging output, so crashes and segfaults wont cause
* you to lose precious debugging insights to the ether.
*
* Enjoy!
*/
// Comment out the below to disable debug output.
#define DEBUG
#ifdef DEBUG
#define debugf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__); fflush(stderr)
#else
#define debugf(fmt, ...) ((void)0)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.