dbg.h
Go to the documentation of this file.
00001 //======================================================================
00029 //======================================================================
00030 
00031 #ifndef DBG_h_
00032 #define DBG_h_
00033 
00034 #include "sdhlibrary_settings.h"
00035 
00036 #if SDH_USE_VCC
00037 # pragma warning(disable : 4996)
00038 #endif
00039 
00040 //----------------------------------------------------------------------
00041 // System Includes - include with <>
00042 //----------------------------------------------------------------------
00043 
00044 #include <iostream>
00045 #include <iomanip>
00046 #include <string>
00047 #include <stdarg.h>
00048 #include <cstring>    // needed in gcc-4.3 for prototypes like strcmp according to http://gcc.gnu.org/gcc-4.3/porting_to.html
00049 #include <stdlib.h>   // needed in gcc-4.3 for prototypes like getenv
00050 #include <cstdio>     // needed in gcc-4.4 (as reported by Hannes Saal)
00051 
00052 //----------------------------------------------------------------------
00053 // Project Includes - include with ""
00054 //----------------------------------------------------------------------
00055 
00056 
00057 //----------------------------------------------------------------------
00058 // Defines, enums, unions, structs
00059 //----------------------------------------------------------------------
00060 
00061 NAMESPACE_SDH_START
00062 
00063 
00064 //----------------------------------------------------------------------
00065 // Global variables (declarations)
00066 //----------------------------------------------------------------------
00067 
00068 
00069 //----------------------------------------------------------------------
00070 // External functions and classes (declarations)
00071 //----------------------------------------------------------------------
00072 
00073 
00074 //----------------------------------------------------------------------
00075 // Function prototypes (function declarations)
00076 //----------------------------------------------------------------------
00077 
00078 
00079 //----------------------------------------------------------------------
00080 // Class declarations
00081 //----------------------------------------------------------------------
00082 
00083 
00113 class VCC_EXPORT cDBG
00114 {
00115 protected:
00116     char const*   debug_color;
00117     char const*   normal_color;
00118     std::ostream *output;
00119     bool          debug_flag;
00120     std::streamsize mywidth;
00121 public:
00122 
00131     cDBG( bool flag=false, char const* color="red", std::ostream *fd=&std::cerr )
00132     {
00133         debug_flag     = flag;
00134         SetColor( color );
00135         output         = fd;
00136         mywidth = output->width();
00137     }
00138     //---------------------
00139 
00140     ~cDBG()
00141     {
00142         output->flush();
00143     }
00144 
00149     void SetFlag( bool flag )
00150     {
00151         debug_flag  = flag;
00152     }
00153 
00154 
00158     bool GetFlag( void ) const
00159     {
00160         return debug_flag;
00161     }
00162 
00170     void SetColor( char const* color )
00171     {
00172         debug_color = GetColor( color );
00173         if ( !strcmp( debug_color, "" ) )
00174             // no debug color hence no normal color needed
00175             normal_color = debug_color;
00176         else
00177             // the code to set color back to normal
00178             normal_color = GetColor( "normal" );
00179     }
00180     //---------------------
00181 
00185     void SetOutput( std::ostream *fd )
00186     {
00187         output = fd;
00188     }
00189     //---------------------
00190 
00195     void PDM( char const* fmt, ... ) SDH__attribute__ ((format (printf, 2, 3)))
00196     /*
00197     * Remark:
00198     *   Since non-static C++ methods have an implicit `this' argument,
00199     *   the arguments of such methods should be counted from two, not
00200     *   one, when giving values for STRING-INDEX and FIRST-TO-CHECK
00201     *   parameter of the format __attribute__.)
00202     */
00203     {
00204         if (!debug_flag) return;
00205 
00206         char buffer[ 256 ];
00207 
00208         va_list arglist;
00209         va_start( arglist, fmt );
00210 #if SDH_USE_VCC
00211         vsnprintf_s( buffer, 256, 256, fmt, arglist );
00212 #else
00213         vsnprintf( buffer, 256, fmt, arglist );
00214 #endif
00215         va_end( arglist );
00216 
00217         *output << debug_color << buffer << normal_color << std::flush;
00218     }
00219     //---------------------
00220 
00232     char const* GetColor( char const* c )
00233     {
00234         char* sdh_no_color = getenv( "SDH_NO_COLOR" );
00235         if ( sdh_no_color != NULL )
00236             return "";
00237 
00238         char* os = getenv( "OS" );
00239         char* ostype = getenv( "OSTYPE" );
00240         if ( os && (!strncmp( os, "WIN", 3 ) || !strncmp( os, "Win", 3 )) && (! ostype || (ostype && strcmp( ostype, "cygwin" ))) )
00241             return "";
00242 
00243         if ( !strcmp( c, "normal" ) )       return "\x1b[0m";
00244         if ( !strcmp( c, "bold" ) )         return "\x1b[1m";
00245         if ( !strcmp( c, "red" ) )          return "\x1b[31m";
00246         if ( !strcmp( c, "green" ) )        return "\x1b[32m";
00247         if ( !strcmp( c, "yellow" ) )       return "\x1b[33m";
00248         if ( !strcmp( c, "blue" ) )         return "\x1b[34m";
00249         if ( !strcmp( c, "magenta" ) )      return "\x1b[35m";
00250         if ( !strcmp( c, "cyan" ) )         return "\x1b[36m";
00251         if ( !strcmp( c, "white" ) )        return "\x1b[37m";
00252         if ( !strcmp( c, "black" ) )        return "\x1b[39m";
00253         if ( !strcmp( c, "black_back" ) )   return "\x1b[40m";
00254         if ( !strcmp( c, "red_back" ) )     return "\x1b[41m";
00255         if ( !strcmp( c, "green_back" ) )   return "\x1b[42m";
00256         if ( !strcmp( c, "yellow_back" ) )  return "\x1b[43m";
00257         if ( !strcmp( c, "blue_back" ) )    return "\x1b[44m";
00258         if ( !strcmp( c, "cyan_back" ) )    return "\x1b[45m";
00259         if ( !strcmp( c, "magenta_back" ) ) return "\x1b[46m";
00260         if ( !strcmp( c, "white_back" ) )   return "\x1b[47m";
00261 
00262         // no coloring possible or unknown color: return ""
00263         return "";
00264     }
00265 
00273     template <typename T>
00274     cDBG& operator<<( T const& v )
00275     {
00276         if (!debug_flag) return *this;
00277 
00278         output->width( 0 );
00279         *output << debug_color;
00280         output->width( mywidth );
00281         *output << v;
00282         mywidth = output->width();
00283         output->width( 0 );
00284         *output << normal_color << std::flush;
00285         return *this;
00286     }
00287     //---------------------
00288 
00289     std::ostream& flush()
00290     {
00291         return output->flush();
00292     }
00293     //---------------------
00294 
00295 
00309 # define VAR( _d, _var )                        \
00310     (_d) << #_var << "='" << _var << "'\n"
00311 
00324 # define VAL( _var )                      \
00325     #_var << "='" << _var << "' "
00326 };
00327 
00329 class VCC_EXPORT cHexByteString
00330 {
00331     char const* bytes;
00332     int         len;
00333 public:
00335     cHexByteString( char const* _bytes, int _len ) :
00336         bytes(_bytes),
00337         len(_len)
00338         {};
00339 
00340     friend VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cHexByteString const &s);
00341 };
00342 
00344 inline VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cHexByteString const &s)
00345 {
00346     //-----
00347     // print all bytes as hex bytes:
00348     bool is_all_printable_ascii = true;
00349     for ( int i = 0; i < s.len; i++ )
00350     {
00351         stream << std::hex << std::setw(2) << std::setfill('0') << int( ((unsigned char const*)s.bytes)[i] ) << " ";
00352         if ( s.bytes[i] < 0x20 || ((unsigned char) s.bytes[i]) >= 0x80 )
00353             is_all_printable_ascii = false;
00354     }
00355     //-----
00356 
00357     //-----
00358     // if the bytes were all printable ascii codes then print them as string as well:
00359     if ( is_all_printable_ascii )
00360         stream << "= \"" << std::string( s.bytes, s.len ) << "\"";
00361     //-----
00362 
00363     return stream << std::dec;
00364 };
00365 
00366 NAMESPACE_SDH_END
00367 
00368 #endif
00369 
00370 
00371 //======================================================================
00372 /*
00373   Here are some settings for the emacs/xemacs editor (and can be safely ignored):
00374   (e.g. to explicitely set C++ mode for *.h header files)
00375 
00376   Local Variables:
00377   mode:C++
00378   mode:ELSE
00379   End:
00380  */
00381 //======================================================================
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


schunk_sdh
Author(s): Florian Weisshardt
autogenerated on Tue Mar 5 2013 14:44:11