From 7be0c797ad6bccdd63b033129c49264bdd94d43d Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Fri, 8 Sep 2017 10:04:41 -0400 Subject: [PATCH] Don't print backtraces from inside Python. This is done by adding a 'backtrace' boolean parameter to PrefixedOutStream. --- src/mlpack/bindings/python/mlpack/cli_util.hpp | 8 ++++++++ src/mlpack/bindings/python/print_pyx.cpp | 6 +++++- src/mlpack/core/util/prefixedoutstream.hpp | 10 +++++++++- src/mlpack/core/util/prefixedoutstream_impl.hpp | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/mlpack/bindings/python/mlpack/cli_util.hpp b/src/mlpack/bindings/python/mlpack/cli_util.hpp index 306548e12f..cc4aecee81 100644 --- a/src/mlpack/bindings/python/mlpack/cli_util.hpp +++ b/src/mlpack/bindings/python/mlpack/cli_util.hpp @@ -100,6 +100,14 @@ inline void EnableVerbose() Log::Info.ignoreInput = false; } +/** + * Disable backtraces. + */ +inline void DisableBacktrace() +{ + Log::Fatal.backtrace = false; +} + } // namespace util } // namespace mlpack diff --git a/src/mlpack/bindings/python/print_pyx.cpp b/src/mlpack/bindings/python/print_pyx.cpp index 5a0f1d566f..2a0b80be08 100644 --- a/src/mlpack/bindings/python/print_pyx.cpp +++ b/src/mlpack/bindings/python/print_pyx.cpp @@ -72,7 +72,8 @@ void PrintPYX(const ProgramDoc& programInfo, cout << "cimport arma_numpy" << endl; cout << "from cli cimport CLI" << endl; cout << "from cli cimport SetParam, SetParamWithInfo" << endl; - cout << "from cli cimport EnableVerbose" << endl; + cout << "from cli cimport EnableVerbose, DisableBacktrace" + << endl; cout << "from cli cimport MoveFromPtr, MoveToPtr" << endl; cout << "from matrix_utils import to_matrix, to_matrix_with_info" << endl; cout << endl; @@ -168,6 +169,9 @@ void PrintPYX(const ProgramDoc& programInfo, << "returned." << endl; cout << " \"\"\"" << endl; + // Disable backtraces. + cout << " DisableBacktrace()" << endl; + // Restore the parameters. cout << " CLI.RestoreSettings(\"" << programInfo.programName << "\")"; diff --git a/src/mlpack/core/util/prefixedoutstream.hpp b/src/mlpack/core/util/prefixedoutstream.hpp index b01cd2af96..6078edde1b 100644 --- a/src/mlpack/core/util/prefixedoutstream.hpp +++ b/src/mlpack/core/util/prefixedoutstream.hpp @@ -54,13 +54,17 @@ class PrefixedOutStream * @param ignoreInput If true, the stream will not be printed. * @param fatal If true, a std::runtime_error exception is thrown after * printing a newline. + * @param backtrace If true, attempt to print a backtrace (will only be + * done if HAS_BFD_DL is defined). */ PrefixedOutStream(std::ostream& destination, const char* prefix, bool ignoreInput = false, - bool fatal = false) : + bool fatal = false, + bool backtrace = true) : destination(destination), ignoreInput(ignoreInput), + backtrace(backtrace), prefix(prefix), // We want the first call to operator<< to prefix the prefix so we set // carriageReturned to true. @@ -113,6 +117,10 @@ class PrefixedOutStream //! Discards input, prints nothing if true. bool ignoreInput; + //! If true, on a fatal error, a backtrace will be printed if HAS_BFD_DL is + //! defined. + bool backtrace; + private: /** * Conducts the base logic required in all the operator << overloads. Mostly diff --git a/src/mlpack/core/util/prefixedoutstream_impl.hpp b/src/mlpack/core/util/prefixedoutstream_impl.hpp index bd4faf5e3c..0ac5f06d71 100644 --- a/src/mlpack/core/util/prefixedoutstream_impl.hpp +++ b/src/mlpack/core/util/prefixedoutstream_impl.hpp @@ -113,7 +113,7 @@ PrefixedOutStream::BaseLogic(const T& val) // Print a backtrace, if we can. #ifdef HAS_BFD_DL - if (fatal && !ignoreInput) + if (fatal && !ignoreInput && backtrace) { size_t nl; size_t pos = 0;