/* errlog.h - Debug only macros */ * PUBLIC DOMAIN - Jon Mayo - Dec 2006 * initial: 2006-12-29 * * DIE() - ends the program with a message * PERROR() * PERROR_SOCKET() - displays errors related to sockets * ASSERT() * VERBOSE() * DEBUG() does nothing if NDEBUG is defined * TRACE() does nothing if NTRACE is defined * * TODO: * + use FormatMessage in the windows version of PERROR_SOCKET * + have a different PERROR_SOCKET if NDEBUG defined */ #ifndef ERRLOG_H #define ERRLOG_H #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 version */ # define ERRLOG_PROLOGUE_FORMAT "%s:%d:%s():" # define ERRLOG_PROLOGUE_ARGS __FILE__, __LINE__, __func__ #else /* ANSI/C89 version */ # define ERRLOG_PROLOGUE_FORMAT "%s:%d:" # define ERRLOG_PROLOGUE_ARGS __FILE__, __LINE__ #endif /* this is used to prefix messages */ #define ERRLOG_PROLOGUE(tag) errlog(tag ERRLOG_PROLOGUE_FORMAT, ERRLOG_PROLOGUE_ARGS) #define VERBOSE(x) do { if(errlog_level) errlog x; } while(0) #define BUG(x, reason) do { if((x)) { errlog(ERRLOG_PROLOGUE_FORMAT "BUG:"#x":%s\n" , ERRLOG_PROLOGUE_ARGS, (reason)); } } while(0) #ifdef NDEBUG # define DEBUG(x) /* DEBUG disabled */ # define PERROR(reason) perror(reason) /* normal perror */ #define ASSERT(x) /* ASSERT disabled */ #else /* headers needed for debugging */ # include # include /* DEBUG() macro enabled */ # define DEBUG(x) do { ERRLOG_PROLOGUE(""); errlog x; } while(0) /* PERROR() is a replacement for perror() that displays extra information */ # define PERROR(reason) errlog(ERRLOG_PROLOGUE_FORMAT "%s:%s (%d)\n", ERRLOG_PROLOGUE_ARGS, (reason), strerror(errno), errno) #define ASSERT(x) do { if(!(x)) { errlog(ERRLOG_PROLOGUE_FORMAT "ASSERT:"#x"\n" , ERRLOG_PROLOGUE_ARGS); abort(); } } while(0) #endif #ifdef __MINGW32__ /* special version of PERROR just for socket errors */ # define PERROR_SOCKET(reason) errlog(ERRLOG_PROLOGUE_FORMAT "%s:socket error 0x%X\n",ERRLOG_PROLOGUE_ARGS, (reason), WSAGetLastError()) #else # define PERROR_SOCKET(reason) errlog(ERRLOG_PROLOGUE_FORMAT "%s:%s (%d)\n", ERRLOG_PROLOGUE_ARGS, (reason), strerror(errno), errno) #endif # ifdef NTRACE # define TRACE(x) /* TRACE disabled */ # else # define TRACE(x) do { ERRLOG_PROLOGUE(""); errlog x; } while(0) # endif /* print a message and die */ #define DIE(reason) do { PERROR(reason); exit(EXIT_FAILURE); } while(0) extern int errlog_level; /* current logging level */ void errlog(const char *fmt, ...); #endif /* vim: ts=4 sts=0 noet sw=4 */