-
-
Notifications
You must be signed in to change notification settings - Fork 291
/
error.c
263 lines (211 loc) · 4.33 KB
/
error.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
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
/*!
\file lib/db/dbmi_base/error.c
\brief DBMI Library (base) - error management
(C) 1999-2011 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author Joel Jones (CERL/UIUC)
\author Upgraded to GRASS 5.7 by Radim Blazek
\author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
*/
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
static int err_flag = 0;
static int err_code = DB_OK;
static char *err_msg = 0;
static int auto_print_errors = 1;
static int auto_print_protocol_errors = 1;
static void (*user_print_function) (const char *);
static char *who = NULL;
/*!
\brief User defined error procedure
\param f pointer to user-defined function
*/
void db_on_error(void (*f) (const char *))
{
user_print_function = f;
}
/*!
\brief Set 'who' for error messages
\param me my name
*/
void db_set_error_who(const char *me)
{
if (who)
db_free(who);
who = db_store(me);
}
/*!
brief Get 'who' string
\return pointer to string buffer
\return empty buffer if 'who' is not defined
*/
const char *db_get_error_who(void)
{
return who ? who : "";
}
/*!
\brief Report error message
\param s error message (can be NULL)
*/
void db_error(const char *s)
{
if (s == NULL)
s = _("<NULL error message>");
if (err_msg)
db_free(err_msg);
err_msg = db_store(s);
err_flag = 1;
if (auto_print_errors)
db_print_error();
err_code = DB_FAILED;
}
/*!
\brief Report protocol error
*/
void db_protocol_error(void)
{
int flag;
flag = auto_print_errors;
auto_print_errors = auto_print_protocol_errors;
db_error(_("dbmi: Protocol error"));
auto_print_errors = flag;
err_code = DB_PROTOCOL_ERR;
}
/*!
\brief Report system error
\param s error message
*/
void db_syserror(const char *s)
{
char lead[1024];
char msg[1024];
err_flag = 0;
if (errno <= 0)
return;
*lead = 0;
if (who)
sprintf(lead, "%s: ", who);
if (errno > 0)
sprintf(msg, "%s%s: %s", lead, strerror(errno), s);
db_error(msg);
}
/*!
\brief Get error code
\return DB_OK if not defined
*/
int db_get_error_code(void)
{
return err_flag ? err_code : DB_OK;
}
/*!
\brief Report memory error
*/
void db_memory_error(void)
{
db_error(_("dbmi: Out of Memory"));
err_code = DB_MEMORY_ERR;
}
/*!
\brief Report 'not implemented' error
\param name name of functionality
*/
void db_procedure_not_implemented(const char *name)
{
char msg[128];
sprintf(msg, _("dbmi: %s() not implemented"), name);
db_error(msg);
err_code = DB_NOPROC;
}
/*!
\brief Report no procedure error
\param procnum procedure number
*/
void db_noproc_error(procnum)
{
char msg[128];
sprintf(msg, _("dbmi: Invalid procedure %d"), procnum);
db_error(msg);
err_code = DB_NOPROC;
}
/*!
\brief Clear error status
*/
void db_clear_error(void)
{
err_flag = 0;
err_code = DB_OK;
errno = 0; /* clearn system errno as well */
}
/*!
\brief Print error
If not defined, the error message is printed to stderr.
*/
void db_print_error(void)
{
char lead[1024];
if (!err_flag)
return;
*lead = 0;
if (who)
sprintf(lead, "%s: ", who);
if (user_print_function) {
char buf[1024];
sprintf(buf, "%s%s\n", lead, err_msg);
user_print_function(buf);
}
else
fprintf(stderr, "%s%s\n", lead, err_msg);
}
static int debug_on = 0;
/*!
\brief Turn on debugging
*/
void db_debug_on(void)
{
debug_on = 1;
}
/*!
\brief Turn off debugging
*/
void db_debug_off(void)
{
debug_on = 0;
}
/*!
\brief Print debug message
\param s debug message
*/
void db_debug(const char *s)
{
if (debug_on)
fprintf(stderr, "debug(%s): %s\n", who ? who : "", s ? s : "<NULL>");
}
/*!
\brief Get error message
\return pointer to error message string
*/
const char *db_get_error_msg(void)
{
return err_flag ? err_msg : (const char *)NULL;
}
/*!
\brief Toggles printing of DBMI error messages
\param flag ?
*/
void db_auto_print_errors(int flag)
{
auto_print_errors = flag;
auto_print_protocol_errors = flag;
}
/*!
\brief Set auto print protocol error
\param flag ?
*/
void db_auto_print_protocol_errors(int flag)
{
auto_print_protocol_errors = flag;
}