-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWErrorHandling.c
197 lines (165 loc) · 6.11 KB
/
CWErrorHandling.c
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
/*******************************************************************************************
* Copyright (c) 2006-7 Laboratorio di Sistemi di Elaborazione e Bioingegneria Informatica *
* Universita' Campus BioMedico - Italy *
* *
* This program is free software; you can redistribute it and/or modify it under the terms *
* of the GNU General Public License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with this *
* program; if not, write to the: *
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, *
* MA 02111-1307, USA. *
* *
* --------------------------------------------------------------------------------------- *
* Project: Capwap *
* *
* Author : Ludovico Rossi ([email protected]) *
* Del Moro Andrea ([email protected]) *
* Giovannini Federica ([email protected]) *
* Massimo Vellucci ([email protected]) *
* Mauro Bisson ([email protected]) *
*******************************************************************************************/
#include "CWCommon.h"
#include "CWAC.h"
#ifdef DMALLOC
#include "../dmalloc-5.5.0/dmalloc.h"
#endif
#ifndef CW_SINGLE_THREAD
CWThreadSpecific gLastError;
//CWThreadOnce gInitLastErrorOnce = CW_THREAD_ONCE_INIT;
#else
static CWErrorHandlingInfo *gLastErrorDataPtr;
#endif
void CWErrorHandlingInitLib() {
CWDebugLog("Init Errors ");
#ifndef CW_SINGLE_THREAD
if(!CWThreadCreateSpecific(&gLastError, NULL))
{
CWLog("Critical Error, closing the process...");
exit(1);
}
#else
CWErrorHandlingInfo *infoPtr;
CW_CREATE_OBJECT_ERR(infoPtr, CWErrorHandlingInfo, return;);
infoPtr->code = CW_ERROR_NONE;
gLastErrorDataPtr = infoPtr;
#endif
}
CWBool _CWErrorRaise(CWErrorCode code, const char *msg, const char *fileName, int line) {
CWErrorHandlingInfo *infoPtr;
#ifndef CW_SINGLE_THREAD
infoPtr = CWThreadGetSpecific(&gLastError);
if(infoPtr==NULL){
CW_CREATE_OBJECT_ERR(infoPtr, CWErrorHandlingInfo, exit(1););
infoPtr->code = CW_ERROR_NONE;
if(!CWThreadSetSpecific(&gLastError, infoPtr))
{
CWLog("Critical Error, closing the process...");
exit(1);
}
}
#else
infoPtr = gLastErrorDataPtr;
#endif
if(infoPtr == NULL)
{
CWLog("Critical Error: something strange has happened, closing the process...");
exit(1);
}
infoPtr->code = code;
if(msg != NULL && strlen(msg) < 256)
{
strcpy(infoPtr->message, msg);
infoPtr->message[strlen(msg)]='\0';
}
else infoPtr->message[0]='\0';
if(fileName != NULL && strlen(fileName) < 64)
{
strcpy(infoPtr->fileName, fileName);
infoPtr->fileName[strlen(fileName)]='\0';
}
else infoPtr->fileName[0]='\0';
infoPtr->line = line;
return CW_FALSE;
}
void CWErrorPrint(CWErrorHandlingInfo *infoPtr, const char *desc, const char *fileName, int line) {
if(infoPtr == NULL) return;
if(infoPtr->message != NULL && infoPtr->message[0]!='\0') {
CWLog("Error: %s. %s .", desc, infoPtr->message);
} else {
CWLog("Error: %s", desc);
}
CWLog("(occurred at line %d in file %s, catched at line %d in file %s).",
infoPtr->line, infoPtr->fileName, line, fileName);
}
CWErrorCode CWErrorGetLastErrorCode() {
CWErrorHandlingInfo *infoPtr;
#ifndef CW_SINGLE_THREAD
infoPtr = CWThreadGetSpecific(&gLastError);
#else
infoPtr = gLastErrorDataPtr;
#endif
if(infoPtr == NULL) return CW_ERROR_GENERAL;
return infoPtr->code;
}
CWBool _CWErrorHandleLast(const char *fileName, int line) {
CWErrorHandlingInfo *infoPtr;
#ifndef CW_SINGLE_THREAD
infoPtr = CWThreadGetSpecific(&gLastError);
#else
infoPtr = gLastErrorDataPtr;
#endif
if(infoPtr == NULL) {
CWLog("No Error Pending");
//can't exit
// exit((3));
return CW_FALSE;
}
#define __CW_ERROR_PRINT(str) CWErrorPrint(infoPtr, (str), fileName, line)
switch(infoPtr->code) {
case CW_ERROR_SUCCESS:
case CW_ERROR_NONE:
return CW_TRUE;
break;
case CW_ERROR_OUT_OF_MEMORY:
__CW_ERROR_PRINT("Out of Memory");
#ifndef CW_SINGLE_THREAD
//CWExitThread(); // note: we can manage this on per-thread basis: ex. we can
// kill some other thread if we are a manager thread.
CWCloseThread();
#else
exit(1);
#endif
break;
case CW_ERROR_WRONG_ARG:
__CW_ERROR_PRINT("Wrong Arguments in Function");
break;
case CW_ERROR_NEED_RESOURCE:
__CW_ERROR_PRINT("Missing Resource");
break;
case CW_ERROR_GENERAL:
__CW_ERROR_PRINT("Error Occurred");
break;
case CW_ERROR_CREATING:
__CW_ERROR_PRINT("Error Creating Resource");
break;
case CW_ERROR_SENDING:
__CW_ERROR_PRINT("Error Sending");
break;
case CW_ERROR_RECEIVING:
__CW_ERROR_PRINT("Error Receiving");
break;
case CW_ERROR_INVALID_FORMAT:
__CW_ERROR_PRINT("Invalid Format");
break;
case CW_ERROR_INTERRUPTED:
default:
break;
}
return CW_FALSE;
}