Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

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 //! contains some debugging functions and #ASSERT macros (although the macros don't respect the namespace scoping...)
00015 namespace debuget {
00016   //! for display, just use the filename, not the whole path
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   //! mostly for use with a debugger -- set a breakpoint on this function and you can catch anytime an assertion is generated
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   //! if the bool b is false, std::cout the string
00030   #define ASSERT(b,msgstream) {if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); }}
00031   //! if the bool b is false, std::cout the string and return
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   //! if the bool b is false, std::cout the string and return the value
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   //! if the bool b is false, std::cout the string and exit(x)
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   //! if the bool b is false, std::cout the string
00039   #define ASSERT(b,msgstream) {}
00040   //! if the bool b is false, std::cout the string and return
00041   #define ASSERTRET(b,msgstream) {}
00042   //! if the bool b is false, std::cout the string and return the value
00043   #define ASSERTRETVAL(b,msgstream,v) {}
00044   //! if the bool b is false, std::cout the string and exit(x)
00045   #define ASSERTFATAL(b,msgstream,x) {}
00046   #endif
00047 
00048   //! returns the hex char that corresponds to @a c, which should be 0-16 (returns '.' otherwise)
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   //! printf's the two hex digits coresponding to a byte
00060   inline void charhexout(char c) {
00061     printf("%c%c",hexdigit((c>>4)&0x0F),hexdigit(c&0x0F));
00062   }
00063 
00064   //! charhexout's @a n bytes starting at @a p
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           //        std::cout << flush;
00074         }
00075         printf(" ");
00076       }
00077       printf("\n");
00078     }
00079   }
00080 
00081   //! displays hex and ascii values of @a size bytes from @a p
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   //! displays hex and ascii values of @a size bytes from @a p
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     //finish off the last line (still need to do human-readable version)
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 /*! @file
00142  * @brief Defines several debugging functions and macros, including ::ASSERT (and variations)
00143  * @author ejt (Creator)
00144  *
00145  * $Author: ejt $
00146  * $Name: tekkotsu-4_0 $
00147  * $Revision: 1.15 $
00148  * $State: Exp $
00149  * $Date: 2007/11/01 18:51:37 $
00150  * $Revision: 1.15 $
00151  * $State: Exp $
00152  * $Date: 2007/11/01 18:51:37 $
00153  */
00154 
00155 #endif

Tekkotsu v4.0
Generated Thu Nov 22 00:54:52 2007 by Doxygen 1.5.4