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   
00030   //! if the bool b is false, std::cout the string
00031   #define ASSERT(b,msgstream) {if(!(b)) { std::stringstream DEBUGET_ss; DEBUGET_ss << msgstream; debuget::displayAssert(__FILE__,__LINE__,DEBUGET_ss.str().c_str()); }}
00032   //! if the bool b is false, std::cout the string and return
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   //! if the bool b is false, std::cout the string and return the value
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   //! if the bool b is false, std::cout the string and exit(x)
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   //! sets up an 'if' statement so the following block won't be executed if the ASSERT fails
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   //! if the bool b is false, std::cout the string
00044   #define ASSERT(b,msgstream) {}
00045   //! if the bool b is false, std::cout the string and return
00046   #define ASSERTRET(b,msgstream) {}
00047   //! if the bool b is false, std::cout the string and return the value
00048   #define ASSERTRETVAL(b,msgstream,v) {}
00049   //! if the bool b is false, std::cout the string and exit(x)
00050   #define ASSERTFATAL(b,msgstream,x) {}
00051   //! sets up an 'if' statement so the following block won't be executed if the ASSERT fails
00052   #define ASSERTIF(b,msgstream) {}
00053   
00054 #endif
00055 
00056   //! returns the hex char that corresponds to @a c, which should be 0-16 (returns '.' otherwise)
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   //! printf's the two hex digits coresponding to a byte
00068   inline void charhexout(char c) {
00069     printf("%c%c",hexdigit((c>>4)&0x0F),hexdigit(c&0x0F));
00070   }
00071 
00072   //! charhexout's @a n bytes starting at @a p
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           //        std::cout << flush;
00082         }
00083         printf(" ");
00084       }
00085       printf("\n");
00086     }
00087   }
00088 
00089   //! displays hex and ascii values of @a size bytes from @a p
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   //! displays hex and ascii values of @a size bytes from @a p
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     //finish off the last line (still need to do human-readable version)
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 /*! @file
00150  * @brief Defines several debugging functions and macros, including ::ASSERT (and variations)
00151  * @author ejt (Creator)
00152  */
00153 
00154 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:37 2016 by Doxygen 1.6.3