fsc::DebugPrinter Class Reference

#include <DebugPrinter.hpp>

Description

Class for global static dout object.

 

Quick Usage Reference

For details, consult the Member Function Documentation of fsc::DebugPrinter, the Macros section of DebugPrinter.hpp, as well as the Examples.

// ...
// basic usage:
dout_HERE // print current file, line and function
dout_FUNC // print full current function signature
dout_STACK // print stack trace
dout_TYPE(std::map<T,U>) // print given type
dout_TYPE_OF(var) // print RTTI of variable
dout_VAL(var) // print highlighted 'name = value'
dout_PAUSE() // wait for user input (enter key)
dout_PAUSE(x < 10) // conditionally wait for user input
// advanced usage (non-exhaustive, check member details):
using fsc::dout;
dout << "foo" << std::endl;
dout, var , 5, " bar ", 6, " foobar ", 7, 8, std::endl;
dout(object); // highlight object
dout(object, label, " at "); // highlight label, object and separator
dout.stack(4, false, 2); // print 4 stack frames, omitting the first
dout = std::cout // set output stream
dout.set_precision(13) // set decimal display precision
dout.set_color("1;34") // set terminal highlighting color

In case the program terminates with SIGSEGV, SIGSYS, SIGABRT or SIGFPE, you will automatically get a stack trace from the raise location. To turn off this behaviour, check the Compilation section.

Definition at line 154 of file DebugPrinter.hpp.

Public Member Functions

 DebugPrinter ()
 Constructor for dout and user specified DebugPrinter objects.
 
 DebugPrinter (const DebugPrinter &)=delete
 Deleted copy constructor.
 
 DebugPrinter (DebugPrinter &&)=delete
 Deleted move constructor.
 
void operator= (std::ostream &os) noexcept
 Assignment operator for changing streams. More...
 
template<typename T >
auto operator= (T &&os) -> std::enable_if_t< std::is_move_assignable< T >::value &&std::is_rvalue_reference< decltype(os)>::value >
 Assignment operator for moving streams. More...
 
void set_precision (const std::streamsize prec) noexcept
 Number of displayed decimal digits. More...
 
void set_color (const std::string str)
 Highlighting color. More...
 
void set_color () noexcept
 Remove highlighting color. More...
 
template<typename T , typename U >
void operator() (const T &label, U const &obj, const std::string sc=": ") const
 Print highlighted label and object. More...
 
template<typename T >
void operator() (const T &obj) const
 Print highlighted object. More...
 
void stack (const int backtrace_size=max_backtrace, const bool compact=false, const int begin=1) const
 Print a stack trace. More...
 

Friends

template<typename T >
DebugPrinteroperator<< (DebugPrinter &, const T &)
 operator<< overload for std::ostream
 
DebugPrinteroperator<< (DebugPrinter &, std::ostream &(*pf)(std::ostream &))
 operator<< overload for std::ostream manipulators
 

Member Function Documentation

template<typename T , typename U >
void fsc::DebugPrinter::operator() ( const T &  label,
U const &  obj,
const std::string  sc = ": " 
) const
inline

Print highlighted label and object.

Parameters
labelshould have a std::ostream & operator<< overload
objshould have a std::ostream & operator<< overload
scdelimiter between label and object

If label or obj don't have the required operator << overloads, the function will print an error instead. Example usage:

dout(label, object);
dout(label, object, "\t");

Definition at line 284 of file DebugPrinter.hpp.

template<typename T >
void fsc::DebugPrinter::operator() ( const T &  obj) const
inline

Print highlighted object.

Parameters
objshould have a std::ostream & operator<< overload

Equivalent to

dout(">>>", obj, " ");

Example usage:

dout(object);

Definition at line 300 of file DebugPrinter.hpp.

void fsc::DebugPrinter::operator= ( std::ostream &  os)
inlinenoexcept

Assignment operator for changing streams.

Parameters
osoutput stream to use

Default == std::cout. Change to any std::ostream with:

dout = std::cerr;

The DebugPrinter assumes that the object is managed elsewhere (to have it take ownership, check the assigment operator for moving streams).

Definition at line 204 of file DebugPrinter.hpp.

template<typename T >
auto fsc::DebugPrinter::operator= ( T &&  os) -> std::enable_if_t<std::is_move_assignable<T>::value && std::is_rvalue_reference<decltype(os)>::value>
inline

Assignment operator for moving streams.

Parameters
osoutput stream to take over

Use to pass ownership if the stream object would leave scope.

if(file_write == true) {
std::ofstream fs("debug.log");
dout = std::move(fs);
dout.set_color();
} // fs gets destroyed here
dout << "This shows up in debug.log";

Note: trying to move std::cout (or other static standard streams) is considered a bad life choice.

Definition at line 221 of file DebugPrinter.hpp.

void fsc::DebugPrinter::set_color ( const std::string  str)
inline

Highlighting color.

Parameters
strcolor code

Assumes a bash compatible terminal and sets the operator() highlighting color (also used for dout_HERE and dout_VAL), for example red == "0;31" (default). Example usage:

dout.set_color("1;34");

For bash color codes check http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

Definition at line 249 of file DebugPrinter.hpp.

void fsc::DebugPrinter::set_color ( )
inlinenoexcept

Remove highlighting color.

No color highlighting (e.g. when writing to a file). Example usage:

dout.set_color();

Definition at line 263 of file DebugPrinter.hpp.

void fsc::DebugPrinter::set_precision ( const std::streamsize  prec)
inlinenoexcept

Number of displayed decimal digits.

Parameters
precdesired precision

Default == 5. Example usage:

dout.set_precision(13);

Definition at line 235 of file DebugPrinter.hpp.

void fsc::DebugPrinter::stack ( const int  backtrace_size = max_backtrace,
const bool  compact = false,
const int  begin = 1 
) const
inline

Print a stack trace.

Parameters
backtrace_sizeprint at most this many frames
compactonly print function names
beginstarting offset

Print one line per stack frame, consisting of the binary object name, the demangled function name, and the offset within the function and within the binary. Example usage:

dout.stack(); // print a full stack trace
dout.stack(count); // print at most (int)count frames
dout.stack(count, true); // print in compact format
dout.stack(count, true, 2); // and slice off the "first" frame
dout_FUNC // shortcut for dout.stack(1, true);

Definition at line 324 of file DebugPrinter.hpp.


The documentation for this class was generated from the following file: