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 //======================================================================
cDBG::mywidth
std::streamsize mywidth
Definition: dbg.h:120
cDBG::operator<<
*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 "\x1b[0m";if(!strcmp(c, "bold")) return "\x1b[1m";if(!strcmp(c, "red")) return "\x1b[31m";if(!strcmp(c, "green")) return "\x1b[32m";if(!strcmp(c, "yellow")) return "\x1b[33m";if(!strcmp(c, "blue")) return "\x1b[34m";if(!strcmp(c, "magenta")) return "\x1b[35m";if(!strcmp(c, "cyan")) return "\x1b[36m";if(!strcmp(c, "white")) return "\x1b[37m";if(!strcmp(c, "black")) return "\x1b[39m";if(!strcmp(c, "black_back")) return "\x1b[40m";if(!strcmp(c, "red_back")) return "\x1b[41m";if(!strcmp(c, "green_back")) return "\x1b[42m";if(!strcmp(c, "yellow_back")) return "\x1b[43m";if(!strcmp(c, "blue_back")) return "\x1b[44m";if(!strcmp(c, "cyan_back")) return "\x1b[45m";if(!strcmp(c, "magenta_back")) return "\x1b[46m";if(!strcmp(c, "white_back")) return "\x1b[47m";return "";} template< typename T > cDBG & operator<<(T const &v)
Definition: dbg.h:274
cDBG::output
std::ostream * output
Definition: dbg.h:118
NAMESPACE_SDH_END
#define NAMESPACE_SDH_END
Definition: sdhlibrary_settings.h:80
cHexByteString::cHexByteString
cHexByteString(char const *_bytes, int _len)
ctor: create a cHexByteString with _len bytes at _bytes
Definition: dbg.h:335
operator<<
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::GetFlag
bool GetFlag(void) const
Definition: dbg.h:158
cDBG::SetColor
void SetColor(char const *color)
Definition: dbg.h:170
NULL
#define NULL
Definition: getopt1.c:56
cDBG::arglist
va_list arglist
Definition: dbg.h:208
cDBG::SetOutput
void SetOutput(std::ostream *fd)
Definition: dbg.h:185
cDBG::flush
std::ostream & flush()
Definition: dbg.h:289
cDBG::debug_color
char const * debug_color
Definition: dbg.h:116
cHexByteString::len
int len
Definition: dbg.h:332
cDBG::debug_flag
bool debug_flag
Definition: dbg.h:119
cDBG
A class to print colored debug messages.
Definition: dbg.h:113
NAMESPACE_SDH_START
#define NAMESPACE_SDH_START
Definition: sdhlibrary_settings.h:79
cDBG::normal_color
char const * normal_color
Definition: dbg.h:117
cDBG::~cDBG
~cDBG()
Definition: dbg.h:140
SDH__attribute__
#define SDH__attribute__(...)
Definition: sdh_command_codes.h:57
cHexByteString
dummy class for (debug) stream output of bytes as list of hex values
Definition: dbg.h:329
cDBG::SetFlag
void SetFlag(bool flag)
Definition: dbg.h:149
cHexByteString::bytes
char const * bytes
Definition: dbg.h:331
sdhlibrary_settings.h
This file contains settings to make the SDHLibrary compile on differen systems:
cDBG::cDBG
cDBG(bool flag=false, char const *color="red", std::ostream *fd=&std::cerr)
Definition: dbg.h:131


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Wed Mar 2 2022 01:00:58