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
00042
00043
00044 #include <iostream>
00045 #include <iomanip>
00046 #include <stdarg.h>
00047 #include <cstring>
00048 #include <stdlib.h>
00049 #include <cstdio>
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 NAMESPACE_SDH_START
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00112 class cDBG
00113 {
00114 protected:
00115 char const* debug_color;
00116 char const* normal_color;
00117 std::ostream *output;
00118 bool debug_flag;
00119 public:
00120
00129 cDBG( bool flag=false, char const* color="red", std::ostream *fd=&std::cerr )
00130 {
00131 debug_flag = flag;
00132 SetColor( color );
00133 output = fd;
00134 }
00135
00136
00137 ~cDBG()
00138 {
00139 output->flush();
00140 }
00141
00146 void SetFlag( bool flag )
00147 {
00148 debug_flag = flag;
00149 }
00150
00151
00155 bool GetFlag( void ) const
00156 {
00157 return debug_flag;
00158 }
00159
00167 void SetColor( char const* color )
00168 {
00169 debug_color = GetColor( color );
00170 if ( !strcmp( debug_color, "" ) )
00171
00172 normal_color = debug_color;
00173 else
00174
00175 normal_color = GetColor( "normal" );
00176 }
00177
00178
00182 void SetOutput( std::ostream *fd )
00183 {
00184 output = fd;
00185 }
00186
00187
00192 void PDM( char const* fmt, ... ) SDH__attribute__ ((format (printf, 2, 3)))
00193
00194
00195
00196
00197
00198
00199
00200 {
00201 if (!debug_flag) return;
00202
00203 char buffer[ 256 ];
00204
00205 va_list arglist;
00206 va_start( arglist, fmt );
00207 #if SDH_USE_VCC
00208 vsnprintf_s( buffer, 256, 256, fmt, arglist );
00209 #else
00210 vsnprintf( buffer, 256, fmt, arglist );
00211 #endif
00212 va_end( arglist );
00213
00214 *output << debug_color << buffer << normal_color << std::flush;
00215 }
00216
00217
00229 char const* GetColor( char const* c )
00230 {
00231 char* sdh_no_color = getenv( "SDH_NO_COLOR" );
00232 if ( sdh_no_color != NULL )
00233 return "";
00234
00235 char* os = getenv( "OS" );
00236 char* ostype = getenv( "OSTYPE" );
00237 if ( os && (!strncmp( os, "WIN", 3 ) || !strncmp( os, "Win", 3 )) && (! ostype || (ostype && strcmp( ostype, "cygwin" ))) )
00238 return "";
00239
00240 if ( !strcmp( c, "normal" ) ) return "\x1b[0m";
00241 if ( !strcmp( c, "bold" ) ) return "\x1b[1m";
00242 if ( !strcmp( c, "red" ) ) return "\x1b[31m";
00243 if ( !strcmp( c, "green" ) ) return "\x1b[32m";
00244 if ( !strcmp( c, "yellow" ) ) return "\x1b[33m";
00245 if ( !strcmp( c, "blue" ) ) return "\x1b[34m";
00246 if ( !strcmp( c, "magenta" ) ) return "\x1b[35m";
00247 if ( !strcmp( c, "cyan" ) ) return "\x1b[36m";
00248 if ( !strcmp( c, "white" ) ) return "\x1b[37m";
00249 if ( !strcmp( c, "black" ) ) return "\x1b[39m";
00250 if ( !strcmp( c, "black_back" ) ) return "\x1b[40m";
00251 if ( !strcmp( c, "red_back" ) ) return "\x1b[41m";
00252 if ( !strcmp( c, "green_back" ) ) return "\x1b[42m";
00253 if ( !strcmp( c, "yellow_back" ) ) return "\x1b[43m";
00254 if ( !strcmp( c, "blue_back" ) ) return "\x1b[44m";
00255 if ( !strcmp( c, "cyan_back" ) ) return "\x1b[45m";
00256 if ( !strcmp( c, "magenta_back" ) ) return "\x1b[46m";
00257 if ( !strcmp( c, "white_back" ) ) return "\x1b[47m";
00258
00259
00260 return "";
00261 }
00262
00270 template <typename T>
00271 cDBG& operator<<( T const& v )
00272 {
00273 if (!debug_flag) return *this;
00274
00275 *output << debug_color
00276 << v
00277 << normal_color << std::flush;
00278 return *this;
00279 }
00280
00281
00295 # define VAR( _d, _var ) \
00296 (_d) << #_var << "='" << _var << "'\n"
00297
00310 # define V( _var ) \
00311 #_var << "='" << _var << "' "
00312 };
00313
00314 NAMESPACE_SDH_END
00315
00316 #endif
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329