From 1bcdf6aafbbfced8252eedfcd4f221f260675467 Mon Sep 17 00:00:00 2001 From: vesis84 Date: Wed, 4 May 2016 12:14:33 +0200 Subject: [PATCH] base/kaldi_error : the error messages are no longer printed 2x - e.what() contains stackttrace or is empty string - we should also consider changing: 'std::cerr << e.what();' -> 'fprintf(stderr, e.what().c_str());' - fprintf is thread-safe and it is better not to mix 'std::cerr' and 'stderr', and 'stderr' is already used for logging... --- src/base/kaldi-error.cc | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/base/kaldi-error.cc b/src/base/kaldi-error.cc index f5e83ad55..b02d59520 100644 --- a/src/base/kaldi-error.cc +++ b/src/base/kaldi-error.cc @@ -181,22 +181,31 @@ MessageLogger::~MessageLogger() KALDI_NOEXCEPT(false) { std::string str = ss_.str(); while (!str.empty() && str[str.length() - 1] == '\n') str.resize(str.length() - 1); + + // print the mesage (or send to logging handler), SendToLog(envelope_, str.c_str()); - if (envelope_.severity > LogMessageEnvelope::Error) + if (envelope_.severity > LogMessageEnvelope::Error) { + // We are done, it was not 'KALDI_ERR << msg', return; - - // On error, throw an exception with the message, plus traceback info if - // available. - if (!std::uncaught_exception()) { -#ifdef HAVE_EXECINFO_H - throw std::runtime_error(str + "\n\n[stack trace: ]\n" + - KaldiGetStackTrace() + "\n"); -#else - throw std::runtime_error(str); -#endif } else { - abort(); + if (std::uncaught_exception()) { + // We get here, if there was an exception on this thread that has not + // yet arrived to its 'catch' clause... (can happen if an exception + // triggers a destructor and the destructor calls 'KALDI_ERR << msg'). + // Throwing a new exception would be unsafe! + abort(); + } else { + // On error we throw an exception. + // - 'what()' contains stack-trace or is empty. + // - the message was printed in 'SendToLog(...)'. +#ifdef HAVE_EXECINFO_H + throw std::runtime_error("\n[stack trace: ]\n" + + KaldiGetStackTrace() + "\n"); +#else + throw std::runtime_error(""); +#endif + } } }