Skip to content

Instantly share code, notes, and snippets.

@mgarg1
Last active December 6, 2017 08:03
Show Gist options
  • Save mgarg1/68de1c706ee566f3bcec to your computer and use it in GitHub Desktop.
Save mgarg1/68de1c706ee566f3bcec to your computer and use it in GitHub Desktop.
Some of the debugging utilities
#ifdef assert
#undef assert
#endif
#define assert(expr,i) \
if (!(expr)) { \
std::cerr << "Error at line " << __LINE__ << ": " \
<< "assert(" #expr ");" <<"value of i: "<<i<< std::endl; \
exit(EXIT_FAILURE); \
}
#define dbg(fmt, ...) \
fprintf(stderr, "%s[%d]: " fmt "\n", __FILE__, __LINE__, ## __VA_ARGS__)
/*----------another utility---------------*/
__PRETTY_FUNCTION__
/*----------chrome console like utility---------------*/
/* FOR DEBUGGING PURPOSE ONLY */
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
const char *lastFile;
static void printFuncLineNum(const char *fileName, const size_t lineNum){
static int lastCount = 0;
static size_t lastLine = 0;
if(lastFile == fileName && lineNum == lastLine){
fprintf(stderr, "\r" ANSI_COLOR_CYAN "(%d):" ANSI_COLOR_RESET,lastCount++);
}
else{
fprintf(stderr, "\n");
lastCount = 0;
lastFile = fileName;
lastLine = lineNum;
}
fprintf(stderr, ANSI_COLOR_YELLOW "%s[%zu]:" ANSI_COLOR_RESET, fileName, lineNum);
}
#define dbg(fmt, ...) \
do{ \
printFuncLineNum(__FILE__, __LINE__); \
fprintf(stderr,fmt,## __VA_ARGS__); \
}while(0)
/* FOR DEBUGGING PURPOSE ONLY */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment