src/debug.h

Go to the documentation of this file.
00001 #ifndef _FUMIGATE_
00002 #define _FUMIGATE_
00003 
00004 #include <time.h>
00005 #include <stdio.h>
00006 #include <errno.h>
00007 #include <sys/types.h>
00008 #include <sys/time.h>
00009 #include <unistd.h>
00010 
00011 #define dassert(x) do { if(!(x)) { \
00012     struct timeval tv; struct tm tm; time_t now; \
00013     time(&now); localtime_r(&now, &tm); gettimeofday(&tv, NULL); \
00014     fprintf(stderr, "%02d%02d%02d.%08d:%5d %s (%s:%d] failed assertion '%s'\n", \
00015             tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec, getpid(), __FUNCTION__, \
00016             __FILE__, __LINE__, #x); abort(); } } while(0)
00017 
00018 #define dperror(x) do { if(x) { \
00019     struct timeval tv; struct tm tm; time_t now; \
00020     time(&now); localtime_r(&now, &tm); gettimeofday(&tv, NULL); \
00021     fprintf(stderr, "%02d%02d%02d.%08d:%5d %s (%s:%d] '%s' failed with '%s'\n", \
00022             tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec, getpid(), __FUNCTION__, \
00023             __FILE__, __LINE__, #x, strerror(errno));  } } while(0)
00024 
00025 
00026 /* DEBUG only error messages */
00027 #ifdef DEBUG
00028         /* debug test: 
00029         * if the first argument `test' evaluates as false,
00030         * the rest of the arguments are passed to fprintf */ 
00031         #define dtest(test, args...)    \
00032         do { if (!test) {       \
00033                 fprintf(stderr, "%5d %s (%s:%d)] ", getpid(), __FUNCTION__, __FILE__, __LINE__);        \
00034                 fprintf(stderr, args);  \
00035                 fprintf(stderr, "\n");  \
00036         }} while(0)
00037 
00038         /* debug print 
00039          * prints arguments */
00040         #define dprintk(args...)        \
00041         do {    \
00042         struct timeval __tv; struct tm __tm; time_t __now; \
00043         time(&__now); localtime_r(&__now, &__tm); gettimeofday(&__tv, NULL); \
00044         fprintf(stderr, "%02d%02d%02d.%08d:%5d %s (%s:%d)] ", \
00045             __tm.tm_hour, __tm.tm_min, __tm.tm_sec, (int)__tv.tv_usec, getpid(), __FUNCTION__, \
00046             __FILE__, __LINE__); \
00047         fprintf(stderr, args);  \
00048         fprintf(stderr, "\n");  \
00049         } while(0)
00050 
00051     #define bhexdump(buffer, size) \
00052         do { \
00053             struct timeval tv; struct tm tm; time_t now; \
00054             int my__count; unsigned char *my__buffer = (unsigned char *)buffer; \
00055             time(&now); localtime_r(&now, &tm); gettimeofday(&tv, NULL); \
00056             fprintf(stderr, "%02d%02d%02d.%08d:%5d %s (%s:%d)] buffer %s at %p len %d ", \
00057                 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec, getpid(), __FUNCTION__, \
00058                 __FILE__, __LINE__, #buffer, buffer, size); \
00059             for(my__count = 0; my__count < size; my__count++) { \
00060                 fprintf(stderr, "%02x ", my__buffer[my__count]); \
00061                 if(my__count && my__count % 16 == 15) fprintf(stderr, "\n\t"); \
00062             } \
00063             fprintf(stderr, "\n"); \
00064         } while(0) 
00065     
00066 #else
00067         #define dtest(args...)
00068         #define dprintk(args...)
00069     #define dhexdump(args...)
00070 #endif /* DEBUG */
00071 
00072 #define printk(args...) \
00073         do {    \
00074         struct timeval tv; struct tm tm; time_t now; \
00075         time(&now); localtime_r(&now, &tm); gettimeofday(&tv, NULL); \
00076         fprintf(stderr, "%02d%02d%02d.%08d:%5d %s (%s:%d)] ", \
00077             tm.tm_hour, tm.tm_min, tm.tm_sec, (int)tv.tv_usec, getpid(), __FUNCTION__, \
00078             __FILE__, __LINE__); \
00079         fprintf(stderr, args);  \
00080         fprintf(stderr, "\n");  \
00081         } while(0)
00082 
00083 
00084 #define IF_FAIL_ERRNO(condition, args...)       \
00085         do {    \
00086         if (!(condition)) {     \
00087                 fprintf(stderr, "%s (%s:%d)] ", __FUNCTION__, __FILE__, __LINE__);      \
00088                 fprintf(stderr, args);  \
00089                 fprintf(stderr, ": ");  \
00090                 perror(NULL);   \
00091                 abort();        \
00092     }} while (0)
00093 
00094 #define handle_errno(x) if((x)<0) do { fprintf(stderr, "%s (%s:%d)] %s\n", __FUNCTION__, __FILE__,  __LINE__, strerror(errno)); abort(); } while(0)
00095 
00096 #endif /* !_FUMIGATE_ */

Generated on Mon May 28 04:25:19 2007 for BattletechMUX by  doxygen 1.4.7