DebugPrinter.hpp File Reference
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <typeinfo>
#include <cstdlib>
#include <algorithm>
#include <stdexcept>
#include <memory>
#include <type_traits>
#include <execinfo.h>
#include <cxxabi.h>
#include <signal.h>
#include <map>

Go to the source code of this file.

Description

DebugPrinter header-only lib.

Creates a static object named dout and defines debugging macros.

Version
2015.1110
Author
Year Name
2011-2015 Donjan Rodic
2015 Mario Könz
2015 C. Frescolino

 

Compilation

DebugPrinter requires C++14.

Link with -rdynamic in order to get proper stack() frame names and useful dout_FUNC output.

Note: compiler optimisations may inline functions (shorter stack).

Pass DEBUGPRINTER_OFF to turn off all functionality provided here. The debug statements can be left in the code, since all methods and macros become inline and trivial, and thus will be optimised away by the compiler at sufficient optimisation flags. This flag will be automatically assumed if the standard NDEBUG is passed.

Pass DEBUGPRINTER_NO_EXECINFO flag on Windows (makes stack(), dout_STACK and dout_FUNC trivial).

Pass DEBUGPRINTER_NO_CXXABI if you don't have a cxxabi demangle call in your libc distribution (this translates raw type symbols, i.e. the typeid(std::string).name() output Ss to std::string). The stack and type methods will then print the mangled names and a c++filt-ready output.

Pass DEBUGPRINTER_NO_SIGNALS to turn off automatic stack tracing when certain fatal signals occur. Passing this flag is recommended on non-Unix-like systems.

Definition in file DebugPrinter.hpp.

Classes

class  fsc::DebugPrinter
 Class for global static dout object. More...
 

Namespaces

 fsc
 General fsc namespace.
 

Macros

#define DEBUGPRINTER_DIRSEP   '/'
 
#define dout_HERE
 Print current line in the form filename:line (function) More...
 
#define dout_FUNC
 Print current function signature. More...
 
#define dout_VAL(...)
 Print highlighted 'name = value' of given variable or expression. More...
 
#define dout_TYPE(...)
 Print demangled type information of given type. More...
 
#define dout_TYPE_OF(...)
 Print demangled type information of given variable or expression. More...
 
#define dout_STACK
 Print a stack trace. More...
 
#define dout_PAUSE(...)
 Pause execution (optionally) and wait for user key press (ENTER). More...
 

Functions

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

Macro Definition Documentation

#define dout_FUNC

Print current function signature.

Example usage:

template<typename T> void f(T&&) {
}

Shortcut for:

fsc::dout.stack(1, true);

Needs to be compiled with -rdynamic (you will get an error message otherwise)

Examples:
01_types.cpp.

Definition at line 750 of file DebugPrinter.hpp.

#define dout_HERE

Print current line in the form filename:line (function)

Example usage:

Shortcut for (pseudocode):

fsc::dout(__FILE__ ,__LINE__ + __func__, ":");
Examples:
01_types.cpp, 02_flow.cpp, and 03_advanced.cpp.

Definition at line 731 of file DebugPrinter.hpp.

#define dout_PAUSE (   ...)

Pause execution (optionally) and wait for user key press (ENTER).

Parameters
...
A string literal can be specified as argument to act as label.
A pause condition can be specified as argument. This must be a valid expression for an if-statement.

Example usage:

dout_PAUSE("label")
for(int i = 0; i < 10; ++i)
dout_PAUSE(i >= 8)
Examples:
02_flow.cpp.

Definition at line 835 of file DebugPrinter.hpp.

#define dout_STACK

Print a stack trace.

Example usage:

void f2() {
}
void f1() {
f2();
}

Shortcut for

fsc::dout.stack();

Needs to be compiled with -rdynamic (you will get an error message otherwise). Keep in mind that compiler optimisations will inline functions.

Examples:
02_flow.cpp.

Definition at line 819 of file DebugPrinter.hpp.

#define dout_TYPE (   ...)

Print demangled type information of given type.

Parameters
...can't be an incomplete type.

This macro prints the instantiated demangled input type.
Example usage:

dout_TYPE(std::map<T,U>) // in a template
Examples:
01_types.cpp.

Definition at line 779 of file DebugPrinter.hpp.

#define dout_TYPE_OF (   ...)

Print demangled type information of given variable or expression.

Parameters
...can't have an incomplete type.

This macro prints the demangled (as specified by the local cxxabi) RTTI of the input variable or resolved expression, including cvr qualifiers and valueness.
Example usage:

dout_TYPE_OF(var) // var is an object
dout_TYPE_OF(std::cout << 1 << std::endl;)
Examples:
01_types.cpp.

Definition at line 795 of file DebugPrinter.hpp.

#define dout_VAL (   ...)

Print highlighted 'name = value' of given variable or expression.

Parameters
...the expression to print; must be streamable into the stream object assigned to the currently used fsc::DebugPrinter

Example usage:

int x = 5;
dout_VAL(1==2)

This is a shortcut for preprocessed equivalent of:

fsc::dout(#expression, expression, " = ");
Examples:
01_types.cpp, and 02_flow.cpp.

Definition at line 768 of file DebugPrinter.hpp.