This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.cpp
247 lines (187 loc) · 4.26 KB
/
Error.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Error.cpp
*
* Created on: 2011-5-14
* Author: simophin
*/
#include "Error.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sstream>
#include <ostream>
#include <vector>
#include "Log.h"
#ifdef OS_WIN32
#include <windows.h>
#endif
extern std::vector<Error::SystemMap> ERROR_MAPS;
extern syserrno_t SYS_ERR_SUCCESS;
extern syserrno_t SYS_ERR_UNKNOWN;
static bool hasInit = false;
extern void initErrors();
class Error::Impl {
public:
Impl()
{
init();
}
void init()
{
if(!hasInit){
initErrors();
hasInit = true;
}
}
inline String getErrorString() const
{
return mErrorString;
}
inline void setErrorString(const String & str)
{
mErrorString = str;
}
inline Type getErrorType() const
{
return mErrorType;
}
inline syserrno_t getSystemErrno() const
{
return mSystemErrno;
}
inline void setErrorType(Type mErrorType)
{
this->mErrorType = mErrorType;
}
inline void setSystemErrno(syserrno_t mSystemErrno)
{
this->mSystemErrno = mSystemErrno;
}
private:
String mErrorString;
syserrno_t mSystemErrno;
Type mErrorType;
};
static syserrno_t findSystemErrno (Error::Type t) {
for (std::vector<Error::SystemMap>::const_iterator iter = ERROR_MAPS.begin();
iter != ERROR_MAPS.end(); ++ iter) {
if (iter->type == t) return iter->system;
}
return SYS_ERR_UNKNOWN;
}
static Error::Type findErrorType (syserrno_t e) {
for (std::vector<Error::SystemMap>::const_iterator iter = ERROR_MAPS.begin();
iter != ERROR_MAPS.end(); ++ iter) {
if (iter->system == e) return iter->type;
}
return Error::ERR_SYS_UNKNOWN;
}
static const char * findErrorTypeMessage (Error::Type t) {
for (std::vector<Error::SystemMap>::const_iterator iter = ERROR_MAPS.begin();
iter != ERROR_MAPS.end(); ++ iter) {
if (iter->type == t) return iter->message;
}
return "Unknown error";
}
static const char * findSystemErrorMessage (syserrno_t e) {
return findErrorTypeMessage(findErrorType(e));
}
Error::Error(Type t)
:d(new Impl)
{
setErrorType(t);
}
Error::Error(Type t, const String & str)
:d(new Impl)
{
setErrorType(t,str);
}
Error::Error(syserrno_t sys)
:d(new Impl)
{
setSystemError(sys);
}
Error::Error(syserrno_t sys, const String & str)
:d(new Impl)
{
setSystemError(sys,str);
}
Error::~Error () {
}
Error::Type Error::getErrorType() const
{
return d->getErrorType();
}
void Error::setErrorType(Type t)
{
if (!d.unique()) {
d.reset(new Impl(*d));
}
d->setErrorString(findErrorTypeMessage(t));
d->setErrorType(t);
d->setSystemErrno(findSystemErrno(t));
}
void Error::setErrorType(Type t, const String & str)
{
if (!d.unique()) {
d.reset(new Impl(*d));
}
d->setErrorString(str);
d->setErrorType(t);
d->setSystemErrno(findSystemErrno(t));
}
void Error::setSystemError(syserrno_t e)
{
if (!d.unique()) {
d.reset(new Impl(*d));
}
d->setErrorString(findSystemErrorMessage(e));
d->setErrorType(findErrorType(e));
d->setSystemErrno(e);
}
void Error::setSystemError(syserrno_t e, const String & str)
{
if (!d.unique()) {
d.reset(new Impl(*d));
}
d->setErrorString(str);
d->setErrorType(findErrorType(e));
d->setSystemErrno(e);
}
String Error::getErrorString() const
{
return d->getErrorString();
}
syserrno_t Error::getSystemError() const
{
return d->getSystemErrno();
}
bool Error::isSuccess() const
{
return (d->getErrorType() == ERR_SUCCESS);
}
String Error::toString() const
{
std::stringstream stream;
stream << "$ERRNO:" << d->getErrorType() << ";$ERRSTR:" << d->getErrorString();
return stream.str();
}
Error Error::fromString(const String & str, bool *ok)
{
Type e;
char buf[1024];
sscanf(str.c_str(),"$ERRNO:%d;$ERRSTR:%s", (int *)&e, buf);
buf[sizeof(buf)-1] = '\0';
Error ret;
ret.setErrorType(e,buf);
return ret;
}
void Error::setErrorString(const String & str)
{
setErrorType(ERR_UNKNOWN,str);
}
std::ostream & operator <<(std::ostream & os, const Error & obj)
{
os << obj.getErrorString();
return os;
}