error.c
Go to the documentation of this file.00001 #include <string.h>
00002 #include <stdlib.h>
00003 #include <grass/dbmi.h>
00004
00005 #include <errno.h>
00006
00007 static int err_flag = 0;
00008 static int err_code = DB_OK;
00009 static char *err_msg = 0;
00010 static int auto_print_errors = 1;
00011 static int auto_print_protocol_errors = 1;
00012 static void (*user_print_function) (const char *);
00013
00014 static char *who = NULL;
00015
00022 void db_on_error(void (*f) (const char *))
00023 {
00024 user_print_function = f;
00025 }
00026
00033 void db_set_error_who(const char *me)
00034 {
00035 if (who)
00036 free(who);
00037 who = db_store(me);
00038 }
00039
00046 const char *db_get_error_who(void)
00047 {
00048 return who ? who : "";
00049 }
00050
00057 void db_error(const char *s)
00058 {
00059 if (s == NULL)
00060 s = "<NULL error message>";
00061 if (err_msg)
00062 free(err_msg);
00063 err_msg = db_store(s);
00064 err_flag = 1;
00065 if (auto_print_errors)
00066 db_print_error();
00067 err_code = DB_FAILED;
00068 }
00069
00076 void db_protocol_error(void)
00077 {
00078 int flag;
00079
00080 flag = auto_print_errors;
00081 auto_print_errors = auto_print_protocol_errors;
00082 db_error("dbmi: Protocol error");
00083 auto_print_errors = flag;
00084 err_code = DB_PROTOCOL_ERR;
00085 }
00086
00093 void db_syserror(const char *s)
00094 {
00095 char lead[1024];
00096 char msg[1024];
00097
00098
00099 err_flag = 0;
00100 if (errno <= 0)
00101 return;
00102
00103 *lead = 0;
00104 if (who)
00105 sprintf(lead, "%s: ", who);
00106
00107 if (errno > 0)
00108 sprintf(msg, "%s%s: %s", lead, strerror(errno), s);
00109
00110 db_error(msg);
00111 }
00112
00119 int db_get_error_code(void)
00120 {
00121 return err_flag ? err_code : DB_OK;
00122 }
00123
00130 void db_memory_error(void)
00131 {
00132 db_error("dbmi: Out of Memory");
00133 err_code = DB_MEMORY_ERR;
00134 }
00135
00142 void db_procedure_not_implemented(const char *name)
00143 {
00144 char msg[128];
00145
00146 sprintf(msg, "dbmi: %s() not implemented", name);
00147 db_error(msg);
00148 err_code = DB_NOPROC;
00149 }
00150
00157 void db_noproc_error(procnum)
00158 {
00159 char msg[128];
00160
00161 sprintf(msg, "dbmi: Invalid procedure %d", procnum);
00162 db_error(msg);
00163 err_code = DB_NOPROC;
00164 }
00165
00172 void db_clear_error(void)
00173 {
00174 err_flag = 0;
00175 err_code = DB_OK;
00176 errno = 0;
00177 }
00178
00185 void db_print_error(void)
00186 {
00187 char lead[1024];
00188
00189 if (!err_flag)
00190 return;
00191
00192 *lead = 0;
00193 if (who)
00194 sprintf(lead, "%s: ", who);
00195
00196 if (user_print_function) {
00197 char buf[1024];
00198
00199 sprintf(buf, "%s%s\n", lead, err_msg);
00200 user_print_function(buf);
00201 }
00202 else
00203 fprintf(stderr, "%s%s\n", lead, err_msg);
00204 }
00205
00206
00207 static int debug_on = 0;
00208
00215 void db_debug_on(void)
00216 {
00217 debug_on = 1;
00218 }
00219
00226 void db_debug_off(void)
00227 {
00228 debug_on = 0;
00229 }
00230
00237 void db_debug(const char *s)
00238 {
00239 if (debug_on)
00240 fprintf(stderr, "debug(%s): %s\n", who ? who : "", s ? s : "<NULL>");
00241 }
00242
00249 const char *db_get_error_msg(void)
00250 {
00251 return err_flag ? err_msg : (const char *)NULL;
00252 }
00253
00260 void db_auto_print_errors(int flag)
00261 {
00262 auto_print_errors = flag;
00263 auto_print_protocol_errors = flag;
00264 }
00265
00272 void db_auto_print_protocol_errors(int flag)
00273 {
00274 auto_print_protocol_errors = flag;
00275 }