#define UNUSED(x) (void)(x) #define NELEMS(x) (sizeof(x) / sizeof((x)[0])) #define PI 3.14159265358979323846 #define RAD(a) ((a) * PI / 180) #define DEG(a) ((a) * 180 / PI) // typedef char bit; // Largely inspired by "Zed's Awesome Debug Macros" // From http://c.learncodethehardway.org/book/ex20.html #define BEGIN do { #define END } while (0) #define EPRINT(...) fprintf(stderr, __VA_ARGS__) #ifdef NDEBUG #define DEBUG(M, ...) #define DEBUG_LOC_FMT #define DEBUG_LOC_VALS #define DEBUG_LEVEL(X) #else #define DEBUG(M, ...) EPRINT("[DEBUG] %s:%d %s: " M "\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__) #define DEBUG_LOC_FMT "%s:%d %s: " #define DEBUG_LOC_VALS , __FILE__, __LINE__, __func__ #define DEBUG_LEVEL(X) X #endif #define STRERROR() (errno == 0 ? "" : ": "), (errno == 0 ? "" : strerror(errno)) #define ERROR1(M, ...) EPRINT(DEBUG_LEVEL("[ERROR] ") DEBUG_LOC_FMT M "\n" DEBUG_LOC_VALS, ##__VA_ARGS__) #define WARN1(M, ...) EPRINT(DEBUG_LEVEL("[WARN] ") DEBUG_LOC_FMT M "\n" DEBUG_LOC_VALS, ##__VA_ARGS__) #define CHECK1(A, M, ...) BEGIN if (!(A)) { ERROR1(M, ##__VA_ARGS__); goto error; } END #define CERROR(M, ...) EPRINT(DEBUG_LEVEL("[ERROR] ") DEBUG_LOC_FMT M "%s%s\n" DEBUG_LOC_VALS, ##__VA_ARGS__, STRERROR()) #define WARN(M, ...) EPRINT(DEBUG_LEVEL("[WARN] ") DEBUG_LOC_FMT M "%s%s\n" DEBUG_LOC_VALS, ##__VA_ARGS__, STRERROR()) #define INFO(M, ...) EPRINT(DEBUG_LEVEL("[INFO] ") DEBUG_LOC_FMT M "\n" DEBUG_LOC_VALS, ##__VA_ARGS__) #define CHECK(A, M, ...) BEGIN if (!(A)) { CERROR(M, ##__VA_ARGS__); errno = 0; goto error; } END // #define SENTINEL(M, ...) BEGIN CERROR(M, ##__VA_ARGS__); errno=0; goto error; END // #define CHECK_DEBUG(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }