A tiny little macro to help debug your C code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// debugf.h | |
// Charlton Trezevant - 2018 | |
// MIT license | |
/* USAGE: | |
* This is a leveled version of debugf | |
* Unfamiliar with leveled logging? Check out http://thejoyofcode.com/Logging_Levels_and_how_to_use_them.aspx | |
* for some background. | |
* Example call: | |
* debugf(DEBUG_LEVEL_INFO, "Hi! The magic number is %d", 5); | |
* Enjoy! | |
*/ | |
// You can define your own custom debug levels, or use these defaults | |
#define DEBUG_LEVEL_ALL 0 | |
#define DEBUG_LEVEL_TRACE 1 | |
#define DEBUG_LEVEL_DEBUG 2 | |
#define DEBUG_LEVEL_INFO 3 | |
#define DEBUG_LEVEL_WARN 4 | |
#define DEBUG_LEVEL_ERROR 5 | |
#define DEBUG_LEVEL_FATAL 6 | |
#define DEBUG_LEVEL_NONE 99 | |
// Comment out the below or set DEBUG_LEVEL_NONE to disable debug output. | |
// When not disabled, only calls to debugf with a level that's greater than or equal to | |
// the current global debug level will produce output. | |
//#define DEBUG DEBUG_LEVEL_NONE | |
#ifdef DEBUG | |
#define debugf(lvl, fmt, ...) \ | |
({ \ | |
if (DEBUG == 0 || (lvl) >= DEBUG) { \ | |
fprintf(stderr, fmt, ## __VA_ARGS__); fflush(stderr); \ | |
} \ | |
}) | |
#else | |
#define debugf(lvl, fmt, ...) ((void)0) | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Panic is called when something goes wrong | |
void panic(const char * fmt, ...){ | |
// Vargs to behave like printf (but not to make black metal) | |
va_list vargs; | |
va_start(vargs, fmt); | |
// Print error message to stderr | |
vfprintf(stderr, fmt, vargs); | |
fflush(stderr); | |
va_end(vargs); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment