1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
24
* The main exception classes
30
#include <sys/signal.h>
35
#include "CSException.h"
36
#include "CSStrUtil.h"
39
void CSException::setStackTrace(CSThread *self, const char *stack)
41
char buffer[CS_EXC_CONTEXT_SIZE];
43
self->myException.iStackTrace.setLength(0);
45
self->myException.iStackTrace.append(stack);
46
for (int i=self->callTop-1; i>=0; i--) {
47
cs_format_context(CS_EXC_CONTEXT_SIZE, buffer,
48
self->callStack[i].cs_func, self->callStack[i].cs_file, self->callStack[i].cs_line);
49
self->myException.iStackTrace.append(buffer);
50
self->myException.iStackTrace.append('\n');
54
void CSException::setStackTrace(CSThread *self)
56
setStackTrace(self, NULL);
59
const char *CSException::getStackTrace()
61
return iStackTrace.getCString();
64
void CSException::log(CSThread *self)
67
CSL.log(self, CSLog::Error, getContext());
68
CSL.log(self, CSLog::Error, " ");
69
CSL.log(self, CSLog::Error, getMessage());
70
CSL.eol(self, CSLog::Error);
71
CSL.log(self, CSLog::Error, getStackTrace());
75
void CSException::log(CSThread *self, const char *message)
78
CSL.log(self, CSLog::Error, message);
79
CSL.eol(self, CSLog::Error);
80
CSL.log(self, CSLog::Error, getContext());
81
CSL.log(self, CSLog::Error, " ");
82
CSL.log(self, CSLog::Error, getMessage());
83
CSL.eol(self, CSLog::Error);
84
CSL.log(self, CSLog::Error, getStackTrace());
88
void CSException::initException(const char *func, const char *file, int line, int err, const char *message)
90
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
92
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, message);
95
void CSException::initException(CSException &exception)
97
iErrorCode = exception.iErrorCode;
98
strcpy(iContext, exception.iContext);
99
strcpy(iMessage, exception.iMessage);
101
iStackTrace.setLength(0);
102
iStackTrace.append(exception.iStackTrace.getCString());
106
void CSException::initAssertion(const char *func, const char *file, int line, const char *message)
108
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
109
iErrorCode = CS_ERR_ASSERTION;
110
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, message);
113
void CSException::getCoreError(uint32_t size, char *buffer, int err)
115
const char *message = NULL;
118
case CS_ERR_JUMP_OVERFLOW: message = "Jump stack overflow"; break;
119
case CS_ERR_BAD_ADDRESS: message = "Incorrect network address: %"; break;
120
case CS_ERR_UNKNOWN_SERVICE: message = "Unknown network service: %"; break;
121
case CS_ERR_UNKNOWN_HOST: message = "Unknown host: %"; break;
122
case CS_ERR_UNKNOWN_METHOD: message = "Unknown HTTP method: %"; break;
123
case CS_ERR_NO_LISTENER: message = "Listening port has been closed"; break;
124
case CS_ERR_RELEASE_OVERFLOW: message = "Release stack overflow"; break;
125
case CS_ERR_IMPL_MISSING: message = "Function %s not implemented"; break;
126
case CS_ERR_BAD_HEADER_MAGIC: message = "Incorrect file type"; break;
127
case CS_ERR_VERSION_TOO_NEW: message = "Incompatible file version"; break;
130
cs_strcpy(size, buffer, message);
132
cs_strcpy(size, buffer, "Unknown system error ");
133
cs_strcat(size, buffer, err);
137
void CSException::initCoreError(const char *func, const char *file, int line, int err)
139
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
141
getCoreError(CS_EXC_MESSAGE_SIZE, iMessage, err);
144
void CSException::initCoreError(const char *func, const char *file, int line, int err, const char *item)
146
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
148
getCoreError(CS_EXC_MESSAGE_SIZE, iMessage, err);
149
cs_replace_string(CS_EXC_MESSAGE_SIZE, iMessage, '%', item);
152
void CSException::initOSError(const char *func, const char *file, int line, int err)
156
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
160
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, iMessage, CS_EXC_MESSAGE_SIZE, NULL)) {
163
ptr = &iMessage[strlen(iMessage)];
164
while (ptr-1 > err_msg) {
165
if (*(ptr-1) != '\n' && *(ptr-1) != '\r' && *(ptr-1) != '.')
171
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, " (");
172
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, err);
173
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, ")");
180
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, msg);
181
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, " (");
182
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, err);
183
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, ")");
186
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, "Unknown OS error code ");
187
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, err);
191
void CSException::initFileError(const char *func, const char *file, int line, const char *path, int err)
193
initOSError(func, file, line, err);
194
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, ": '");
195
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, path);
196
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, "'");
199
void CSException::initSignal(const char *func, const char *file, int line, int sig)
203
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
205
if (!(str = strsignal(sig))) {
206
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, "Unknown signal ");
207
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, sig);
210
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, str);
211
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, " (");
212
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, sig);
213
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, ")");
217
void CSException::initEOFError(const char *func, const char *file, int line, const char *path)
219
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
220
iErrorCode = CS_ERR_EOF;
221
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, "EOF encountered: '");
222
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, path);
223
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, "'");
226
void CSException::RecordException(const char *func, const char *file, int line, int err, const char *message)
230
if ((self = CSThread::getSelf())) {
231
if (!self->myException.getErrorCode())
232
self->myException.initException(func, file, line, err, message);
236
void CSException::ClearException()
240
if ((self = CSThread::getSelf())) {
241
self->myException.setErrorCode(0);
245
void CSException::throwException(const char *func, const char *file, int line, int err, const char *message, const char *stack)
249
if ((self = CSThread::getSelf())) {
250
self->myException.initException(func, file, line, err, message);
251
self->myException.setStackTrace(self, stack);
252
self->throwException();
257
e.initException(func, file, line, err,message);
258
e.log(NULL, "*** Uncaught error");
262
void CSException::throwException(const char *func, const char *file, int line, int err, const char *message)
264
throwException(func, file, line, err, message, NULL);
267
void CSException::throwAssertion(const char *func, const char *file, int line, const char *message)
271
if ((self = CSThread::getSelf())) {
272
self->myException.initAssertion(func, file, line, message);
273
self->myException.setStackTrace(self);
274
self->logException();
275
self->throwException();
280
e.initAssertion(func, file, line, message);
281
e.log(NULL, "*** Uncaught error");
285
void CSException::throwCoreError(const char *func, const char *file, int line, int err)
289
if ((self = CSThread::getSelf())) {
290
self->myException.initCoreError(func, file, line, err);
291
self->myException.setStackTrace(self);
292
self->throwException();
297
e.initCoreError(func, file, line, err);
298
e.log(NULL, "*** Uncaught error");
302
void CSException::throwCoreError(const char *func, const char *file, int line, int err, const char *item)
306
if ((self = CSThread::getSelf())) {
307
self->myException.initCoreError(func, file, line, err, item);
308
self->myException.setStackTrace(self);
309
self->throwException();
314
e.initCoreError(func, file, line, err, item);
315
e.log(NULL, "*** Uncaught error");
319
void CSException::throwOSError(const char *func, const char *file, int line, int err)
323
if ((self = CSThread::getSelf())) {
324
/* A pending signal has priority over a system error,
325
* In fact, the pending signal may be the reason for
329
self->myException.initOSError(func, file, line, err);
330
self->myException.setStackTrace(self);
331
self->throwException();
336
e.initOSError(func, file, line, err);
337
e.log(NULL, "*** Uncaught error");
341
void CSException::throwFileError(const char *func, const char *file, int line, const char *path, int err)
345
if ((self = CSThread::getSelf())) {
347
self->myException.initFileError(func, file, line, path, err);
348
self->myException.setStackTrace(self);
349
self->throwException();
354
e.initFileError(func, file, line, path, err);
355
e.log(NULL, "*** Uncaught error");
359
void CSException::throwFileError(const char *func, const char *file, int line, CSString *path, int err)
363
if ((self = CSThread::getSelf())) {
365
self->myException.initFileError(func, file, line, path->getCString(), err);
366
self->myException.setStackTrace(self);
367
self->throwException();
372
e.initFileError(func, file, line, path->getCString(), err);
373
e.log(NULL, "*** Uncaught error");
377
void CSException::throwSignal(const char *func, const char *file, int line, int sig)
381
if ((self = CSThread::getSelf())) {
382
self->myException.initSignal(func, file, line, sig);
383
self->myException.setStackTrace(self);
384
self->throwException();
389
e.initSignal(func, file, line, sig);
390
e.log(NULL, "*** Uncaught error");
394
void CSException::throwEOFError(const char *func, const char *file, int line, const char *path)
398
if ((self = CSThread::getSelf())) {
400
self->myException.initEOFError(func, file, line, path);
401
self->myException.setStackTrace(self);
402
self->throwException();
407
e.initEOFError(func, file, line, path);
408
e.log(NULL, "*** Uncaught error");
412
void CSException::throwLastError(const char *func, const char *file, int line)
415
throwOSError(func, file, line, (int) getLastError());
417
throwOSError(func, file, line, (int) errno);
421
void CSException::logOSError(const char *func, const char *file, int line, int err)
425
if ((self = CSThread::getSelf())) {
426
self->myException.initOSError(func, file, line, err);
427
self->myException.setStackTrace(self);
428
self->logException();
433
e.initOSError(func, file, line, err);
438
void CSException::logOSError(CSThread *self, const char *func, const char *file, int line, int err)
440
self->myException.initOSError(func, file, line, err);
441
self->myException.setStackTrace(self);
442
self->logException();
445
void CSException::logException(const char *func, const char *file, int line, int err, const char *message)
449
if ((self = CSThread::getSelf())) {
450
self->myException.initException(func, file, line, err, message);
451
self->logException();
456
e.initException(func, file, line, err,message);