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 
00006 #ifdef DEBUG
00007 #include <iostream>
00008 #include <string.h>
00009 #include <fstream>
00010 //! for historical reasons - the previous compiler give the entire path for __FILE__, for display, just use the filename
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 //! if the bool b is false, std::cout the string
00019 #define ASSERT(b,str) {if(!(b)) std::cout << "ASSERT:"<<_extractFilename(__FILE__)<<'.'<<__LINE__<<':'<< str << std::endl;}
00020 //! if the bool b is false, std::cout the string and return
00021 #define ASSERTRET(b,str) {if(!(b)) { std::cout << "ASSERT:"<<_extractFilename(__FILE__)<<'.'<<__LINE__<<':'<< str << std::endl; return; }}
00022 //! if the bool b is false, std::cout the string and return the value
00023 #define ASSERTRETVAL(b,str,v) {if(!(b)) { std::cout << "ASSERT:"<<_extractFilename(__FILE__)<<'.'<<__LINE__<<':'<< str << std::endl; return v; }}
00024 //! if the bool b is false, std::cout the string and exit(x)
00025 #define ASSERTFATAL(b,str,x) {if(!(b)) { std::cout << "ASSERT:"<<_extractFilename(__FILE__)<<'.'<<__LINE__<<':'<< str << std::endl; exit(x); }}
00026 //#define FILELOG(fname,type,str) { ofstream f(fname,type); f<<str; }
00027 #else
00028 //! if the bool b is false, std::cout the string
00029 #define ASSERT(b,str) {}
00030 //! if the bool b is false, std::cout the string and return
00031 #define ASSERTRET(b,str) {}
00032 //! if the bool b is false, std::cout the string and return the value
00033 #define ASSERTRETVAL(b,str,v) {}
00034 //! if the bool b is false, std::cout the string and exit(x)
00035 #define ASSERTFATAL(b,str,x) {}
00036 #endif
00037 
00038 //! returns the hex char that corresponds to @a c, which should be 0-16 (returns '.' otherwise)
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 //! printf's the two hex digits coresponding to a byte
00050 inline void charhexout(char c) {
00051   printf("%c%c",hexdigit((c>>4)&0x0F),hexdigit(c&0x0F));
00052 }
00053 
00054 //! charhexout's @a n bytes starting at @a p
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         //        std::cout << flush;
00064       }
00065       printf(" ");
00066     }
00067     printf("\n");
00068   }
00069 }
00070 
00071 //! displays hex and ascii values of @a size bytes from @a p
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 //! displays hex and ascii values of @a size bytes from @a p
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   //finish off the last line (still need to do human-readable version)
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 /*! @file
00133  * @brief Defines several debugging functions and macros, including ::ASSERT (and variations)
00134  * @author ejt (Creator)
00135  *
00136  * $Author: ejt $
00137  * $Name: tekkotsu-2_4_1 $
00138  * $Revision: 1.6 $
00139  * $State: Exp $
00140  * $Date: 2005/08/07 04:11:04 $
00141  * $Revision: 1.6 $
00142  * $State: Exp $
00143  * $Date: 2005/08/07 04:11:04 $
00144  */
00145 
00146 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:46 2005 by Doxygen 1.4.4