GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
db/dbmi_base/error.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/error.c
3 
4  \brief DBMI Library (base) - error management
5 
6  (C) 1999-2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Joel Jones (CERL/UIUC)
12  \author Upgraded to GRASS 5.7 by Radim Blazek
13  \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
14  */
15 
16 #include <string.h>
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <grass/dbmi.h>
20 #include <grass/glocale.h>
21 
22 static int err_flag = 0;
23 static int err_code = DB_OK;
24 static char *err_msg = 0;
25 static int auto_print_errors = 1;
26 static int auto_print_protocol_errors = 1;
27 static void (*user_print_function)(const char *);
28 
29 static char *who = NULL;
30 
31 /*!
32  \brief User defined error procedure
33 
34  \param f pointer to user-defined function
35  */
36 void db_on_error(void (*f)(const char *))
37 {
38  user_print_function = f;
39 }
40 
41 /*!
42  \brief Set 'who' for error messages
43 
44  \param me my name
45  */
46 void db_set_error_who(const char *me)
47 {
48  if (who)
49  db_free(who);
50  who = db_store(me);
51 }
52 
53 /*!
54  brief Get 'who' string
55 
56  \return pointer to string buffer
57  \return empty buffer if 'who' is not defined
58  */
59 const char *db_get_error_who(void)
60 {
61  return who ? who : "";
62 }
63 
64 /*!
65  \brief Report error message
66 
67  \param s error message (can be NULL)
68  */
69 void db_error(const char *s)
70 {
71  if (s == NULL)
72  s = _("<NULL error message>");
73  if (err_msg)
74  db_free(err_msg);
75  err_msg = db_store(s);
76  err_flag = 1;
77  if (auto_print_errors)
79  err_code = DB_FAILED;
80 }
81 
82 /*!
83  \brief Report protocol error
84  */
86 {
87  int flag;
88 
89  flag = auto_print_errors;
90  auto_print_errors = auto_print_protocol_errors;
91  db_error(_("dbmi: Protocol error"));
92  auto_print_errors = flag;
93  err_code = DB_PROTOCOL_ERR;
94 }
95 
96 /*!
97  \brief Report system error
98 
99  \param s error message
100  */
101 void db_syserror(const char *s)
102 {
103  char lead[1024];
104  char msg[2048];
105 
106  err_flag = 0;
107  if (errno <= 0)
108  return;
109 
110  *lead = 0;
111  if (who)
112  sprintf(lead, "%s: ", who);
113 
114  if (errno > 0)
115  sprintf(msg, "%s%s: %s", lead, strerror(errno), s);
116 
117  db_error(msg);
118 }
119 
120 /*!
121  \brief Get error code
122 
123  \return DB_OK if not defined
124  */
126 {
127  return err_flag ? err_code : DB_OK;
128 }
129 
130 /*!
131  \brief Report memory error
132  */
133 void db_memory_error(void)
134 {
135  db_error(_("dbmi: Out of Memory"));
136  err_code = DB_MEMORY_ERR;
137 }
138 
139 /*!
140  \brief Report 'not implemented' error
141 
142  \param name name of functionality
143  */
145 {
146  char msg[128];
147 
148  sprintf(msg, _("dbmi: %s() not implemented"), name);
149  db_error(msg);
150  err_code = DB_NOPROC;
151 }
152 
153 /*!
154  \brief Report no procedure error
155 
156  \param procnum procedure number
157  */
159 {
160  char msg[128];
161 
162  sprintf(msg, _("dbmi: Invalid procedure %d"), procnum);
163  db_error(msg);
164  err_code = DB_NOPROC;
165 }
166 
167 /*!
168  \brief Clear error status
169  */
170 void db_clear_error(void)
171 {
172  err_flag = 0;
173  err_code = DB_OK;
174  errno = 0; /* clearn system errno as well */
175 }
176 
177 /*!
178  \brief Print error
179 
180  If not defined, the error message is printed to stderr.
181  */
182 void db_print_error(void)
183 {
184  char lead[1024];
185 
186  if (!err_flag)
187  return;
188 
189  *lead = 0;
190  if (who)
191  sprintf(lead, "%s: ", who);
192 
193  if (user_print_function) {
194  char buf[2048];
195 
196  sprintf(buf, "%s%s\n", lead, err_msg);
197  user_print_function(buf);
198  }
199  else
200  fprintf(stderr, "%s%s\n", lead, err_msg);
201 }
202 
203 static int debug_on = 0;
204 
205 /*!
206  \brief Turn on debugging
207  */
208 void db_debug_on(void)
209 {
210  debug_on = 1;
211 }
212 
213 /*!
214  \brief Turn off debugging
215  */
216 void db_debug_off(void)
217 {
218  debug_on = 0;
219 }
220 
221 /*!
222  \brief Print debug message
223 
224  \param s debug message
225  */
226 void db_debug(const char *s)
227 {
228  if (debug_on)
229  fprintf(stderr, "debug(%s): %s\n", who ? who : "", s ? s : "<NULL>");
230 }
231 
232 /*!
233  \brief Get error message
234 
235  \return pointer to error message string
236  */
237 const char *db_get_error_msg(void)
238 {
239  return err_flag ? err_msg : (const char *)NULL;
240 }
241 
242 /*!
243  \brief Toggles printing of DBMI error messages
244 
245  \param flag ?
246  */
247 void db_auto_print_errors(int flag)
248 {
249  auto_print_errors = flag;
250  auto_print_protocol_errors = flag;
251 }
252 
253 /*!
254  \brief Set auto print protocol error
255 
256  \param flag ?
257  */
259 {
260  auto_print_protocol_errors = flag;
261 }
#define NULL
Definition: ccmath.h:32
const char * db_get_error_who(void)
void db_error(const char *s)
Report error message.
void db_debug_off(void)
Turn off debugging.
void db_debug(const char *s)
Print debug message.
void db_on_error(void(*f)(const char *))
User defined error procedure.
void db_print_error(void)
Print error.
void db_debug_on(void)
Turn on debugging.
void db_set_error_who(const char *me)
Set 'who' for error messages.
void db_auto_print_protocol_errors(int flag)
Set auto print protocol error.
void db_procedure_not_implemented(const char *name)
Report 'not implemented' error.
void db_syserror(const char *s)
Report system error.
void db_clear_error(void)
Clear error status.
int db_get_error_code(void)
Get error code.
const char * db_get_error_msg(void)
Get error message.
void db_auto_print_errors(int flag)
Toggles printing of DBMI error messages.
void db_protocol_error(void)
Report protocol error.
void db_noproc_error(int procnum)
Report no procedure error.
void db_memory_error(void)
Report memory error.
#define DB_NOPROC
Definition: dbmi.h:73
#define DB_FAILED
Definition: dbmi.h:72
#define DB_PROTOCOL_ERR
Definition: dbmi.h:75
#define DB_OK
Definition: dbmi.h:71
#define DB_MEMORY_ERR
Definition: dbmi.h:74
void db_free(void *)
Free allocated memory.
char * db_store(const char *)
Make a copy of string buffer.
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:6
int procnum
Definition: procs.h:33