GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71785
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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[1024];
105 
106 
107  err_flag = 0;
108  if (errno <= 0)
109  return;
110 
111  *lead = 0;
112  if (who)
113  sprintf(lead, "%s: ", who);
114 
115  if (errno > 0)
116  sprintf(msg, "%s%s: %s", lead, strerror(errno), s);
117 
118  db_error(msg);
119 }
120 
121 /*!
122  \brief Get error code
123 
124  \return DB_OK if not defined
125  */
127 {
128  return err_flag ? err_code : DB_OK;
129 }
130 
131 /*!
132  \brief Report memory error
133 */
134 void db_memory_error(void)
135 {
136  db_error(_("dbmi: Out of Memory"));
137  err_code = DB_MEMORY_ERR;
138 }
139 
140 /*!
141  \brief Report 'not implemented' error
142 
143  \param name name of functionality
144 */
146 {
147  char msg[128];
148 
149  sprintf(msg, _("dbmi: %s() not implemented"), name);
150  db_error(msg);
151  err_code = DB_NOPROC;
152 }
153 
154 /*!
155  \brief Report no procedure error
156 
157  \param procnum procedure number
158 */
160 {
161  char msg[128];
162 
163  sprintf(msg, _("dbmi: Invalid procedure %d"), procnum);
164  db_error(msg);
165  err_code = DB_NOPROC;
166 }
167 
168 /*!
169  \brief Clear error status
170 */
171 void db_clear_error(void)
172 {
173  err_flag = 0;
174  err_code = DB_OK;
175  errno = 0; /* clearn system errno as well */
176 }
177 
178 /*!
179  \brief Print error
180 
181  If not defined, the error message is printed to stderr.
182 */
183 void db_print_error(void)
184 {
185  char lead[1024];
186 
187  if (!err_flag)
188  return;
189 
190  *lead = 0;
191  if (who)
192  sprintf(lead, "%s: ", who);
193 
194  if (user_print_function) {
195  char buf[1024];
196 
197  sprintf(buf, "%s%s\n", lead, err_msg);
198  user_print_function(buf);
199  }
200  else
201  fprintf(stderr, "%s%s\n", lead, err_msg);
202 }
203 
204 
205 static int debug_on = 0;
206 
207 /*!
208  \brief Turn on debugging
209 */
210 void db_debug_on(void)
211 {
212  debug_on = 1;
213 }
214 
215 /*!
216  \brief Turn off debugging
217 */
218 void db_debug_off(void)
219 {
220  debug_on = 0;
221 }
222 
223 /*!
224  \brief Print debug message
225 
226  \param s debug message
227 */
228 void db_debug(const char *s)
229 {
230  if (debug_on)
231  fprintf(stderr, "debug(%s): %s\n", who ? who : "", s ? s : "<NULL>");
232 }
233 
234 /*!
235  \brief Get error message
236 
237  \return pointer to error message string
238 */
239 const char *db_get_error_msg(void)
240 {
241  return err_flag ? err_msg : (const char *)NULL;
242 }
243 
244 /*!
245  \brief Toggles printing of DBMI error messages
246 
247  \param flag ?
248 */
249 void db_auto_print_errors(int flag)
250 {
251  auto_print_errors = flag;
252  auto_print_protocol_errors = flag;
253 }
254 
255 /*!
256  \brief Set auto print protocol error
257 
258  \param flag ?
259  */
261 {
262  auto_print_protocol_errors = flag;
263 }
void db_on_error(void(*f)(const char *))
User defined error procedure.
#define DB_NOPROC
Definition: dbmi.h:73
void db_syserror(const char *s)
Report system error.
#define DB_PROTOCOL_ERR
Definition: dbmi.h:75
void db_auto_print_protocol_errors(int flag)
Set auto print protocol error.
void db_debug_on(void)
Turn on debugging.
void db_debug_off(void)
Turn off debugging.
void db_clear_error(void)
Clear error status.
#define NULL
Definition: ccmath.h:32
void db_set_error_who(const char *me)
Set &#39;who&#39; for error messages.
int db_get_error_code(void)
Get error code.
void db_auto_print_errors(int flag)
Toggles printing of DBMI error messages.
void db_print_error(void)
Print error.
void db_procedure_not_implemented(const char *name)
Report &#39;not implemented&#39; error.
void db_protocol_error(void)
Report protocol error.
const char * db_get_error_who(void)
#define DB_FAILED
Definition: dbmi.h:72
const char * db_get_error_msg(void)
Get error message.
void db_debug(const char *s)
Print debug message.
#define _(str)
Definition: glocale.h:13
char * db_store(const char *s)
Make a copy of string buffer.
void db_noproc_error(int procnum)
Report no procedure error.
const char * name
Definition: named_colr.c:7
#define DB_MEMORY_ERR
Definition: dbmi.h:74
int procnum
Definition: procs.h:34
void db_memory_error(void)
Report memory error.
void db_free(void *s)
Free allocated memory.
void db_error(const char *s)
Report error message.
#define DB_OK
Definition: dbmi.h:71