Skip to content

Commit

Permalink
base/kaldi_error : integrating comments from Dan.
Browse files Browse the repository at this point in the history
  • Loading branch information
KarelVesely84 committed May 10, 2016
1 parent 24bef8d commit a4fff0d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 87 deletions.
74 changes: 37 additions & 37 deletions src/base/kaldi-error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
// limitations under the License.

#ifdef HAVE_EXECINFO_H
#include <execinfo.h> // To get stack trace in error messages.
// If this #include fails there is an error in the Makefile, it does not
// support your platform well. Make sure HAVE_EXECINFO_H is undefined,
// and the code will compile.
#ifdef HAVE_CXXABI_H
#include <cxxabi.h> // For name demangling.
// Useful to decode the stack trace, but only used if we have execinfo.h
#endif // HAVE_CXXABI_H
#include <execinfo.h> // To get stack trace in error messages.
// If this #include fails there is an error in the Makefile, it does not
// support your platform well. Make sure HAVE_EXECINFO_H is undefined,
// and the code will compile.
#ifdef HAVE_CXXABI_H
#include <cxxabi.h> // For name demangling.
// Useful to decode the stack trace, but only used if we have execinfo.h
#endif // HAVE_CXXABI_H
#endif // HAVE_EXECINFO_H

#include "base/kaldi-common.h"
Expand All @@ -48,6 +48,23 @@ const char *GetProgramName() {
}


/***** HELPER FUNCTIONS *****/

// Given a filename like "/a/b/c/d/e/f.cc", GetShortFileName
// returns "e/f.cc". Does not currently work if backslash is
// the filename separator.
static const char *GetShortFileName(const char *filename) {
const char *last_slash = strrchr(filename, '/');
if (!last_slash) {
return filename;
} else {
while (last_slash > filename && last_slash[-1] != '/')
last_slash--;
return last_slash;
}
}


/***** STACKTRACE *****/

static std::string Demangle(std::string trace_name) {
Expand Down Expand Up @@ -84,8 +101,8 @@ static std::string Demangle(std::string trace_name) {
static std::string KaldiGetStackTrace() {
std::string ans;
#ifdef HAVE_EXECINFO_H
#define KALDI_MAX_TRACE_SIZE 50
#define KALDI_MAX_TRACE_PRINT 20 // must be even.
#define KALDI_MAX_TRACE_SIZE 50
#define KALDI_MAX_TRACE_PRINT 20 // must be even.
// buffer for the trace,
void *trace[KALDI_MAX_TRACE_SIZE];
// get the trace,
Expand Down Expand Up @@ -120,21 +137,6 @@ static std::string KaldiGetStackTrace() {

/***** KALDI LOGIGNG *****/

// Given a filename like "/a/b/c/d/e/f.cc", GetShortFileName
// returns "e/f.cc". Does not currently work if backslash is
// the filename separator.
static const char *GetShortFileName(const char *filename) {
const char *last_slash = strrchr(filename, '/');
if (!last_slash) {
return filename;
} else {
while (last_slash > filename && last_slash[-1] != '/')
last_slash--;
return last_slash;
}
}


MessageLogger::MessageLogger(LogMessageEnvelope::Severity severity,
const char *func, const char *file, int32 line) {
// Obviously, we assume the strings survive the destruction of this object.
Expand All @@ -152,12 +154,12 @@ MessageLogger::~MessageLogger() KALDI_NOEXCEPT(false) {
str.resize(str.length() - 1);

// print the mesage (or send to logging handler),
MessageLogger::SendToLog(envelope_, str.c_str());
MessageLogger::HandleMessage(envelope_, str.c_str());
}


void MessageLogger::SendToLog(const LogMessageEnvelope &envelope,
const char *message) {
void MessageLogger::HandleMessage(const LogMessageEnvelope &envelope,
const char *message) {
// Send to a logging handler if provided.
if (g_log_handler != NULL) {
g_log_handler(envelope, message);
Expand Down Expand Up @@ -198,20 +200,19 @@ void MessageLogger::SendToLog(const LogMessageEnvelope &envelope,
// print to stderr,
fprintf(stderr, "%s %s\n", header.str().c_str(), message);
} else if (envelope.severity == LogMessageEnvelope::Error) {
// ERROR:
// throw exception with 'what()' message (contains stack-trace),
std::string what_arg = header.str() + " " +
message + "\n\n" +
KaldiGetStackTrace() + "\n";
// ERROR:
// print to stderr (with stack-trace),
fprintf(stderr, "%s %s\n\n%s\n", header.str().c_str(), message,
KaldiGetStackTrace().c_str());
if (!std::uncaught_exception()) {
throw std::runtime_error(what_arg);
// throw exception with empty message,
throw std::runtime_error("");
} else {
// If we got here, this thread has already thrown exception,
// and this exception has not yet arrived to its 'catch' clause...
// Throwing a new exception would be unsafe!
// (can happen during 'stack unwinding', if we have 'KALDI_ERR << msg'
// in a destructor).
fprintf(stderr, "%s", what_arg.c_str());
// in a destructor of some local object).
abort();
}
} else if (envelope.severity == LogMessageEnvelope::AssertFailed) {
Expand Down Expand Up @@ -243,5 +244,4 @@ LogHandler SetLogHandler(LogHandler new_handler) {
return old_handler;
}


} // end namespace kaldi
23 changes: 7 additions & 16 deletions src/base/kaldi-error.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@
// we can tell the compiler that the function must-not produce
// exceptions (true), or may produce exceptions (false):
#if _MSC_VER >= 1900 || (!defined(_MSC_VER) && __cplusplus >= 201103L)
#define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
#define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
#elif defined(__GXX_EXPERIMENTAL_CXX0X__) && \
(__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
#define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
(__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
#define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
#else
#define KALDI_NOEXCEPT(Predicate)
#define KALDI_NOEXCEPT(Predicate)
#endif

#ifdef _MSC_VER
#define __func__ __FUNCTION__
#define __func__ __FUNCTION__
#endif

namespace kaldi {
Expand Down Expand Up @@ -105,7 +105,7 @@ class MessageLogger {
const char *file,
int32 line);

/// Destructor, calls 'SendToLog' which prints the message,
/// Destructor, calls 'HandleMessage' which prints the message,
/// (since C++11 a 'throwing' destructor must be declared 'noexcept(false)')
~MessageLogger() KALDI_NOEXCEPT(false);

Expand All @@ -115,7 +115,7 @@ class MessageLogger {

private:
/// The logging function,
static void SendToLog(const LogMessageEnvelope &env, const char *msg);
static void HandleMessage(const LogMessageEnvelope &env, const char *msg);

private:
LogMessageEnvelope envelope_;
Expand Down Expand Up @@ -194,15 +194,6 @@ typedef void (*LogHandler)(const LogMessageEnvelope &envelope,
/// stderr. SetLogHandler is obviously not thread safe.
LogHandler SetLogHandler(LogHandler);


/***** DEPRECATED *****/
/// TODO: Deprecated function, get rid of it if possible!
inline bool IsKaldiError(const std::string &str) {
KALDI_WARN << "Deprecated!";
return(!strncmp(str.c_str(), "ERROR ", 6));
}


/// @} end "addtogroup error_group"

} // namespace kaldi
Expand Down
12 changes: 6 additions & 6 deletions src/feat/wave-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class WaveHolder {
t.Write(os); // throws exception on failure.
return true;
} catch (const std::exception &e) {
KALDI_WARN << "Exception caught in WaveHolder object (writing).";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught in WaveHolder object (writing). "
<< e.what();
return false; // write failure.
}
}
Expand All @@ -162,8 +162,8 @@ class WaveHolder {
t_.Read(is); // throws exception on failure.
return true;
} catch (const std::exception &e) {
KALDI_WARN << "Exception caught in WaveHolder object (reading).";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught in WaveHolder object (reading). "
<< e.what();
return false; // write failure.
}
}
Expand Down Expand Up @@ -213,8 +213,8 @@ class WaveInfoHolder {
t_.Read(is, WaveData::kLeaveDataUndefined); // throws exception on failure.
return true;
} catch (const std::exception &e) {
KALDI_WARN << "Exception caught in WaveHolder object (reading).";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught in WaveHolder object (reading). "
<< e.what();
return false; // write failure.
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/hmm/posterior.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ bool PosteriorHolder::Write(std::ostream &os, bool binary, const T &t) {
WritePosterior(os, binary, t);
return true;
} catch(const std::exception &e) {
KALDI_WARN << "Exception caught writing table of posteriors";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught writing table of posteriors. " << e.what();
return false; // Write failure.
}
}
Expand All @@ -147,8 +146,7 @@ bool PosteriorHolder::Read(std::istream &is) {
ReadPosterior(is, is_binary, &t_);
return true;
} catch (std::exception &e) {
KALDI_WARN << "Exception caught reading table of posteriors";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught reading table of posteriors. " << e.what();
t_.clear();
return false;
}
Expand All @@ -174,8 +172,7 @@ bool GaussPostHolder::Write(std::ostream &os, bool binary, const T &t) {
if(!binary) os << '\n';
return os.good();
} catch (const std::exception &e) {
KALDI_WARN << "Exception caught writing table of posteriors";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught writing table of posteriors. " << e.what();
return false; // Write failure.
}
}
Expand Down Expand Up @@ -210,8 +207,7 @@ bool GaussPostHolder::Read(std::istream &is) {
}
return true;
} catch (std::exception &e) {
KALDI_WARN << "Exception caught reading table of posteriors";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught reading table of posteriors. " << e.what();
t_.clear();
return false;
}
Expand Down
31 changes: 11 additions & 20 deletions src/util/kaldi-holder-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ template<class KaldiType> class KaldiObjectHolder {
t.Write(os, binary);
return os.good();
} catch(const std::exception &e) {
KALDI_WARN << "Exception caught writing Table object: " << e.what();
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught writing Table object. " << e.what();
return false; // Write failure.
}
}
Expand All @@ -80,8 +79,7 @@ template<class KaldiType> class KaldiObjectHolder {
t_->Read(is, is_binary);
return true;
} catch(const std::exception &e) {
KALDI_WARN << "Exception caught reading Table object ";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught reading Table object. " << e.what();
delete t_;
t_ = NULL;
return false;
Expand Down Expand Up @@ -137,8 +135,7 @@ template<class BasicType> class BasicHolder {
// easier to manipulate.
return os.good();
} catch(const std::exception &e) {
KALDI_WARN << "Exception caught writing Table object: " << e.what();
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught writing Table object. " << e.what();
return false; // Write failure.
}
}
Expand Down Expand Up @@ -186,8 +183,7 @@ template<class BasicType> class BasicHolder {
}
return true;
} catch(const std::exception &e) {
KALDI_WARN << "Exception caught reading Table object";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught reading Table object. " << e.what();
return false;
}
}
Expand Down Expand Up @@ -252,8 +248,8 @@ template<class BasicType> class BasicVectorHolder {
}
return os.good();
} catch(const std::exception &e) {
KALDI_WARN << "Exception caught writing Table object (BasicVector). ";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught writing Table object (BasicVector). "
<< e.what();
return false; // Write failure.
}
}
Expand Down Expand Up @@ -290,8 +286,7 @@ template<class BasicType> class BasicVectorHolder {
return true;
} catch(const std::exception &e) {
KALDI_WARN << "BasicVectorHolder::Read, could not interpret line: "
<< line;
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
<< "'" << line << "'" << "\n" << e.what();
return false;
}
} else { // binary mode.
Expand Down Expand Up @@ -390,8 +385,7 @@ template<class BasicType> class BasicVectorVectorHolder {
}
return os.good();
} catch(const std::exception &e) {
KALDI_WARN << "Exception caught writing Table object. ";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught writing Table object. " << e.what();
return false; // Write failure.
}
}
Expand Down Expand Up @@ -436,8 +430,7 @@ template<class BasicType> class BasicVectorVectorHolder {
}
}
} catch(const std::exception &e) {
KALDI_WARN << "BasicVectorVectorHolder::Read, read error";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "BasicVectorVectorHolder::Read, read error. " << e.what();
return false;
}
} else { // binary mode.
Expand Down Expand Up @@ -532,8 +525,7 @@ template<class BasicType> class BasicPairVectorHolder {
}
return os.good();
} catch(const std::exception &e) {
KALDI_WARN << "Exception caught writing Table object. ";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "Exception caught writing Table object. " << e.what();
return false; // Write failure.
}
}
Expand Down Expand Up @@ -589,8 +581,7 @@ template<class BasicType> class BasicPairVectorHolder {
}
}
} catch(const std::exception &e) {
KALDI_WARN << "BasicPairVectorHolder::Read, read error";
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
KALDI_WARN << "BasicPairVectorHolder::Read, read error. " << e.what();
return false;
}
} else { // binary mode.
Expand Down

0 comments on commit a4fff0d

Please sign in to comment.