All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
kaldi-error.h
Go to the documentation of this file.
1 // base/kaldi-error.h
2 
3 // Copyright 2016 Brno University of Technology (author: Karel Vesely)
4 // Copyright 2009-2011 Microsoft Corporation; Ondrej Glembek; Lukas Burget;
5 // Saarland University
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #ifndef KALDI_BASE_KALDI_ERROR_H_
23 #define KALDI_BASE_KALDI_ERROR_H_ 1
24 
25 #include <cstdio>
26 #include <cstring>
27 #include <sstream>
28 #include <stdexcept>
29 #include <string>
30 #include <vector>
31 
32 #include "base/kaldi-types.h"
33 #include "base/kaldi-utils.h"
34 /* Important that this file does not depend on any other kaldi headers. */
35 
36 // By adding 'KALDI_NOEXCEPT(bool)' immediately after function declaration,
37 // we can tell the compiler that the function must-not produce
38 // exceptions (true), or may produce exceptions (false):
39 #if _MSC_VER >= 1900 || (!defined(_MSC_VER) && __cplusplus >= 201103L)
40 #define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
41 #elif defined(__GXX_EXPERIMENTAL_CXX0X__) && \
42  (__GNUC__ >= 4 && __GNUC_MINOR__ >= 6)
43 #define KALDI_NOEXCEPT(Predicate) noexcept((Predicate))
44 #else
45 #define KALDI_NOEXCEPT(Predicate)
46 #endif
47 
48 #ifdef _MSC_VER
49 #define __func__ __FUNCTION__
50 #endif
51 
52 namespace kaldi {
53 
56 
57 /***** VERBOSITY LEVEL *****/
58 
60 extern int32 g_kaldi_verbose_level;
61 
67 extern const char *g_program_name;
68 
69 inline int32 GetVerboseLevel() { return g_kaldi_verbose_level; }
70 
73 inline void SetVerboseLevel(int32 i) { g_kaldi_verbose_level = i; }
74 
75 
76 /***** KALDI LOGGING *****/
77 
80  enum Severity {
82  kError = -2,
83  kWarning = -1,
84  kInfo = 0,
85  };
86  // An 'enum Severity' value, or a positive number indicating verbosity level.
87  int severity;
88  const char *func;
89  const char *file;
90  int32 line;
91 };
92 
93 // Class MessageLogger is invoked from the KALDI_ASSERT, KALDI_ERR, KALDI_WARN and
94 // KALDI_LOG macros. It formats the message, then either prints it to stderr or
95 // passes to the log custom handler if provided, then, in case of the error,
96 // throws an std::runtime_exception, in case of failed KALDI_ASSERT calls abort().
97 //
98 // Note: we avoid using std::cerr for thread safety issues.
99 // fprintf(stderr,...) is guaranteed thread-safe, and outputs
100 // its formatted string atomically.
102 public:
105  const char *func,
106  const char *file,
107  int32 line);
108 
112 
115  inline std::ostream &stream() { return ss_; }
116 
117 private:
119  static void HandleMessage(const LogMessageEnvelope &env, const char *msg);
120 
121 private:
123  std::ostringstream ss_;
124 };
125 
126 // The definition of the logging macros,
127 #define KALDI_ERR \
128  ::kaldi::MessageLogger(::kaldi::LogMessageEnvelope::kError, \
129  __func__, __FILE__, __LINE__).stream()
130 #define KALDI_WARN \
131  ::kaldi::MessageLogger(::kaldi::LogMessageEnvelope::kWarning, \
132  __func__, __FILE__, __LINE__).stream()
133 #define KALDI_LOG \
134  ::kaldi::MessageLogger(::kaldi::LogMessageEnvelope::kInfo, \
135  __func__, __FILE__, __LINE__).stream()
136 #define KALDI_VLOG(v) if ((v) <= ::kaldi::g_kaldi_verbose_level) \
137  ::kaldi::MessageLogger((::kaldi::LogMessageEnvelope::Severity)(v), \
138  __func__, __FILE__, __LINE__).stream()
139 
140 
141 /***** KALDI ASSERTS *****/
142 
143 void KaldiAssertFailure_(const char *func, const char *file,
144  int32 line, const char *cond_str);
145 
146 // Note on KALDI_ASSERT and KALDI_PARANOID_ASSERT
147 // The original (simple) version of the code was this
148 //
149 // #define KALDI_ASSERT(cond) if (!(cond))
150 // kaldi::KaldiAssertFailure_(__func__, __FILE__, __LINE__, #cond);
151 //
152 // That worked well, but we were concerned that it
153 // could potentially cause a performance issue due to failed branch
154 // prediction (best practice is to have the if branch be the commonly
155 // taken one).
156 // Therefore, we decided to move the call into the else{} branch.
157 // A single block {} around if /else does not work, because it causes
158 // syntax error (unmatched else block) in the following code:
159 //
160 // if (condition)
161 // KALDI_ASSERT(condition2);
162 // else
163 // SomethingElse();
164 //
165 // do {} while(0) -- note there is no semicolon at the end! --- works nicely
166 // and compilers will be able to optimize the loop away (as the condition
167 // is always false).
168 #ifndef NDEBUG
169 #define KALDI_ASSERT(cond) do { if (cond) (void)0; else \
170  ::kaldi::KaldiAssertFailure_(__func__, __FILE__, __LINE__, #cond); } while(0)
171 #else
172 #define KALDI_ASSERT(cond) (void)0
173 #endif
174 // Also see KALDI_COMPILE_TIME_ASSERT, defined in base/kaldi-utils.h,
175 // and KALDI_ASSERT_IS_INTEGER_TYPE and KALDI_ASSERT_IS_FLOATING_TYPE,
176 // also defined there.
177 // some more expensive asserts only checked if this defined
178 #ifdef KALDI_PARANOID
179 #define KALDI_PARANOID_ASSERT(cond) do { if (cond) (void)0; else \
180  ::kaldi::KaldiAssertFailure_(__func__, __FILE__, __LINE__, #cond); } while(0)
181 #else
182 #define KALDI_PARANOID_ASSERT(cond) (void)0
183 #endif
184 
185 
186 /***** THIRD-PARTY LOG-HANDLER *****/
187 
189 typedef void (*LogHandler)(const LogMessageEnvelope &envelope,
190  const char *message);
191 
197 
198 
199 /***** WRITING 'std::vector<T>' TO LOGPRINT *****/
200 template<typename T>
201 std::ostream& operator<< (std::ostream& os, const std::vector<T>& v) {
202  os << "[ ";
203  typename std::vector<T>::const_iterator it;
204  for (it = v.begin(); it != v.end(); ++it) {
205  os << *it << " ";
206  }
207  os << "]";
208  return os;
209 }
210 
212 
213 } // namespace kaldi
214 
215 #endif // KALDI_BASE_KALDI_ERROR_H_
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void(* LogHandler)(const LogMessageEnvelope &envelope, const char *message)
Type of third-party logging function,.
Definition: kaldi-error.h:189
int32 GetVerboseLevel()
Definition: kaldi-error.h:69
MessageLogger(LogMessageEnvelope::Severity severity, const char *func, const char *file, int32 line)
Constructor stores the info,.
Definition: kaldi-error.cc:140
std::ostringstream ss_
Definition: kaldi-error.h:123
void KaldiAssertFailure_(const char *func, const char *file, int32 line, const char *cond_str)
Definition: kaldi-error.cc:230
LogMessageEnvelope envelope_
Definition: kaldi-error.h:122
#define KALDI_NOEXCEPT(Predicate)
Definition: kaldi-error.h:45
void SetVerboseLevel(int32 i)
This should be rarely used; command-line programs set the verbose level automatically from ParseOptio...
Definition: kaldi-error.h:73
const char * g_program_name
This is set by util/parse-options.
Definition: kaldi-error.cc:41
static void HandleMessage(const LogMessageEnvelope &env, const char *msg)
The logging function,.
Definition: kaldi-error.cc:161
LogHandler SetLogHandler(LogHandler new_handler)
Set logging handler.
Definition: kaldi-error.cc:239
std::ostream & stream()
The hook for the 'insertion operator', e.g.
Definition: kaldi-error.h:115
int32 g_kaldi_verbose_level
This is set by util/parse-options.{h, cc} if you set –verbose=? option.
Definition: kaldi-error.cc:40
~MessageLogger() KALDI_NOEXCEPT(false)
Destructor, calls 'HandleMessage' which prints the message, (since C++11 a 'throwing' destructor must...
Definition: kaldi-error.cc:150
Log message severity and source location info.
Definition: kaldi-error.h:79