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::initAssertion(const char *func, const char *file, int line, const char *message)
97
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
98
iErrorCode = CS_ERR_ASSERTION;
99
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, message);
102
void CSException::getCoreError(u_int size, char *buffer, int err)
104
const char *message = NULL;
107
case CS_ERR_JUMP_OVERFLOW: message = "Jump stack overflow"; break;
108
case CS_ERR_BAD_ADDRESS: message = "Incorrect network address: %"; break;
109
case CS_ERR_UNKNOWN_SERVICE: message = "Unknown network service: %"; break;
110
case CS_ERR_UNKNOWN_HOST: message = "Unknown host: %"; break;
111
case CS_ERR_UNKNOWN_METHOD: message = "Unknown HTTP method: %"; break;
112
case CS_ERR_NO_LISTENER: message = "Listening port has been closed"; break;
113
case CS_ERR_RELEASE_OVERFLOW: message = "Release stack overflow"; break;
114
case CS_ERR_IMPL_MISSING: message = "Function %s not implemented"; break;
115
case CS_ERR_BAD_HEADER_MAGIC: message = "Incorrect file type"; break;
116
case CS_ERR_VERSION_TOO_NEW: message = "Incompatible file version"; break;
119
cs_strcpy(size, buffer, message);
121
cs_strcpy(size, buffer, "Unknown system error ");
122
cs_strcat(size, buffer, err);
126
void CSException::initCoreError(const char *func, const char *file, int line, int err)
128
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
130
getCoreError(CS_EXC_MESSAGE_SIZE, iMessage, err);
133
void CSException::initCoreError(const char *func, const char *file, int line, int err, const char *item)
135
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
137
getCoreError(CS_EXC_MESSAGE_SIZE, iMessage, err);
138
cs_replace_string(CS_EXC_MESSAGE_SIZE, iMessage, '%', item);
141
void CSException::initOSError(const char *func, const char *file, int line, int err)
145
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
149
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, iMessage, CS_EXC_MESSAGE_SIZE, NULL)) {
152
ptr = &iMessage[strlen(iMessage)];
153
while (ptr-1 > err_msg) {
154
if (*(ptr-1) != '\n' && *(ptr-1) != '\r' && *(ptr-1) != '.')
160
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, " (");
161
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, err);
162
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, ")");
169
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, msg);
170
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, " (");
171
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, err);
172
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, ")");
175
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, "Unknown OS error code ");
176
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, err);
180
void CSException::initFileError(const char *func, const char *file, int line, const char *path, int err)
182
initOSError(func, file, line, err);
183
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, ": '");
184
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, path);
185
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, "'");
188
void CSException::initSignal(const char *func, const char *file, int line, int sig)
192
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
194
if (!(str = strsignal(sig))) {
195
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, "Unknown signal ");
196
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, sig);
199
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, str);
200
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, " (");
201
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, sig);
202
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, ")");
206
void CSException::initEOFError(const char *func, const char *file, int line, const char *path)
208
cs_format_context(CS_EXC_CONTEXT_SIZE, iContext, func, file, line);
209
iErrorCode = CS_ERR_EOF;
210
cs_strcpy(CS_EXC_MESSAGE_SIZE, iMessage, "EOF encountered: '");
211
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, path);
212
cs_strcat(CS_EXC_MESSAGE_SIZE, iMessage, "'");
215
void CSException::RecordException(const char *func, const char *file, int line, int err, const char *message)
219
if ((self = CSThread::getSelf())) {
220
if (!self->myException.getErrorCode())
221
self->myException.initException(func, file, line, err, message);
225
void CSException::ClearException()
229
if ((self = CSThread::getSelf())) {
230
self->myException.setErrorCode(0);
234
void CSException::throwException(const char *func, const char *file, int line, int err, const char *message, const char *stack)
238
if ((self = CSThread::getSelf())) {
239
self->myException.initException(func, file, line, err, message);
240
self->myException.setStackTrace(self, stack);
241
self->throwException();
246
e.initException(func, file, line, err,message);
247
e.log(NULL, "*** Uncaught error");
251
void CSException::throwException(const char *func, const char *file, int line, int err, const char *message)
253
throwException(func, file, line, err, message, NULL);
256
void CSException::throwAssertion(const char *func, const char *file, int line, const char *message)
260
if ((self = CSThread::getSelf())) {
261
self->myException.initAssertion(func, file, line, message);
262
self->myException.setStackTrace(self);
263
self->logException();
264
self->throwException();
269
e.initAssertion(func, file, line, message);
270
e.log(NULL, "*** Uncaught error");
274
void CSException::throwCoreError(const char *func, const char *file, int line, int err)
278
if ((self = CSThread::getSelf())) {
279
self->myException.initCoreError(func, file, line, err);
280
self->myException.setStackTrace(self);
281
self->throwException();
286
e.initCoreError(func, file, line, err);
287
e.log(NULL, "*** Uncaught error");
291
void CSException::throwCoreError(const char *func, const char *file, int line, int err, const char *item)
295
if ((self = CSThread::getSelf())) {
296
self->myException.initCoreError(func, file, line, err, item);
297
self->myException.setStackTrace(self);
298
self->throwException();
303
e.initCoreError(func, file, line, err, item);
304
e.log(NULL, "*** Uncaught error");
308
void CSException::throwOSError(const char *func, const char *file, int line, int err)
312
if ((self = CSThread::getSelf())) {
313
/* A pending signal has priority over a system error,
314
* In fact, the pending signal may be the reason for
318
self->myException.initOSError(func, file, line, err);
319
self->myException.setStackTrace(self);
320
self->throwException();
325
e.initOSError(func, file, line, err);
326
e.log(NULL, "*** Uncaught error");
330
void CSException::throwFileError(const char *func, const char *file, int line, const char *path, int err)
334
if ((self = CSThread::getSelf())) {
336
self->myException.initFileError(func, file, line, path, err);
337
self->myException.setStackTrace(self);
338
self->throwException();
343
e.initFileError(func, file, line, path, err);
344
e.log(NULL, "*** Uncaught error");
348
void CSException::throwFileError(const char *func, const char *file, int line, CSString *path, int err)
352
if ((self = CSThread::getSelf())) {
354
self->myException.initFileError(func, file, line, path->getCString(), err);
355
self->myException.setStackTrace(self);
356
self->throwException();
361
e.initFileError(func, file, line, path->getCString(), err);
362
e.log(NULL, "*** Uncaught error");
366
void CSException::throwSignal(const char *func, const char *file, int line, int sig)
370
if ((self = CSThread::getSelf())) {
371
self->myException.initSignal(func, file, line, sig);
372
self->myException.setStackTrace(self);
373
self->throwException();
378
e.initSignal(func, file, line, sig);
379
e.log(NULL, "*** Uncaught error");
383
void CSException::throwEOFError(const char *func, const char *file, int line, const char *path)
387
if ((self = CSThread::getSelf())) {
389
self->myException.initEOFError(func, file, line, path);
390
self->myException.setStackTrace(self);
391
self->throwException();
396
e.initEOFError(func, file, line, path);
397
e.log(NULL, "*** Uncaught error");
401
void CSException::throwLastError(const char *func, const char *file, int line)
404
throwOSError(func, file, line, (int) getLastError());
406
throwOSError(func, file, line, (int) errno);
410
void CSException::logOSError(const char *func, const char *file, int line, int err)
414
if ((self = CSThread::getSelf())) {
415
self->myException.initOSError(func, file, line, err);
416
self->myException.setStackTrace(self);
417
self->logException();
422
e.initOSError(func, file, line, err);
427
void CSException::logOSError(CSThread *self, const char *func, const char *file, int line, int err)
429
self->myException.initOSError(func, file, line, err);
430
self->myException.setStackTrace(self);
431
self->logException();
434
void CSException::logException(const char *func, const char *file, int line, int err, const char *message)
438
if ((self = CSThread::getSelf())) {
439
self->myException.initException(func, file, line, err, message);
440
self->logException();
445
e.initException(func, file, line, err,message);