forked from AustralianSynchrotron/imbl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.h
More file actions
155 lines (126 loc) · 3.06 KB
/
error.h
File metadata and controls
155 lines (126 loc) · 3.06 KB
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
#ifndef _ERROR_H
#define _ERROR_H
#include <QtCore>
#ifdef QT_QTGUI_MODULE_H
#include <QMessageBox>
#endif
#include <iostream>
/// \defgroup Error Error handling.
/// Functions in this group are used for the
/// error parsing, printing, throwing and so on.
///
/// @{
/// Error type.
class Err {
public:
/// Error severity
typedef enum {
INFO, ///< Warning
WARN, ///< Error
ERR ///< Fatal error
} ErrTp ;
private:
ErrTp terr; ///< Error severity
QString modname;
QString message; ///< The message which describes the error
public:
inline Err(ErrTp _terr, const QString & msg, const QObject * _parent=0)
: terr(_terr), message(msg) {
if ( ! _parent || _parent->objectName().isEmpty() )
modname = "no-name";
else
modname = _parent->objectName();
}
inline Err(ErrTp _terr, const QString & msg, const QString & _modname)
: terr(_terr), modname(_modname), message(msg) { }
/// Return error severity
inline ErrTp type() const {
return terr;
}
inline QString form() const {
QString repstr;
switch ( terr ) {
case INFO: repstr += "FYI."; break;
case WARN: repstr += "WARNING!"; break;
case ERR: repstr += "ERROR!"; break;
}
return repstr += " In module \'" + modname + "\'. " + message;
}
inline void report() {
std::cerr << form().toStdString() << std::endl << std::flush;
}
#ifdef QT_QTGUI_MODULE_H
inline void popup() {
switch (terr) {
case INFO:
QMessageBox::information ( 0 , modname, form(),
QMessageBox::Ok ) ;
break;
case WARN:
QMessageBox::warning ( 0, modname, form(),
QMessageBox::Ok) ;
break;
case ERR:
QMessageBox::critical ( 0, modname, form(),
QMessageBox::Abort) ;
break;
}
}
#endif
};
static inline void
throw_error(const QString & msg, const QObject * parent=0) {
Err err(Err::ERR, msg, parent);
err.report();
#ifdef QT_QTGUI_MODULE_H
err.popup();
#endif
throw err;
}
static inline Err
warn(const QString & msg, const QObject * parent=0) {
Err err(Err::WARN, msg, parent);
err.report();
#ifdef QT_QTGUI_MODULE_H
err.popup();
#else
#endif
return err;
}
static inline Err
inform(const QString & msg, const QObject * parent=0) {
Err err(Err::INFO, msg, parent);
err.report();
#ifdef QT_QTGUI_MODULE_H
err.popup();
#endif
return err;
}
static inline void
throw_error(const QString & msg, const QString & modname) {
Err err(Err::ERR, msg, modname);
err.report();
#ifdef QT_QTGUI_MODULE_H
err.popup();
#endif
}
static inline Err
warn(const QString & msg, const QString & modname) {
Err err(Err::WARN, msg, modname);
err.report();
#ifdef QT_QTGUI_MODULE_H
err.popup();
#endif
return err;
}
static inline Err
inform(const QString & msg, const QString & modname) {
Err err(Err::INFO, msg, modname);
err.report();
#ifdef QT_QTGUI_MODULE_H
err.popup();
#endif
return err;
}
/// @}
#endif