Classes | Functions
gar::debug Namespace Reference

Classes

struct  CallInfo_t
 Structure with information about a single call, parsed. More...
 
class  CallInfoPrinter
 Class handling the output of information in a CallInfo_t object. More...
 

Functions

template<typename T >
std::string demangle (T const *=nullptr)
 Outputs a demangled name for type T. More...
 
template<typename Stream >
Stream & operator<< (Stream &&out, CallInfo_t const &info)
 Helper operator to insert a call information in a stream with default options. More...
 
template<typename Stream >
void printBacktrace (Stream &&out, unsigned int maxLines=5, std::string indent=" ", CallInfoPrinter::opt const *options=nullptr)
 Prints the full backtrace into a stream. More...
 

Function Documentation

template<typename T >
std::string gar::debug::demangle ( T const *  = nullptr)
inline

Outputs a demangled name for type T.


Parameters
Ttype whose name must be demangled (optional)
Returns
a string with demangled name

It relies on cetlib. The type to be demangled can be specified either as template argument:

auto name = gar::debug::demangle<std::string>();

or via a argument pointer:

auto name = gar::debug::demangle(this);

Definition at line 56 of file DebugUtils.h.

57  { return cet::demangle_symbol(typeid(std::decay_t<T>).name()); }
static QCString name
Definition: declinfo.cpp:673
template<typename Stream >
Stream& gar::debug::operator<< ( Stream &&  out,
CallInfo_t const &  info 
)
inline

Helper operator to insert a call information in a stream with default options.

Definition at line 229 of file DebugUtils.h.

230  {
231  CallInfoPrinter print;
232  print(std::forward<Stream>(out), info);
233  return out;
234  }
template<typename Stream >
void gar::debug::printBacktrace ( Stream &&  out,
unsigned int  maxLines = 5,
std::string  indent = "  ",
CallInfoPrinter::opt const *  options = nullptr 
)

Prints the full backtrace into a stream.

Template Parameters
Streamtype of output stream
Parameters
outthe output stream to insert output into
maxLinesprint at most this many lines in the output (default: 5)
indentprepend a string in front of any new line (default: " ")
optionsuse these output options (default ones if null pointer)

The output options are described in CallInfo_t::opt structure.

Definition at line 249 of file DebugUtils.h.

254  {
255  constexpr unsigned int nSkip = 1;
256  std::vector<void*> buffer(nSkip + std::max(maxLines, 200U), nullptr);
257 
258  unsigned int nItems
259  = (unsigned int) backtrace(buffer.data(), buffer.size());
260 
261  // convert the calls in the buffer into a vector of strings
262  char** symbols = backtrace_symbols(buffer.data(), buffer.size());
263  if (!symbols) {
264  out << indent << "<failed to get the call stack>" << std::endl;
265  }
266  std::vector<CallInfo_t> callStack;
267  for (size_t i = 0; i < buffer.size(); ++i)
268  callStack.push_back((const char*) symbols[i]);
269  std::free(symbols);
270 
271  size_t lastItem = nSkip + maxLines;
272  if (lastItem > nItems) lastItem = nItems;
273  if (lastItem >= buffer.size()) --lastItem;
274 
275  CallInfoPrinter print;
276  if (options) print.setOptions(*options);
277  for (size_t i = nSkip; i < lastItem; ++i) {
278  out << indent;
279  print(std::forward<Stream>(out), callStack[i]);
280  out << "\n";
281  }
282  if (lastItem < nItems) {
283  out << indent << " ... and other " << (nItems - lastItem);
284  if (nItems == buffer.size()) out << " (or more)";
285  out << " levels\n";
286  }
287  out << std::flush;
288 
289  } // printBacktrace()
QTextStream & flush(QTextStream &s)
static int max(int a, int b)
QTextStream & endl(QTextStream &s)