-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirserror.cpp
More file actions
458 lines (422 loc) · 13 KB
/
Copy pathirserror.cpp
File metadata and controls
458 lines (422 loc) · 13 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! \file
//! \ingroup error_processing_group
//! \brief Îáðàáîòêà îøèáîê
//!
//! Äàòà: 20.05.2010\n
//! Ðàííÿÿ äàòà: 4.08.2009
// Íîìåð ôàéëà
#define IRSERRORCPP_IDX 5
#include <irspch.h>
#ifdef __BORLANDC__
#pragma hdrstop
#endif // __BORLANDC__
#include <irserror.h>
#include <irsfinal.h>
namespace irs {
// Ïðèåìî-ïåðåäàò÷èê îøèáîê (Ðåàëèçàöèÿ)
class error_trans_t: public error_trans_base_t
{
public:
error_trans_t();
virtual ~error_trans_t();
// Ïåðåäàòü îøèáêó (òèï ap_spec_data îïðåäåëÿåòñÿ a_error_code)
virtual void throw_error(
error_code_t a_error_code = ec_standard,
cstr_type ap_file_name = 0,
int a_line_number = 0,
const void *ap_spec_data = 0
);
// Ñ÷èòàòü ïîñëåäíèé êîä îøèáêè
virtual error_code_t error_code();
// Ñ÷èòàòü èìÿ ôàéëà â êîòîðîì ïðîèçîøëà ïîñëåäíÿÿ îøèáêà
virtual cstr_type file_name();
// Ñ÷èòàòü íîìåð ñòðîêè â ôàéëå â êîòîðîé ïðîèçîøëà îøèáêà
virtual int line_number();
// Ñ÷èòàòü ñïåöèôè÷åñêèå äàííûå ñîîòâåòñòâóþùèå êîäó îøèáêè
virtual const void *spec_data();
// Äîáàâèòü îáðàáîò÷èê îøèáêè
virtual void add_handler(mxfact_event_t *ap_handler);
private:
error_code_t m_error_code;
cstr_type mp_file_name;
int m_line_number;
const void *mp_spec_data;
mxfact_event_t *mp_handler;
};
} //namespace irs
// Ïðèåìî-ïåðåäàò÷èê îøèáîê (Ðåàëèçàöèÿ)
irs::error_trans_t::error_trans_t():
m_error_code(ec_standard),
mp_file_name(0),
m_line_number(0),
mp_spec_data(0),
mp_handler(0)
{
}
irs::error_trans_t::~error_trans_t()
{
}
// Ïåðåäàòü îøèáêó (òèï ap_spec_data îïðåäåëÿåòñÿ a_error_code)
void irs::error_trans_t::throw_error(
error_code_t a_error_code,
cstr_type ap_file_name,
int a_line_number,
const void *ap_spec_data
)
{
m_error_code = a_error_code;
mp_file_name = ap_file_name;
m_line_number = a_line_number;
mp_spec_data = ap_spec_data;
if (mp_handler) mp_handler->exec();
}
// Ñ÷èòàòü ïîñëåäíèé êîä îøèáêè
irs::error_code_t irs::error_trans_t::error_code()
{
return m_error_code;
}
// Ñ÷èòàòü èìÿ ôàéëà â êîòîðîì ïðîèçîøëà ïîñëåäíÿÿ îøèáêà
irs::error_trans_t::cstr_type irs::error_trans_t::file_name()
{
return mp_file_name;
}
// Ñ÷èòàòü íîìåð ñòðîêè â ôàéëå â êîòîðîé ïðîèçîøëà îøèáêà
int irs::error_trans_t::line_number()
{
return m_line_number;
}
// Ñ÷èòàòü ñïåöèôè÷åñêèå äàííûå ñîîòâåòñòâóþùèå êîäó îøèáêè
const void *irs::error_trans_t::spec_data()
{
return mp_spec_data;
}
// Äîáàâèòü îáðàáîò÷èê îøèáêè
void irs::error_trans_t::add_handler(mxfact_event_t *ap_handler)
{
ap_handler->connect(mp_handler);
}
// Ïðèåìî-ïåðåäàò÷èê îøèáîê (Ñ÷èòûâàíèå óêàçàòåëÿ íà ýêçåìïëÿð)
irs::error_trans_base_t *irs::error_trans()
{
static error_trans_t error_trans_i;
return &error_trans_i;
}
// Ñïåöèôè÷åñêèå äàííûå äëÿ óòâåðäåíèé â error_trans_base_t
// (Ñ÷èòûâàíèå óêàçàòåëÿ íà ýêçåìïëÿð)
const void* irs::spec_assert(error_trans_base_t::cstr_type assert_str,
error_trans_base_t::cstr_type message)
{
static et_spec_assert_t spec_assert_i;
spec_assert_i.assert_str = assert_str;
spec_assert_i.message = message;
return static_cast<const void*>(&spec_assert_i);
}
// Âûâîä èíôîðìàöèè îá îøèáêå äëÿ îáðàáîò÷èêîâ îøèáîê
irs::error_out_t::error_out_t(
error_trans_base_t* ap_error_trans,
irs_size_t a_out_func_list_size
):
mp_error_trans(ap_error_trans),
m_out_func_ptr_list(a_out_func_list_size),
m_out_unknown_error_obj(this, &irs::error_out_t::out_unknown_error),
#ifdef IRS_ERROR_OUT_STATIC_EVENT
m_out_standart_error_obj(this, &irs::error_out_t::out_standart_error),
m_out_assert_error_obj(this, &irs::error_out_t::out_assert_error),
m_out_fatal_error_obj(this, &irs::error_out_t::out_fatal_error),
m_out_new_assert_error_obj(this, &irs::error_out_t::out_new_assert_error),
#else //IRS_ERROR_OUT_STATIC_EVENT
m_out_func_obj_list(a_out_func_list_size, m_out_unknown_error_obj),
#endif //IRS_ERROR_OUT_STATIC_EVENT
mp_out(IRS_NULL)
{
#ifdef IRS_ERROR_OUT_STATIC_EVENT
m_out_func_ptr_list[ec_standard] = &m_out_standart_error_obj;
m_out_func_ptr_list[ec_assert] = &m_out_assert_error_obj;
m_out_func_ptr_list[ec_fatal_error] = &m_out_fatal_error_obj;
m_out_func_ptr_list[ec_new_assert] = &m_out_new_assert_error_obj;
#else //IRS_ERROR_OUT_STATIC_EVENT
m_out_func_obj_list[ec_standard] =
out_func_obj_type(this, &irs::error_out_t::out_standart_error);
m_out_func_obj_list[ec_assert] =
out_func_obj_type(this, &irs::error_out_t::out_assert_error);
m_out_func_obj_list[ec_fatal_error] =
out_func_obj_type(this, &irs::error_out_t::out_fatal_error);
m_out_func_obj_list[ec_new_assert] =
out_func_obj_type(this, &irs::error_out_t::out_new_assert_error);
::transform(m_out_func_obj_list.begin(), m_out_func_obj_list.end(),
m_out_func_ptr_list.begin(), out_func_obj_extract_ptr);
#endif //IRS_ERROR_OUT_STATIC_EVENT
}
void irs::error_out_t::out_general_info(ostream &a_out,
error_trans_base_t::cstr_type ap_error_type)
{
a_out << endl << irsu(a_out, "IRS ERROR") << endl;
a_out << irsu(a_out, "Error Code: ");
a_out << static_cast<int>(mp_error_trans->error_code()) << endl;
a_out << irsu(a_out, "Error Type: ") << ap_error_type << endl;
irs::error_trans_base_t::cstr_type file_name = mp_error_trans->file_name();
if (file_name) {
a_out << irsu(a_out, "File: ");
a_out << mp_error_trans->file_name() << endl;
}
a_out << irsu(a_out, "Line number: ");
a_out << mp_error_trans->line_number() << endl;
}
void irs::error_out_t::out_info(ostream &a_out)
{
if (mp_error_trans) {
irs::error_code_t error_code = mp_error_trans->error_code();
if (is_valid_error_code(error_code)) {
mp_out = &a_out;
if (mp_out) {
m_out_func_ptr_list[error_code]->exec();
} else {
IRS_LIB_DBG_RAW_MSG(irsm("\nÎøèáêà â irs::error_out_t::out_info!\n"));
IRS_LIB_DBG_RAW_MSG(irsm("mp_out íå äîëæåí áûòü "));
IRS_LIB_DBG_RAW_MSG(irsm("ðàâåí íóëþ\n") << endl);
}
}
}
}
void irs::error_out_t::insert_out_func(error_code_t a_error_code,
event_t* ap_out_func)
{
if (!is_valid_error_code(a_error_code)) {
m_out_func_ptr_list.resize(a_error_code + 1);
} else {
// Åñëè êîä îøèáêè ïðàâèëüíûé (ò. å. ïàìÿòü ïîä íåãî âûäåëåíà),
// òî âûäåëåíèå ïàìÿòè íå òðåáóåòñÿ
}
m_out_func_ptr_list[a_error_code] = ap_out_func;
}
void irs::error_out_t::erase_out_func(error_code_t a_error_code)
{
if (is_valid_error_code(a_error_code)) {
m_out_func_ptr_list[a_error_code] = &m_out_unknown_error_obj;
}
}
#ifndef IRS_ERROR_OUT_STATIC_EVENT
irs::event_t* irs::error_out_t::out_func_obj_extract_ptr(
out_func_obj_type& a_out_func_obj)
{
return &a_out_func_obj;
}
#endif //IRS_ERROR_OUT_STATIC_EVENT
void irs::error_out_t::out_standart_error()
{
IRS_SPEC_CSTR_DECLARE(error_name, "Standart Error");
out_general_info(*mp_out, error_name);
irs::error_trans_base_t::cstr_type msg =
reinterpret_cast<irs::error_trans_base_t::cstr_type>(
mp_error_trans->spec_data());
*mp_out << irsu(*mp_out, "Message: ") << msg << endl;
}
void irs::error_out_t::out_assert_error()
{
IRS_SPEC_CSTR_DECLARE(error_name, "Assert");
out_general_info(*mp_out, error_name);
const irs::et_spec_assert_t *spec_data =
static_cast<const irs::et_spec_assert_t *>(
mp_error_trans->spec_data()
);
if (spec_data) {
if (spec_data->assert_str) {
*mp_out << irsu(*mp_out, "Assert: ") << spec_data->assert_str;
*mp_out << endl;
}
if (spec_data->message) {
*mp_out << irsu(*mp_out, "Message: ") << spec_data->message;
*mp_out << endl;
}
}
}
void irs::error_out_t::out_fatal_error()
{
IRS_SPEC_CSTR_DECLARE(error_name, "Fatal Error");
out_general_info(*mp_out, error_name);
irs::error_trans_base_t::cstr_type spec_data =
reinterpret_cast<irs::error_trans_base_t::cstr_type>(
mp_error_trans->spec_data());
if (spec_data) {
*mp_out << irsu(*mp_out, "Message: ") << spec_data;
*mp_out << endl;
}
}
void irs::error_out_t::out_new_assert_error()
{
IRS_SPEC_CSTR_DECLARE(error_name, "Assert for new");
out_general_info(*mp_out, error_name);
const int& file_idx = *static_cast<const int*>(
mp_error_trans->spec_data());
*mp_out << irsu(*mp_out, "File Index: ") << file_idx << endl;
*mp_out << irsu(*mp_out, "Allocation Error (operator new)") << endl;
}
void irs::error_out_t::out_unknown_error()
{
IRS_SPEC_CSTR_DECLARE(error_name, "Unknown");
out_general_info(*mp_out, error_name);
}
bool irs::error_out_t::is_valid_error_code(error_code_t a_error_code)
{
return static_cast<size_t>(a_error_code) < m_out_func_ptr_list.size();
}
irs::error_trans_base_t* irs::error_out_t::error_trans()
{
return mp_error_trans;
}
#if defined(IRS_WIN32) || defined(IRS_LINUX)
int irs::last_error_code()
{
int error_code = 0;
#if defined(IRS_WIN32)
error_code = GetLastError();
#elif defined(IRS_LINUX)
error_code = errno;
#endif // defined(IRS_LINUX)
return error_code;
}
irs::string irs::last_error_str()
{
return error_str(last_error_code());
}
irs::string irs::error_str(int a_error_code)
{
irs::string message;
#if defined(IRS_WIN32)
LPVOID lpMsgBuf;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
a_error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&lpMsgBuf),
0,
NULL);
message = static_cast<char*>(lpMsgBuf);
LocalFree(lpMsgBuf);
#elif defined(IRS_LINUX)
message = strerror(a_error_code);
#endif // defined(IRS_LINUX)
return message;
}
void irs::send_format_msg(
int a_error_code,
const char* ap_file,
int a_line)
{
#if defined (IRS_WIN32) || defined (IRS_LINUX)
const irs::string message = static_cast<irs::string>(a_error_code)+
": " + error_str(a_error_code);
irs::error_trans()->throw_error(ec_standard, ap_file, a_line,
reinterpret_cast<const void*>(message.c_str()));
#endif // defined (IRS_WIN32) || defined (IRS_LINUX)
}
void irs::send_last_message_err(const char* ap_file, int a_line)
{
int error_code = 0;
#if defined(IRS_WIN32)
error_code = GetLastError();
#elif defined(IRS_LINUX)
error_code = errno;
#endif // defined(IRS_LINUX)
send_format_msg(error_code, ap_file, a_line);
}
#ifdef IRS_WIN32
void irs::send_wsa_last_message_err(const char* ap_file, int a_line)
{
int error_code = WSAGetLastError();
send_format_msg(error_code, ap_file, a_line);
}
#endif // IRS_WIN32
void irs::send_message_err(int a_error_code, const char* ap_file,
int a_line)
{
send_format_msg(a_error_code, ap_file, a_line);
}
#endif // defined(IRS_WIN32) || defined(IRS_LINUX)
#if (__IAR_SYSTEMS_ICC__ >= 9) || defined(IRS_FULL_STDCPPLIB_SUPPORT)
/*template<typename stream_type, typename buffer_type>
stream_type& irs::basic_mlog<stream_type, buffer_type>()
{
static buffer_type buf;
static stream_type mlog_obj(&buf);
return mlog_obj;
}*/
ostream& irs::mlog()
{
return irs::basic_mlog<char>();
}
wostream& irs::wmlog()
{
return irs::basic_mlog<wchar_t>();
}
#else //(__IAR_SYSTEMS_ICC__ >= 9) || defined(IRS_FULL_STDCPPLIB_SUPPORT)
ostream& irs::mlog()
{
static zerobuf buf;
static ostream mlog_obj(&buf);
return mlog_obj;
}
#endif // (__IAR_SYSTEMS_ICC__ >= 9) || defined(IRS_FULL_STDCPPLIB_SUPPORT)
std::ostream& irs::fslog()
{
static irs::zerobuf buf;
static std::ostream fslog_obj(&buf);
return fslog_obj;
}
namespace irs {
class debug_param_implementation_t: public debug_param_t
{
public:
typedef map<string_type, size_t> param_list_type;
//typedef param_list_type::iterator pl_iterator;
typedef param_list_type::const_iterator pl_const_iterator;
debug_param_implementation_t():
m_param_list()
{
}
virtual void set(const string_type& a_name, size_t a_value_num)
{
m_param_list[a_name] = a_value_num;
}
virtual size_t get(const string_type& a_name) const
{
size_t param = 0;
pl_const_iterator it = m_param_list.find(a_name);
if (it != m_param_list.end()) {
param = it->second;
}
return param;
}
virtual bool is_exist(const string_type& a_name) const
{
return m_param_list.find(a_name) != m_param_list.end();
}
private:
param_list_type m_param_list;
};
} //namespace irs
irs::debug_param_t& irs::debug_param()
{
static debug_param_implementation_t debug_param_implementation;
return debug_param_implementation;
}
irs::irs_string_t irs::conv_notprintable_to_hex(const irs_string_t& a_str)
{
irs_string_t result_str = "";
for (size_t i = 0; i < a_str.size(); i++) {
const char space_char = 32;
if (a_str[i] >= space_char) {
result_str += a_str[i];
} else {
ostrstream stream;
stream << "\\x";
stream << hex << setw(2) << setfill('0') << (int)a_str[i];
result_str += stream.str();
stream.rdbuf()->freeze(false);
}
}
return result_str;
}