Skip to content

Instantly share code, notes, and snippets.

@jlintz
Created September 4, 2011 04:19
Show Gist options
  • Save jlintz/1192247 to your computer and use it in GitHub Desktop.
Save jlintz/1192247 to your computer and use it in GitHub Desktop.
Useful C debug macro
#ifdef DEBUG
#define _DEBUG(fmt, args...) printf("%s:%s:%d: "fmt, __FILE__, __FUNCTION__, __LINE__, args)
#else
#define _DEBUG(fmt, args...)
#endif
@Cat22
Copy link

Cat22 commented Aug 24, 2018

You should put {} after the macro defined in the else clause
e.g #define _DEBUG(fmt, args...) {} so it wont mess with "if" statements
I needed to be able to turn debug on and off from a conf file and also #define it out at compile time sometimes, so i use this:

//#define DO_DEBUG
int debug; // set from conf file at runtime
#ifdef DO_DEBUG
#define debug_print(fmt, ...) do { if(debug == 1){plog(__FILE__, ___FUNCTION__, __LINE__, ((fmt)), ##__VA_ARGS__);} } while (0)
#else
#define debug_print(fmt, ...){}
#endif
plog is my logging function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment