00001 #ifndef INCLUDED_debuget_h
00002 #define INCLUDED_debuget_h
00003
00004 #include <stdio.h>
00005 #include <ctype.h>
00006 #include <iostream>
00007
00008 #ifdef DEBUG
00009 #include <string.h>
00010 #include <sstream>
00011 #include <fstream>
00012 #endif
00013
00014
00015 namespace debuget {
00016
00017 inline const char* extractFilename(const char* path) {
00018 const char * last=path;
00019 while(*path++)
00020 if(*path=='/')
00021 last=path+1;
00022 return last;
00023 }
00024
00025
00026 inline void displayAssert(const char* file, unsigned int line,const char* msg) { std::cerr << "ASSERT:"<<extractFilename(file)<<'.'<<line<<':'<< msg << std::endl; }
00027
00028 #ifdef DEBUG
00029
00030 #define ASSERT(b,msgstream) {if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); }}
00031
00032 #define ASSERTRET(b,msgstream) {if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); return; }}
00033
00034 #define ASSERTRETVAL(b,msgstream,v) {if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); return v; }}
00035
00036 #define ASSERTFATAL(b,msgstream,x) {if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); exit(x); }}
00037 #else
00038
00039 #define ASSERT(b,msgstream) {}
00040
00041 #define ASSERTRET(b,msgstream) {}
00042
00043 #define ASSERTRETVAL(b,msgstream,v) {}
00044
00045 #define ASSERTFATAL(b,msgstream,x) {}
00046 #endif
00047
00048
00049 inline char hexdigit(int c) {
00050 if(c<0)
00051 return '.';
00052 if(c<10)
00053 return '0'+c;
00054 if(c<16)
00055 return 'a'+(c-10);
00056 return ',';
00057 }
00058
00059
00060 inline void charhexout(char c) {
00061 printf("%c%c",hexdigit((c>>4)&0x0F),hexdigit(c&0x0F));
00062 }
00063
00064
00065 inline void hexout(const void* p, size_t n) {
00066 printf("%p:\n",p);
00067 const char* x=(const char*)p;
00068 for(unsigned int i=0; i<n;) {
00069 printf("%6d ",i);
00070 for(unsigned int k=0; k<8 && i<n; k++) {
00071 for(unsigned int j=0; j<4 && i<n; j++, i++) {
00072 charhexout(x[i]);
00073
00074 }
00075 printf(" ");
00076 }
00077 printf("\n");
00078 }
00079 }
00080
00081
00082 inline void hexout2(const void* p, size_t size) {
00083 const char* buf=static_cast<const char*>(p);
00084 unsigned int prev_line=0;
00085 const unsigned int cols=4;
00086 const unsigned int n_per_col=4;
00087 printf("Base: %p\n",buf);
00088 for(unsigned int i=0; i<size; i++) {
00089 if(i%(cols*n_per_col)==0)
00090 printf("%6u |",i);
00091 printf("%02hx",buf[i]);
00092 if((i+1)%(cols*n_per_col)==0) {
00093 printf("| ");
00094 for(unsigned int j=prev_line; j<=i; j++)
00095 printf("%c",isprint(buf[j])?buf[j]:'.');
00096 prev_line=i+1;
00097 printf("\n");
00098 } else if((i+1)%(n_per_col)==0)
00099 printf(" ");
00100 }
00101 for(unsigned int i=size; i%(cols*n_per_col)!=0; i++) {
00102 printf(" ");
00103 if((i+1)%(cols*n_per_col)==0) {
00104 printf("| ");
00105 for(unsigned int j=prev_line; j<size; j++)
00106 printf("%c",isprint(buf[j])?buf[j]:'.');
00107 prev_line=i;
00108 printf("\n");
00109 } else if((i+1)%(n_per_col)==0)
00110 printf(" ");
00111 }
00112 }
00113
00114
00115 inline void hexout3(const char* buf, size_t size) {
00116 const unsigned int linelen=24;
00117 for(unsigned int i=0; i<size; i++) {
00118 printf("%.2hx",buf[i]);
00119 if((i+1)%linelen==0) {
00120 printf(" ");
00121 for(unsigned int j=i+1-linelen; j<=i; j++)
00122 putchar(isprint(buf[j])?buf[j]:'.');
00123 printf("\n");
00124 } else if((i+1)%4==0)
00125 printf(" ");
00126 }
00127
00128 for(unsigned int i=size; i%linelen!=0; i++) {
00129 printf(" ");
00130 if((i+1)%linelen==0) {
00131 printf(" ");
00132 for(unsigned int j=(size/linelen)*linelen; j<size; j++)
00133 putchar(isprint(buf[j])?buf[j]:'.');
00134 printf("\n");
00135 } else if((i+1)%4==0)
00136 printf(" ");
00137 }
00138 }
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 #endif