dbg.h
Go to the documentation of this file.
1 //======================================================================
29 //======================================================================
30 
31 #ifndef DBG_h_
32 #define DBG_h_
33 
34 #include "sdhlibrary_settings.h"
35 
36 #if SDH_USE_VCC
37 # pragma warning(disable : 4996)
38 #endif
39 
40 //----------------------------------------------------------------------
41 // System Includes - include with <>
42 //----------------------------------------------------------------------
43 
44 #include <iostream>
45 #include <iomanip>
46 #include <string>
47 #include <stdarg.h>
48 #include <cstring> // needed in gcc-4.3 for prototypes like strcmp according to http://gcc.gnu.org/gcc-4.3/porting_to.html
49 #include <stdlib.h> // needed in gcc-4.3 for prototypes like getenv
50 #include <cstdio> // needed in gcc-4.4 (as reported by Hannes Saal)
51 
52 //----------------------------------------------------------------------
53 // Project Includes - include with ""
54 //----------------------------------------------------------------------
55 
56 
57 //----------------------------------------------------------------------
58 // Defines, enums, unions, structs
59 //----------------------------------------------------------------------
60 
62 
63 
64 //----------------------------------------------------------------------
65 // Global variables (declarations)
66 //----------------------------------------------------------------------
67 
68 
69 //----------------------------------------------------------------------
70 // External functions and classes (declarations)
71 //----------------------------------------------------------------------
72 
73 
74 //----------------------------------------------------------------------
75 // Function prototypes (function declarations)
76 //----------------------------------------------------------------------
77 
78 
79 //----------------------------------------------------------------------
80 // Class declarations
81 //----------------------------------------------------------------------
82 
83 
113 class VCC_EXPORT cDBG
114 {
115 protected:
116  char const* debug_color;
117  char const* normal_color;
118  std::ostream *output;
120  std::streamsize mywidth;
121 public:
122 
131  cDBG( bool flag=false, char const* color="red", std::ostream *fd=&std::cerr )
132  {
133  debug_flag = flag;
134  SetColor( color );
135  output = fd;
136  mywidth = output->width();
137  }
138  //---------------------
139 
141  {
142  output->flush();
143  }
144 
149  void SetFlag( bool flag )
150  {
151  debug_flag = flag;
152  }
153 
154 
158  bool GetFlag( void ) const
159  {
160  return debug_flag;
161  }
162 
170  void SetColor( char const* color )
171  {
172  debug_color = GetColor( color );
173  if ( !strcmp( debug_color, "" ) )
174  // no debug color hence no normal color needed
175  normal_color = debug_color;
176  else
177  // the code to set color back to normal
178  normal_color = GetColor( "normal" );
179  }
180  //---------------------
181 
185  void SetOutput( std::ostream *fd )
186  {
187  output = fd;
188  }
189  //---------------------
190 
195  void PDM( char const* fmt, ... ) SDH__attribute__ ((format (printf, 2, 3)))
196  /*
197  * Remark:
198  * Since non-static C++ methods have an implicit `this' argument,
199  * the arguments of such methods should be counted from two, not
200  * one, when giving values for STRING-INDEX and FIRST-TO-CHECK
201  * parameter of the format __attribute__.)
202  */
203  {
204  if (!debug_flag) return;
205 
206  char buffer[ 256 ];
207 
208  va_list arglist;
209  va_start( arglist, fmt );
210 #if SDH_USE_VCC
211  vsnprintf_s( buffer, 256, 256, fmt, arglist );
212 #else
213  vsnprintf( buffer, 256, fmt, arglist );
214 #endif
215  va_end( arglist );
216 
217  *output << debug_color << buffer << normal_color << std::flush;
218  }
219  //---------------------
220 
232  char const* GetColor( char const* c )
233  {
234  char* sdh_no_color = getenv( "SDH_NO_COLOR" );
235  if ( sdh_no_color != NULL )
236  return "";
237 
238  char* os = getenv( "OS" );
239  char* ostype = getenv( "OSTYPE" );
240  if ( os && (!strncmp( os, "WIN", 3 ) || !strncmp( os, "Win", 3 )) && (! ostype || (ostype && strcmp( ostype, "cygwin" ))) )
241  return "";
242 
243  if ( !strcmp( c, "normal" ) ) return "\x1b[0m";
244  if ( !strcmp( c, "bold" ) ) return "\x1b[1m";
245  if ( !strcmp( c, "red" ) ) return "\x1b[31m";
246  if ( !strcmp( c, "green" ) ) return "\x1b[32m";
247  if ( !strcmp( c, "yellow" ) ) return "\x1b[33m";
248  if ( !strcmp( c, "blue" ) ) return "\x1b[34m";
249  if ( !strcmp( c, "magenta" ) ) return "\x1b[35m";
250  if ( !strcmp( c, "cyan" ) ) return "\x1b[36m";
251  if ( !strcmp( c, "white" ) ) return "\x1b[37m";
252  if ( !strcmp( c, "black" ) ) return "\x1b[39m";
253  if ( !strcmp( c, "black_back" ) ) return "\x1b[40m";
254  if ( !strcmp( c, "red_back" ) ) return "\x1b[41m";
255  if ( !strcmp( c, "green_back" ) ) return "\x1b[42m";
256  if ( !strcmp( c, "yellow_back" ) ) return "\x1b[43m";
257  if ( !strcmp( c, "blue_back" ) ) return "\x1b[44m";
258  if ( !strcmp( c, "cyan_back" ) ) return "\x1b[45m";
259  if ( !strcmp( c, "magenta_back" ) ) return "\x1b[46m";
260  if ( !strcmp( c, "white_back" ) ) return "\x1b[47m";
261 
262  // no coloring possible or unknown color: return ""
263  return "";
264  }
265 
273  template <typename T>
274  cDBG& operator<<( T const& v )
275  {
276  if (!debug_flag) return *this;
277 
278  output->width( 0 );
279  *output << debug_color;
280  output->width( mywidth );
281  *output << v;
282  mywidth = output->width();
283  output->width( 0 );
284  *output << normal_color << std::flush;
285  return *this;
286  }
287  //---------------------
288 
289  std::ostream& flush()
290  {
291  return output->flush();
292  }
293  //---------------------
294 
295 
309 # define VAR( _d, _var ) \
310  (_d) << #_var << "='" << _var << "'\n"
311 
324 # define VAL( _var ) \
325  #_var << "='" << _var << "' "
326 };
327 
329 class VCC_EXPORT cHexByteString
330 {
331  char const* bytes;
332  int len;
333 public:
335  cHexByteString( char const* _bytes, int _len ) :
336  bytes(_bytes),
337  len(_len)
338  {};
339 
340  friend VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cHexByteString const &s);
341 };
342 
344 inline VCC_EXPORT std::ostream &operator<<(std::ostream &stream, cHexByteString const &s)
345 {
346  //-----
347  // print all bytes as hex bytes:
348  bool is_all_printable_ascii = true;
349  for ( int i = 0; i < s.len; i++ )
350  {
351  stream << std::hex << std::setw(2) << std::setfill('0') << int( ((unsigned char const*)s.bytes)[i] ) << " ";
352  if ( s.bytes[i] < 0x20 || ((unsigned char) s.bytes[i]) >= 0x80 )
353  is_all_printable_ascii = false;
354  }
355  //-----
356 
357  //-----
358  // if the bytes were all printable ascii codes then print them as string as well:
359  if ( is_all_printable_ascii && s.len >= 0 )
360  stream << "= \"" << std::string( s.bytes, s.len ) << "\"";
361  //-----
362 
363  return stream << std::dec;
364 }
365 
367 
368 #endif
369 
370 
371 //======================================================================
372 /*
373  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
374  (e.g. to explicitely set C++ mode for *.h header files)
375 
376  Local Variables:
377  mode:C++
378  mode:ELSE
379  End:
380  */
381 //======================================================================
void SetColor(char const *color)
Definition: dbg.h:170
A class to print colored debug messages.
Definition: dbg.h:113
VCC_EXPORT std::ostream & operator<<(std::ostream &stream, cHexByteString const &s)
output the bytes in s to stream as a list of space separated hex bytes (without 0x prefix) ...
Definition: dbg.h:344
cDBG(bool flag=false, char const *color="red", std::ostream *fd=&std::cerr)
Definition: dbg.h:131
cHexByteString(char const *_bytes, int _len)
ctor: create a cHexByteString with _len bytes at _bytes
Definition: dbg.h:335
void SetOutput(std::ostream *fd)
Definition: dbg.h:185
int len
Definition: dbg.h:332
#define NULL
Definition: getopt1.c:56
~cDBG()
Definition: dbg.h:140
void SetFlag(bool flag)
Definition: dbg.h:149
dummy class for (debug) stream output of bytes as list of hex values
Definition: dbg.h:329
char const * bytes
Definition: dbg.h:331
#define NAMESPACE_SDH_START
std::ostream & flush()
Definition: dbg.h:289
std::streamsize mywidth
Definition: dbg.h:120
char const * debug_color
Definition: dbg.h:116
*output<< debug_color<< buffer<< normal_color<< std::flush;} char const *GetColor(char const *c) { char *sdh_no_color=getenv("SDH_NO_COLOR");if(sdh_no_color !=NULL) return "";char *os=getenv("OS");char *ostype=getenv("OSTYPE");if(os &&(!strncmp(os, "WIN", 3)||!strncmp(os, "Win", 3)) &&(! ostype||(ostype &&strcmp(ostype, "cygwin")))) return "";if(!strcmp(c, "normal")) return "\1b[0m";if(!strcmp(c, "bold")) return "\1b[1m";if(!strcmp(c, "red")) return "\1b[31m";if(!strcmp(c, "green")) return "\1b[32m";if(!strcmp(c, "yellow")) return "\1b[33m";if(!strcmp(c, "blue")) return "\1b[34m";if(!strcmp(c, "magenta")) return "\1b[35m";if(!strcmp(c, "cyan")) return "\1b[36m";if(!strcmp(c, "white")) return "\1b[37m";if(!strcmp(c, "black")) return "\1b[39m";if(!strcmp(c, "black_back")) return "\1b[40m";if(!strcmp(c, "red_back")) return "\1b[41m";if(!strcmp(c, "green_back")) return "\1b[42m";if(!strcmp(c, "yellow_back")) return "\1b[43m";if(!strcmp(c, "blue_back")) return "\1b[44m";if(!strcmp(c, "cyan_back")) return "\1b[45m";if(!strcmp(c, "magenta_back")) return "\1b[46m";if(!strcmp(c, "white_back")) return "\1b[47m";return "";} template< typename T > cDBG & operator<<(T const &v)
Definition: dbg.h:274
va_list arglist
Definition: dbg.h:208
#define SDH__attribute__(...)
#define NAMESPACE_SDH_END
This file contains settings to make the SDHLibrary compile on differen systems:
bool debug_flag
Definition: dbg.h:119
char const * normal_color
Definition: dbg.h:117
std::ostream * output
Definition: dbg.h:118
bool GetFlag(void) const
Definition: dbg.h:158


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Mon Feb 28 2022 23:41:51