This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtError.c
186 lines (153 loc) · 4.32 KB
/
rtError.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
/*
pxCore Copyright 2005-2017 John Robinson
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// rtError.cpp
#include "rtError.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdio.h>
#define RT_ERROR_CASE(ERR) case ERR: s = # ERR; break;
#define RT_ERROR_CLASS(ERR) (((ERR) >> 16) & 0x0000ffff)
#define RT_ERROR_CODE(ERR) ((ERR) & 0x0000ffff)
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_key_t key;
static const char* rtError_ToString_BuiltIn(rtError e);
static const char* rtError_ToString_SystemError(int e);
typedef struct
{
char error_message[256];
rtError last_error;
} rtErrorThreadSpecific;
static void rtErrorInitThreadSpecificKey()
{
pthread_key_create(&key, NULL);
}
rtErrorThreadSpecific* getThreadSpecific()
{
pthread_once(&once, rtErrorInitThreadSpecificKey);
rtErrorThreadSpecific* specific = (rtErrorThreadSpecific *)(pthread_getspecific(key));
if (specific == NULL)
{
specific = (rtErrorThreadSpecific *) malloc(sizeof(rtErrorThreadSpecific));
specific->last_error = 0;
specific->error_message[0] = '\0';
pthread_setspecific(key, specific);
}
return specific;
}
const char* rtStrError(rtError e)
{
return rtError_ToString(e);
}
const char* rtError_ToString(rtError e)
{
const char* s = NULL;
int klass = RT_ERROR_CLASS(e);
switch (klass)
{
case RT_ERROR_CLASS_BUILTIN:
s = rtError_ToString_BuiltIn(RT_ERROR_CODE(e));
break;
case RT_ERROR_CLASS_SYSERROR:
s = rtError_ToString_SystemError(RT_ERROR_CODE(e));
break;
default:
return "UNKNOWN_ERROR";
break;
}
return s;
}
rtError rtErrorGetLastError()
{
return rtError_GetLastError();
}
rtError rtError_GetLastError()
{
rtError current = 0;
rtErrorThreadSpecific* specific = getThreadSpecific();
if (specific)
current = specific->last_error;
return current;
}
void rtErrorSetLastError(rtError e)
{
return rtError_SetLastError(e);
}
void rtError_SetLastError(rtError e)
{
rtErrorThreadSpecific* specific = getThreadSpecific();
if (specific)
specific->last_error = e;
}
const char* rtError_ToString_SystemError(int e)
{
rtErrorThreadSpecific* specific = getThreadSpecific();
char* buff = NULL;
// TODO: The below #ifdef is not the best approach. @see man strerror_r and
// /usr/include/string.h for proper check of which version of strerror_r is
// available. Need to check for osx portability also
if (specific->error_message)
{
#ifdef __linux__
int n = strerror_r(e, specific->error_message, 256);
if (!n)
buff = specific->error_message;
#else
buff = strerror(e);
#endif
}
return buff;
}
const char* rtError_ToString_BuiltIn(rtError e)
{
const char* s = "UNKNOWN";
switch (e)
{
RT_ERROR_CASE(RT_OK);
RT_ERROR_CASE(RT_FAIL);
RT_ERROR_CASE(RT_ERROR_NOT_ENOUGH_ARGS);
RT_ERROR_CASE(RT_ERROR_INVALID_ARG);
RT_ERROR_CASE(RT_PROP_NOT_FOUND);
RT_ERROR_CASE(RT_OBJECT_NOT_INITIALIZED);
RT_ERROR_CASE(RT_PROPERTY_NOT_FOUND);
RT_ERROR_CASE(RT_OBJECT_NO_LONGER_AVAILABLE);
RT_ERROR_CASE(RT_RESOURCE_NOT_FOUND);
RT_ERROR_CASE(RT_NO_CONNECTION);
RT_ERROR_CASE(RT_ERROR_NOT_IMPLEMENTED);
RT_ERROR_CASE(RT_ERROR_TYPE_MISMATCH);
RT_ERROR_CASE(RT_ERROR_TIMEOUT);
RT_ERROR_CASE(RT_ERROR_DUPLICATE_ENTRY);
RT_ERROR_CASE(RT_ERROR_OBJECT_NOT_FOUND);
RT_ERROR_CASE(RT_ERROR_PROTOCOL_ERROR);
RT_ERROR_CASE(RT_ERROR_INVALID_OPERATION);
RT_ERROR_CASE(RT_ERROR_IN_PROGRESS);
RT_ERROR_CASE(RT_ERROR_QUEUE_EMPTY);
RT_ERROR_CASE(RT_ERROR_STREAM_CLOSED);
default:
break;
}
return s;
}
rtError
rtErrorFromErrno(int err)
{
return rtError_FromErrno(err);
}
rtError
rtError_FromErrno(int err)
{
// RT_ASSERT(err <= 65535); // uint16_max
return err == 0 ? RT_OK : (rtError) (err | (RT_ERROR_CLASS_SYSERROR << 16));
}