Skip to content

Commit a4fff0d

Browse files
committed
base/kaldi_error : integrating comments from Dan.
1 parent 24bef8d commit a4fff0d

File tree

5 files changed

+65
-87
lines changed

5 files changed

+65
-87
lines changed

src/base/kaldi-error.cc

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
// limitations under the License.
2020

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

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

5050

51+
/***** HELPER FUNCTIONS *****/
52+
53+
// Given a filename like "/a/b/c/d/e/f.cc", GetShortFileName
54+
// returns "e/f.cc". Does not currently work if backslash is
55+
// the filename separator.
56+
static const char *GetShortFileName(const char *filename) {
57+
const char *last_slash = strrchr(filename, '/');
58+
if (!last_slash) {
59+
return filename;
60+
} else {
61+
while (last_slash > filename && last_slash[-1] != '/')
62+
last_slash--;
63+
return last_slash;
64+
}
65+
}
66+
67+
5168
/***** STACKTRACE *****/
5269

5370
static std::string Demangle(std::string trace_name) {
@@ -84,8 +101,8 @@ static std::string Demangle(std::string trace_name) {
84101
static std::string KaldiGetStackTrace() {
85102
std::string ans;
86103
#ifdef HAVE_EXECINFO_H
87-
#define KALDI_MAX_TRACE_SIZE 50
88-
#define KALDI_MAX_TRACE_PRINT 20 // must be even.
104+
#define KALDI_MAX_TRACE_SIZE 50
105+
#define KALDI_MAX_TRACE_PRINT 20 // must be even.
89106
// buffer for the trace,
90107
void *trace[KALDI_MAX_TRACE_SIZE];
91108
// get the trace,
@@ -120,21 +137,6 @@ static std::string KaldiGetStackTrace() {
120137

121138
/***** KALDI LOGIGNG *****/
122139

123-
// Given a filename like "/a/b/c/d/e/f.cc", GetShortFileName
124-
// returns "e/f.cc". Does not currently work if backslash is
125-
// the filename separator.
126-
static const char *GetShortFileName(const char *filename) {
127-
const char *last_slash = strrchr(filename, '/');
128-
if (!last_slash) {
129-
return filename;
130-
} else {
131-
while (last_slash > filename && last_slash[-1] != '/')
132-
last_slash--;
133-
return last_slash;
134-
}
135-
}
136-
137-
138140
MessageLogger::MessageLogger(LogMessageEnvelope::Severity severity,
139141
const char *func, const char *file, int32 line) {
140142
// Obviously, we assume the strings survive the destruction of this object.
@@ -152,12 +154,12 @@ MessageLogger::~MessageLogger() KALDI_NOEXCEPT(false) {
152154
str.resize(str.length() - 1);
153155

154156
// print the mesage (or send to logging handler),
155-
MessageLogger::SendToLog(envelope_, str.c_str());
157+
MessageLogger::HandleMessage(envelope_, str.c_str());
156158
}
157159

158160

159-
void MessageLogger::SendToLog(const LogMessageEnvelope &envelope,
160-
const char *message) {
161+
void MessageLogger::HandleMessage(const LogMessageEnvelope &envelope,
162+
const char *message) {
161163
// Send to a logging handler if provided.
162164
if (g_log_handler != NULL) {
163165
g_log_handler(envelope, message);
@@ -198,20 +200,19 @@ void MessageLogger::SendToLog(const LogMessageEnvelope &envelope,
198200
// print to stderr,
199201
fprintf(stderr, "%s %s\n", header.str().c_str(), message);
200202
} else if (envelope.severity == LogMessageEnvelope::Error) {
201-
// ERROR:
202-
// throw exception with 'what()' message (contains stack-trace),
203-
std::string what_arg = header.str() + " " +
204-
message + "\n\n" +
205-
KaldiGetStackTrace() + "\n";
203+
// ERROR:
204+
// print to stderr (with stack-trace),
205+
fprintf(stderr, "%s %s\n\n%s\n", header.str().c_str(), message,
206+
KaldiGetStackTrace().c_str());
206207
if (!std::uncaught_exception()) {
207-
throw std::runtime_error(what_arg);
208+
// throw exception with empty message,
209+
throw std::runtime_error("");
208210
} else {
209211
// If we got here, this thread has already thrown exception,
210212
// and this exception has not yet arrived to its 'catch' clause...
211213
// Throwing a new exception would be unsafe!
212214
// (can happen during 'stack unwinding', if we have 'KALDI_ERR << msg'
213-
// in a destructor).
214-
fprintf(stderr, "%s", what_arg.c_str());
215+
// in a destructor of some local object).
215216
abort();
216217
}
217218
} else if (envelope.severity == LogMessageEnvelope::AssertFailed) {
@@ -243,5 +244,4 @@ LogHandler SetLogHandler(LogHandler new_handler) {
243244
return old_handler;
244245
}
245246

246-
247247
} // end namespace kaldi

src/base/kaldi-error.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
// we can tell the compiler that the function must-not produce
3737
// exceptions (true), or may produce exceptions (false):
3838
#if _MSC_VER >= 1900 || (!defined(_MSC_VER) && __cplusplus >= 201103L)
39-
#define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
39+
#define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
4040
#elif defined(__GXX_EXPERIMENTAL_CXX0X__) && \
41-
(__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
42-
#define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
41+
(__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
42+
#define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
4343
#else
44-
#define KALDI_NOEXCEPT(Predicate)
44+
#define KALDI_NOEXCEPT(Predicate)
4545
#endif
4646

4747
#ifdef _MSC_VER
48-
#define __func__ __FUNCTION__
48+
#define __func__ __FUNCTION__
4949
#endif
5050

5151
namespace kaldi {
@@ -105,7 +105,7 @@ class MessageLogger {
105105
const char *file,
106106
int32 line);
107107

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

@@ -115,7 +115,7 @@ class MessageLogger {
115115

116116
private:
117117
/// The logging function,
118-
static void SendToLog(const LogMessageEnvelope &env, const char *msg);
118+
static void HandleMessage(const LogMessageEnvelope &env, const char *msg);
119119

120120
private:
121121
LogMessageEnvelope envelope_;
@@ -194,15 +194,6 @@ typedef void (*LogHandler)(const LogMessageEnvelope &envelope,
194194
/// stderr. SetLogHandler is obviously not thread safe.
195195
LogHandler SetLogHandler(LogHandler);
196196

197-
198-
/***** DEPRECATED *****/
199-
/// TODO: Deprecated function, get rid of it if possible!
200-
inline bool IsKaldiError(const std::string &str) {
201-
KALDI_WARN << "Deprecated!";
202-
return(!strncmp(str.c_str(), "ERROR ", 6));
203-
}
204-
205-
206197
/// @} end "addtogroup error_group"
207198

208199
} // namespace kaldi

src/feat/wave-reader.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class WaveHolder {
135135
t.Write(os); // throws exception on failure.
136136
return true;
137137
} catch (const std::exception &e) {
138-
KALDI_WARN << "Exception caught in WaveHolder object (writing).";
139-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
138+
KALDI_WARN << "Exception caught in WaveHolder object (writing). "
139+
<< e.what();
140140
return false; // write failure.
141141
}
142142
}
@@ -162,8 +162,8 @@ class WaveHolder {
162162
t_.Read(is); // throws exception on failure.
163163
return true;
164164
} catch (const std::exception &e) {
165-
KALDI_WARN << "Exception caught in WaveHolder object (reading).";
166-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
165+
KALDI_WARN << "Exception caught in WaveHolder object (reading). "
166+
<< e.what();
167167
return false; // write failure.
168168
}
169169
}
@@ -213,8 +213,8 @@ class WaveInfoHolder {
213213
t_.Read(is, WaveData::kLeaveDataUndefined); // throws exception on failure.
214214
return true;
215215
} catch (const std::exception &e) {
216-
KALDI_WARN << "Exception caught in WaveHolder object (reading).";
217-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
216+
KALDI_WARN << "Exception caught in WaveHolder object (reading). "
217+
<< e.what();
218218
return false; // write failure.
219219
}
220220
}

src/hmm/posterior.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ bool PosteriorHolder::Write(std::ostream &os, bool binary, const T &t) {
129129
WritePosterior(os, binary, t);
130130
return true;
131131
} catch(const std::exception &e) {
132-
KALDI_WARN << "Exception caught writing table of posteriors";
133-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
132+
KALDI_WARN << "Exception caught writing table of posteriors. " << e.what();
134133
return false; // Write failure.
135134
}
136135
}
@@ -147,8 +146,7 @@ bool PosteriorHolder::Read(std::istream &is) {
147146
ReadPosterior(is, is_binary, &t_);
148147
return true;
149148
} catch (std::exception &e) {
150-
KALDI_WARN << "Exception caught reading table of posteriors";
151-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
149+
KALDI_WARN << "Exception caught reading table of posteriors. " << e.what();
152150
t_.clear();
153151
return false;
154152
}
@@ -174,8 +172,7 @@ bool GaussPostHolder::Write(std::ostream &os, bool binary, const T &t) {
174172
if(!binary) os << '\n';
175173
return os.good();
176174
} catch (const std::exception &e) {
177-
KALDI_WARN << "Exception caught writing table of posteriors";
178-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
175+
KALDI_WARN << "Exception caught writing table of posteriors. " << e.what();
179176
return false; // Write failure.
180177
}
181178
}
@@ -210,8 +207,7 @@ bool GaussPostHolder::Read(std::istream &is) {
210207
}
211208
return true;
212209
} catch (std::exception &e) {
213-
KALDI_WARN << "Exception caught reading table of posteriors";
214-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
210+
KALDI_WARN << "Exception caught reading table of posteriors. " << e.what();
215211
t_.clear();
216212
return false;
217213
}

src/util/kaldi-holder-inl.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ template<class KaldiType> class KaldiObjectHolder {
5252
t.Write(os, binary);
5353
return os.good();
5454
} catch(const std::exception &e) {
55-
KALDI_WARN << "Exception caught writing Table object: " << e.what();
56-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
55+
KALDI_WARN << "Exception caught writing Table object. " << e.what();
5756
return false; // Write failure.
5857
}
5958
}
@@ -80,8 +79,7 @@ template<class KaldiType> class KaldiObjectHolder {
8079
t_->Read(is, is_binary);
8180
return true;
8281
} catch(const std::exception &e) {
83-
KALDI_WARN << "Exception caught reading Table object ";
84-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
82+
KALDI_WARN << "Exception caught reading Table object. " << e.what();
8583
delete t_;
8684
t_ = NULL;
8785
return false;
@@ -137,8 +135,7 @@ template<class BasicType> class BasicHolder {
137135
// easier to manipulate.
138136
return os.good();
139137
} catch(const std::exception &e) {
140-
KALDI_WARN << "Exception caught writing Table object: " << e.what();
141-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
138+
KALDI_WARN << "Exception caught writing Table object. " << e.what();
142139
return false; // Write failure.
143140
}
144141
}
@@ -186,8 +183,7 @@ template<class BasicType> class BasicHolder {
186183
}
187184
return true;
188185
} catch(const std::exception &e) {
189-
KALDI_WARN << "Exception caught reading Table object";
190-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
186+
KALDI_WARN << "Exception caught reading Table object. " << e.what();
191187
return false;
192188
}
193189
}
@@ -252,8 +248,8 @@ template<class BasicType> class BasicVectorHolder {
252248
}
253249
return os.good();
254250
} catch(const std::exception &e) {
255-
KALDI_WARN << "Exception caught writing Table object (BasicVector). ";
256-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
251+
KALDI_WARN << "Exception caught writing Table object (BasicVector). "
252+
<< e.what();
257253
return false; // Write failure.
258254
}
259255
}
@@ -290,8 +286,7 @@ template<class BasicType> class BasicVectorHolder {
290286
return true;
291287
} catch(const std::exception &e) {
292288
KALDI_WARN << "BasicVectorHolder::Read, could not interpret line: "
293-
<< line;
294-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
289+
<< "'" << line << "'" << "\n" << e.what();
295290
return false;
296291
}
297292
} else { // binary mode.
@@ -390,8 +385,7 @@ template<class BasicType> class BasicVectorVectorHolder {
390385
}
391386
return os.good();
392387
} catch(const std::exception &e) {
393-
KALDI_WARN << "Exception caught writing Table object. ";
394-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
388+
KALDI_WARN << "Exception caught writing Table object. " << e.what();
395389
return false; // Write failure.
396390
}
397391
}
@@ -436,8 +430,7 @@ template<class BasicType> class BasicVectorVectorHolder {
436430
}
437431
}
438432
} catch(const std::exception &e) {
439-
KALDI_WARN << "BasicVectorVectorHolder::Read, read error";
440-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
433+
KALDI_WARN << "BasicVectorVectorHolder::Read, read error. " << e.what();
441434
return false;
442435
}
443436
} else { // binary mode.
@@ -532,8 +525,7 @@ template<class BasicType> class BasicPairVectorHolder {
532525
}
533526
return os.good();
534527
} catch(const std::exception &e) {
535-
KALDI_WARN << "Exception caught writing Table object. ";
536-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
528+
KALDI_WARN << "Exception caught writing Table object. " << e.what();
537529
return false; // Write failure.
538530
}
539531
}
@@ -589,8 +581,7 @@ template<class BasicType> class BasicPairVectorHolder {
589581
}
590582
}
591583
} catch(const std::exception &e) {
592-
KALDI_WARN << "BasicPairVectorHolder::Read, read error";
593-
if (!IsKaldiError(e.what())) { std::cerr << e.what(); }
584+
KALDI_WARN << "BasicPairVectorHolder::Read, read error. " << e.what();
594585
return false;
595586
}
596587
} else { // binary mode.

0 commit comments

Comments
 (0)