debuget.h
Go to the documentation of this file.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
00031 #define ASSERT(b,msgstream) {if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); }}
00032
00033 #define ASSERTRET(b,msgstream) {if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); return; }}
00034
00035 #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; }}
00036
00037 #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); }}
00038
00039 #define ASSERTIF(b,msgstream) if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); } else
00040
00041 #else
00042
00043
00044 #define ASSERT(b,msgstream) {}
00045
00046 #define ASSERTRET(b,msgstream) {}
00047
00048 #define ASSERTRETVAL(b,msgstream,v) {}
00049
00050 #define ASSERTFATAL(b,msgstream,x) {}
00051
00052 #define ASSERTIF(b,msgstream) {}
00053
00054 #endif
00055
00056
00057 inline char hexdigit(int c) {
00058 if(c<0)
00059 return '.';
00060 if(c<10)
00061 return '0'+c;
00062 if(c<16)
00063 return 'a'+(c-10);
00064 return ',';
00065 }
00066
00067
00068 inline void charhexout(char c) {
00069 printf("%c%c",hexdigit((c>>4)&0x0F),hexdigit(c&0x0F));
00070 }
00071
00072
00073 inline void hexout(const void* p, size_t n) {
00074 printf("%p:\n",p);
00075 const char* x=(const char*)p;
00076 for(unsigned int i=0; i<n;) {
00077 printf("%6d ",i);
00078 for(unsigned int k=0; k<8 && i<n; k++) {
00079 for(unsigned int j=0; j<4 && i<n; j++, i++) {
00080 charhexout(x[i]);
00081
00082 }
00083 printf(" ");
00084 }
00085 printf("\n");
00086 }
00087 }
00088
00089
00090 inline void hexout2(const void* p, size_t size) {
00091 const char* buf=static_cast<const char*>(p);
00092 size_t prev_line=0;
00093 const unsigned int cols=4;
00094 const unsigned int n_per_col=4;
00095 printf("Base: %p\n",buf);
00096 for(unsigned int i=0; i<size; i++) {
00097 if(i%(cols*n_per_col)==0)
00098 printf("%6u |",i);
00099 printf("%02hhx",buf[i]);
00100 if((i+1)%(cols*n_per_col)==0) {
00101 printf("| ");
00102 for(size_t j=prev_line; j<=i; j++)
00103 printf("%c",isprint(buf[j])?buf[j]:'.');
00104 prev_line=i+1;
00105 printf("\n");
00106 } else if((i+1)%(n_per_col)==0)
00107 printf(" ");
00108 }
00109 for(size_t i=size; i%(cols*n_per_col)!=0; i++) {
00110 printf(" ");
00111 if((i+1)%(cols*n_per_col)==0) {
00112 printf("| ");
00113 for(size_t j=prev_line; j<size; j++)
00114 printf("%c",isprint(buf[j])?buf[j]:'.');
00115 prev_line=i;
00116 printf("\n");
00117 } else if((i+1)%(n_per_col)==0)
00118 printf(" ");
00119 }
00120 }
00121
00122
00123 inline void hexout3(const char* buf, size_t size) {
00124 const unsigned int linelen=24;
00125 for(unsigned int i=0; i<size; i++) {
00126 printf("%.2hhx",buf[i]);
00127 if((i+1)%linelen==0) {
00128 printf(" ");
00129 for(unsigned int j=i+1-linelen; j<=i; j++)
00130 putchar(isprint(buf[j])?buf[j]:'.');
00131 printf("\n");
00132 } else if((i+1)%4==0)
00133 printf(" ");
00134 }
00135
00136 for(size_t i=size; i%linelen!=0; i++) {
00137 printf(" ");
00138 if((i+1)%linelen==0) {
00139 printf(" ");
00140 for(size_t j=(size/linelen)*linelen; j<size; j++)
00141 putchar(isprint(buf[j])?buf[j]:'.');
00142 printf("\n");
00143 } else if((i+1)%4==0)
00144 printf(" ");
00145 }
00146 }
00147 }
00148
00149
00150
00151
00152
00153
00154 #endif