LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
lar::debug Namespace Reference

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...
 

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:

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

Definition at line 56 of file DebugUtils.h.

57  { return cet::demangle_symbol(typeid(std::decay_t<T>).name()); }
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 228 of file DebugUtils.h.

229  {
230  CallInfoPrinter print;
231  print(std::forward<Stream>(out), info);
232  return out;
233  }
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 262 of file DebugUtils.h.

References lar::debug::BacktracePrintOptions::callInfoOptions, lar::debug::BacktracePrintOptions::countOthers, lar::debug::BacktracePrintOptions::firstIndent, lar::debug::BacktracePrintOptions::indent, max, lar::debug::BacktracePrintOptions::maxLines, and lar::debug::BacktracePrintOptions::skipLines.

Referenced by printBacktrace().

262  {
263  unsigned int nSkip = std::max(options.skipLines, 0U);
264  std::vector<void*> buffer
265  (nSkip + std::max(options.maxLines, 200U), nullptr);
266 
267  unsigned int const nItems
268  = (unsigned int) backtrace(buffer.data(), buffer.size());
269 
270  // convert the calls in the buffer into a vector of strings
271  char** symbols = backtrace_symbols(buffer.data(), buffer.size());
272  if (!symbols) {
273  out << options.firstIndent << "<failed to get the call stack>\n"
274  << std::flush;
275  return;
276  }
277  std::vector<CallInfo_t> callStack;
278  for (size_t i = 0; i < buffer.size(); ++i)
279  callStack.push_back((const char*) symbols[i]);
280  std::free(symbols);
281 
282  size_t lastItem = nSkip + options.maxLines;
283  if (lastItem > nItems) lastItem = nItems;
284  if (lastItem >= buffer.size()) --lastItem;
285 
286  CallInfoPrinter print(options.callInfoOptions);
287  for (size_t i = nSkip; i < lastItem; ++i) {
288  out << (i == 0? options.firstIndent: options.indent);
289  print(std::forward<Stream>(out), callStack[i]);
290  out << "\n";
291  }
292  if ((lastItem < nItems) && options.countOthers) {
293  out << options.indent << " ... and other " << (nItems - lastItem);
294  if (nItems == buffer.size()) out << " (or more)";
295  out << " levels\n";
296  }
297  out << std::flush;
298 
299  } // printBacktrace()
Int_t max
Definition: plot.C:27
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 307 of file DebugUtils.h.

References printBacktrace().

308  { 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:323
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 323 of file DebugUtils.h.

References lar::debug::BacktracePrintOptions::callInfoOptions, lar::debug::BacktracePrintOptions::firstIndent, art::detail::indent(), lar::debug::BacktracePrintOptions::indent, lar::debug::BacktracePrintOptions::maxLines, and printBacktrace().

328  {
329  BacktracePrintOptions options;
330  options.maxLines = maxLines;
331  options.indent = options.firstIndent = indent;
332  if (callInfoOptions) options.callInfoOptions = *callInfoOptions;
333  printBacktrace(std::forward<Stream>(out), options);
334  }
std::string indent(std::size_t const i)
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:323