Namespaces | Classes | Functions
lar::debug Namespace Reference

Namespaces

 details
 

Classes

struct  BacktracePrintOptions
 Backtrace printing options. More...
 
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...
 
struct  static_assert_on
 
struct  static_assert_on< T, false >
 

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, BacktracePrintOptions options)
 Prints the full backtrace into a stream. More...
 
template<typename Stream >
void printBacktrace (Stream &&out)
 Prints the full backtrace into a stream with default options. More...
 
template<typename Stream >
void printBacktrace (Stream &&out, unsigned int maxLines, std::string indent=" ", CallInfoPrinter::opt const *callInfoOptions=nullptr)
 Prints the full backtrace into a stream. More...
 

Function Documentation

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

Outputs a demangled name for type T.


Template 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 = lar::debug::demangle<std::string>();

or via a argument pointer:

Definition at line 348 of file DebugUtils.h.

349  { return cet::demangle_symbol(typeid(std::decay_t<T>).name()); }
static QCString name
Definition: declinfo.cpp:673
template<typename Stream >
Stream & lar::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 394 of file DebugUtils.h.

394  {
395  CallInfoPrinter print;
396  print(std::forward<Stream>(out), info);
397  return out;
398  }
template<typename Stream >
void lar::debug::printBacktrace ( Stream &&  out,
BacktracePrintOptions  options 
)

Prints the full backtrace into a stream.

Template Parameters
Streamtype of output stream
Parameters
outthe output stream to insert output into
optionsprinting options (see BacktracePrintOptions)

Definition at line 403 of file DebugUtils.h.

403  {
404  unsigned int nSkip = std::max(options.skipLines, 0U);
405  std::vector<void*> buffer
406  (nSkip + std::max(options.maxLines, 200U), nullptr);
407 
408  unsigned int const nItems
409  = (unsigned int) backtrace(buffer.data(), buffer.size());
410 
411  // convert the calls in the buffer into a vector of strings
412  char** symbols = backtrace_symbols(buffer.data(), buffer.size());
413  if (!symbols) {
414  out << options.firstIndent << "<failed to get the call stack>\n"
415  << std::flush;
416  return;
417  }
418  std::vector<CallInfo_t> callStack;
419  for (size_t i = 0; i < buffer.size(); ++i)
420  callStack.push_back((const char*) symbols[i]);
421  std::free(symbols);
422 
423  size_t lastItem = nSkip + options.maxLines;
424  if (lastItem > nItems) lastItem = nItems;
425  if (lastItem >= buffer.size()) --lastItem;
426 
427  CallInfoPrinter print(options.callInfoOptions);
428  for (size_t i = nSkip; i < lastItem; ++i) {
429  out << (i == 0? options.firstIndent: options.indent);
430  print(std::forward<Stream>(out), callStack[i]);
431  out << "\n";
432  }
433  if ((lastItem < nItems) && options.countOthers) {
434  out << options.indent << " ... and other " << (nItems - lastItem);
435  if (nItems == buffer.size()) out << " (or more)";
436  out << " levels\n";
437  }
438  out << std::flush;
439 
440  } // printBacktrace()
QTextStream & flush(QTextStream &s)
static int max(int a, int b)
template<typename Stream >
void lar::debug::printBacktrace ( Stream &&  out)

Prints the full backtrace into a stream with default options.

Template Parameters
Streamtype of output stream
Parameters
outthe output stream to insert output into

Definition at line 236 of file DebugUtils.h.

237  { printBacktrace(std::forward<Stream>(out), BacktracePrintOptions()); }
void printBacktrace(Stream &&out, unsigned int maxLines, std::string indent=" ", CallInfoPrinter::opt const *callInfoOptions=nullptr)
Prints the full backtrace into a stream.
Definition: DebugUtils.h:445
template<typename Stream >
void lar::debug::printBacktrace ( Stream &&  out,
unsigned int  maxLines,
std::string  indent = "  ",
CallInfoPrinter::opt const *  callInfoOptions = 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: " ")
callInfoOptionsuse these output options (default ones if null)

The call information output options are described in CallInfoPrinter::opt structure.

Definition at line 445 of file DebugUtils.h.

450  {
451  BacktracePrintOptions options;
452  options.maxLines = maxLines;
453  options.indent = options.firstIndent = indent;
454  if (callInfoOptions) options.callInfoOptions = *callInfoOptions;
455  printBacktrace(std::forward<Stream>(out), options);
456  }
void printBacktrace(Stream &&out, unsigned int maxLines, std::string indent=" ", CallInfoPrinter::opt const *callInfoOptions=nullptr)
Prints the full backtrace into a stream.
Definition: DebugUtils.h:445