Public Member Functions | Public Attributes | Protected Attributes | List of all members
cDBG Class Reference

A class to print colored debug messages. More...

#include <dbg.h>

Public Member Functions

 cDBG (bool flag=false, char const *color="red", std::ostream *fd=&std::cerr)
 
std::ostream & flush ()
 
bool GetFlag (void) const
 
*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 > cDBGoperator<< (T const &v)
 
void PDM (char const *fmt,...) SDH__attribute__((format(printf
 
void SetColor (char const *color)
 
void SetFlag (bool flag)
 
void SetOutput (std::ostream *fd)
 
 va_end (arglist)
 
 va_start (arglist, fmt)
 
 vsnprintf (buffer, 256, fmt, arglist)
 
 ~cDBG ()
 

Public Attributes

va_list arglist
 
void char buffer [256]
 

Protected Attributes

char const * debug_color
 
bool debug_flag
 
std::streamsize mywidth
 
char const * normal_color
 
std::ostream * output
 

Detailed Description

A class to print colored debug messages.

If the environment variable "SDH_NO_COLOR" is defined then the messages are printed without coloring (usefull for logging or if your terminal does not support colors.

Example:

#include "sdh/dbg.h"
d = cDBG( true );
g = cDBG( true, "green" );
d.PDM( "This message is printed in default color red" );
g << "and this one in a nice green ";
g << "of course you can debug print any objects that have a string representation: " << 08 << 15 << true;
g << "Messages can be turned of and on, e.g. selected by command line options";
g.SetFlag(false);
g << "This messages is not printed";

Definition at line 113 of file dbg.h.

Constructor & Destructor Documentation

◆ cDBG()

cDBG::cDBG ( bool  flag = false,
char const *  color = "red",
std::ostream *  fd = &std::cerr 
)
inline

constructor: construct a cDBG object

Parameters
flag- the initial state of the flag, if true then messages sent to the object are printed. Default is false. Can be changed with SetFlag()
color- the name of the color to use, default is "red". Can be changed with SetColor()
fd- the ostream to use for output, default is stderr. Can be changed with SetOutput()

Definition at line 131 of file dbg.h.

◆ ~cDBG()

cDBG::~cDBG ( )
inline

Definition at line 140 of file dbg.h.

Member Function Documentation

◆ flush()

std::ostream& cDBG::flush ( )
inline

Definition at line 289 of file dbg.h.

◆ GetFlag()

bool cDBG::GetFlag ( void  ) const
inline

Get debug_flag of this cDBG object.

Definition at line 158 of file dbg.h.

◆ 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& cDBG::operator<< ( T const &  v)
inline

Definition at line 274 of file dbg.h.

◆ PDM()

void cDBG::PDM ( char const *  fmt,
  ... 
)

Print debug messages of printf like fmt, ... in the color set with SetColor, but only if debug_flag is true.

◆ SetColor()

void cDBG::SetColor ( char const *  color)
inline

Set debug_color of this cDBG object to color. color is a string like "red", see util.py for valid names.

Attention
The string is NOT copied, just a pointer is stored

Definition at line 170 of file dbg.h.

◆ SetFlag()

void cDBG::SetFlag ( bool  flag)
inline

Set debug_flag of this cDBG object to flag. After setting the flag to true debug messages are printed, else not.

Definition at line 149 of file dbg.h.

◆ SetOutput()

void cDBG::SetOutput ( std::ostream *  fd)
inline

Set output of this cDBG object to fd, which must be a file like object like sys.stderr

Definition at line 185 of file dbg.h.

◆ va_end()

cDBG::va_end ( arglist  )

◆ va_start()

cDBG::va_start ( arglist  ,
fmt   
)

◆ vsnprintf()

cDBG::vsnprintf ( buffer  ,
256  ,
fmt  ,
arglist   
)

Member Data Documentation

◆ arglist

va_list cDBG::arglist

Definition at line 208 of file dbg.h.

◆ buffer

void char cDBG::buffer[256]
Initial value:
{
if (!debug_flag) return

Definition at line 203 of file dbg.h.

◆ debug_color

char const* cDBG::debug_color
protected

Definition at line 116 of file dbg.h.

◆ debug_flag

bool cDBG::debug_flag
protected

Definition at line 119 of file dbg.h.

◆ mywidth

std::streamsize cDBG::mywidth
protected

Definition at line 120 of file dbg.h.

◆ normal_color

char const* cDBG::normal_color
protected

Definition at line 117 of file dbg.h.

◆ output

std::ostream* cDBG::output
protected

Definition at line 118 of file dbg.h.


The documentation for this class was generated from the following file:
dbg.h
This file contains interface and implementation of class #SDH::cDBG, a class for colorfull debug mess...
cDBG::debug_flag
bool debug_flag
Definition: dbg.h:119
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:59