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