Skip to content

Instantly share code, notes, and snippets.

@shengyu7697
Last active March 18, 2018 03:13
Embed
What would you like to do?
debug print
#define DEBUG 4
#if defined(DEBUG)
#if DEBUG >= 4 // filename:function:line
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, "[DEBUG] %s:%s():%d " fmt, \
__FILE__, __func__, __LINE__, ##__VA_ARGS__)
#elif DEBUG >= 3 // filename:function
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, "[DEBUG] %s:%s() " fmt, \
__FILE__, __func__, ##__VA_ARGS__)
#elif DEBUG >= 2 // filename:line
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, "[DEBUG] %s:%d " fmt, \
__FILE__, __LINE__, __func__, ##__VA_ARGS__)
#elif DEBUG >= 1 // function:line
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, "[DEBUG] %s():%d " fmt, \
__func__, __LINE__, ##__VA_ARGS__)
#endif
#else
#define DEBUG_PRINT(fmt, ...) // Don't do anything in release builds
#endif
DEBUG_PRINT("Debugging is enabled.\n");
DEBUG_PRINT("Debug level: %d", (int) DEBUG);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment