From 5e2f44528ca89c1963064f4b39ff77817865c8b5 Mon Sep 17 00:00:00 2001 From: Grzegorz Krajewski Date: Thu, 25 Feb 2016 16:52:37 +0100 Subject: [PATCH] Add mlpack::backtrace Prints backtrace for Log::Fatal. --- .../core/util/prefixedoutstream_impl.hpp | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/mlpack/core/util/prefixedoutstream_impl.hpp b/src/mlpack/core/util/prefixedoutstream_impl.hpp index d86763fed8..bafc15131f 100644 --- a/src/mlpack/core/util/prefixedoutstream_impl.hpp +++ b/src/mlpack/core/util/prefixedoutstream_impl.hpp @@ -10,6 +10,7 @@ // Just in case it hasn't been included. #include "prefixedoutstream.hpp" +#include "backtrace.hpp" #include namespace mlpack { @@ -61,11 +62,31 @@ void PrefixedOutStream::BaseLogic(const T& val) return; } - // Now, we need to check for newlines in this line. If we find one, output - // up until the newline, then output the newline and the prefix and continue - // looking. + // Now, we need to check for newlines in retrieved backtrace. + // If we find one, output up until the newline, then output the newline + // and the prefix and continue looking. size_t nl; size_t pos = 0; + if(fatal) + { + Backtrace bt; + std::string btLine = bt.ToString(); + while ((nl = btLine.find('\n', pos)) != std::string::npos) + { + PrefixIfNeeded(); + + destination << btLine.substr(pos, nl - pos); + destination << std::endl; + newlined = true; + + carriageReturned = true; // Regardless of whether or not we display it. + + pos = nl + 1; + } + } + + //The same logic like above, but this time for 'line'. + pos = 0; while ((nl = line.find('\n', pos)) != std::string::npos) { PrefixIfNeeded(); @@ -93,7 +114,10 @@ void PrefixedOutStream::BaseLogic(const T& val) // If we displayed a newline and we need to throw afterwards, do that. if (fatal && newlined) + { + std::cout << std::endl; throw std::runtime_error("fatal error; see Log::Fatal output"); + } } // This is an inline function (that is why it is here and not in .cc).