1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
//
// C++ Interface: %{MODULE}
//
// Description:
//
//
// Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
//
// Copyright: See COPYING file that comes with this distribution
//
//
/** @file Error.h
* This header defines several types and functions relating to the
* handling of exceptions in STK.
*/
#ifndef TNET_Error_h
#define TNET_Error_h
#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <cstdlib>
#include <execinfo.h>
// THESE MACROS TERRIBLY CLASH WITH STK!!!!
// WE MUST USE SAME MACROS!
//
//#define Error(msg) _Error_(__func__, __FILE__, __LINE__, msg)
//#define Warning(msg) _Warning_(__func__, __FILE__, __LINE__, msg)
//#define TraceLog(msg) _TraceLog_(__func__, __FILE__, __LINE__, msg)
//
#ifndef Error
#define Error(...) _Error_(__func__, __FILE__, __LINE__, __VA_ARGS__)
#endif
#ifndef Warning
#define Warning(...) _Warning_(__func__, __FILE__, __LINE__, __VA_ARGS__)
#endif
#ifndef TraceLog
#define TraceLog(...) _TraceLog_(__func__, __FILE__, __LINE__, __VA_ARGS__)
#endif
namespace TNet {
/** MyException
* Custom exception class, gets the stacktrace
*/
class MyException
: public std::runtime_error
{
public:
explicit MyException(const std::string& what_arg) throw();
virtual ~MyException() throw();
const char* what() const throw()
{ return mWhat.c_str(); }
private:
std::string mWhat;
};
/**
* MyException:: implemenatation
*/
inline
MyException::
MyException(const std::string& what_arg) throw()
: std::runtime_error(what_arg)
{
mWhat = what_arg;
mWhat += "\nTHE STACKTRACE INSIDE MyException OBJECT IS:\n";
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
//<< 0th string is the MyException ctor, so ignore and start by 1
for (i = 1; i < size; i++) {
mWhat += strings[i];
mWhat += "\n";
}
free (strings);
}
inline
MyException::
~MyException() throw()
{ }
/**
* @brief Error throwing function (with backtrace)
*/
inline void
_Error_(const char *func, const char *file, int line, const std::string &msg)
{
std::stringstream ss;
ss << "ERROR (" << func << ':' << file << ':' << line << ") " << msg;
throw MyException(ss.str());
}
/**
* @brief Warning handling function
*/
inline void
_Warning_(const char *func, const char *file, int line, const std::string &msg)
{
std::cout << "WARNING (" << func << ':' << file << ':' << line << ") " << msg << std::endl;
}
inline void
_TraceLog_(const char *func, const char *file, int line, const std::string &msg)
{
std::cout << "INFO (" << func << ':' << file << ':' << line << ") " << msg << std::endl;
}
/**
* New kaldi error handling:
*
* class KaldiErrorMessage is invoked from the KALDI_ERROR macro.
* The destructor throws an exception.
*/
class KaldiErrorMessage {
public:
KaldiErrorMessage(const char *func, const char *file, int line) {
this->stream() << "ERROR ("
<< func << "():"
<< file << ':' << line << ") ";
}
inline std::ostream &stream() { return ss; }
~KaldiErrorMessage() { throw MyException(ss.str()); }
private:
std::ostringstream ss;
};
#define KALDI_ERR TNet::KaldiErrorMessage(__func__, __FILE__, __LINE__).stream()
} // namespace TNet
//#define TNET_Error_h
#endif
|