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)();
00013 
00014 static char *who = NULL;
00015 
00022 void
00023 db_on_error (void (*f)())
00024 
00025 {
00026     user_print_function = f;
00027 }
00028 
00035 void
00036 db_set_error_who (char *me)
00037 
00038 {
00039     if (who) free(who);
00040     who = db_store(me);
00041 }
00042 
00049 char *
00050 db_get_error_who()
00051 {
00052     return who?who:"";
00053 }
00054 
00061 void
00062 db_error (char *s)
00063 
00064 {
00065     if (s == NULL)
00066         s = "<NULL error message>";
00067     if(err_msg)
00068         free(err_msg);
00069     err_msg =  db_store(s);
00070     err_flag = 1;
00071     if (auto_print_errors)
00072         db_print_error();
00073     err_code = DB_FAILED;
00074 }
00075 
00082 void
00083 db_protocol_error()
00084 {
00085     int flag;
00086 
00087     flag = auto_print_errors;
00088     auto_print_errors = auto_print_protocol_errors;
00089     db_error ("dbmi: Protocol error");
00090     auto_print_errors = flag;
00091     err_code = DB_PROTOCOL_ERR;
00092 }
00093 
00100 void
00101 db_syserror (char *s)
00102 
00103 {
00104     char lead[1024];
00105     char msg[1024];
00106 
00107 
00108     err_flag = 0;
00109     if (errno <= 0) return;
00110 
00111     *lead = 0;
00112     if (who)
00113         sprintf (lead, "%s: ", who);
00114 
00115     if (errno > 0)
00116         sprintf (msg, "%s%s: %s", lead, strerror(errno), s);
00117 
00118     db_error (msg);
00119 }
00120 
00127 int
00128 db_get_error_code()
00129 {
00130     return err_flag ? err_code : DB_OK;
00131 }
00132 
00139 void
00140 db_memory_error()
00141 {
00142     db_error ("dbmi: Out of Memory");
00143     err_code = DB_MEMORY_ERR;
00144 }
00145 
00152 void
00153 db_procedure_not_implemented (char *name)
00154 
00155 {
00156     char msg[128];
00157 
00158     sprintf (msg, "dbmi: %s() not implemented", name);
00159     db_error (msg);
00160     err_code = DB_NOPROC;
00161 }
00162 
00169 void
00170 db_noproc_error(procnum)
00171 {
00172     char msg[128];
00173 
00174     sprintf (msg, "dbmi: Invalid procedure %d", procnum);
00175     db_error (msg);
00176     err_code = DB_NOPROC;
00177 }
00178 
00185 void
00186 db_clear_error()
00187 {
00188     err_flag = 0;
00189     err_code = DB_OK;
00190     errno    = 0;       /* clearn system errno as well */
00191 }
00192 
00199 void
00200 db_print_error()
00201 {
00202     char lead[1024];
00203 
00204     if(!err_flag) return;
00205 
00206     *lead = 0;
00207     if (who)
00208         sprintf (lead, "%s: ", who);
00209 
00210     if (user_print_function) {
00211         char buf[1024];
00212         sprintf(buf, "%s%s\n", lead, err_msg);
00213         user_print_function(buf);
00214     }
00215     else
00216         fprintf (stderr, "%s%s\n", lead, err_msg);
00217 }
00218 
00219 
00220 static int debug_on = 0;
00221 
00228 void
00229 db_debug_on()
00230 {
00231     debug_on = 1;
00232 }
00233 
00240 void
00241 db_debug_off()
00242 {
00243     debug_on = 0;
00244 }
00245 
00252 void
00253 db_debug (char *s)
00254 
00255 {
00256     if (debug_on)
00257         fprintf (stderr, "debug(%s): %s\n", who?who:"", s?s:"<NULL>");
00258 }
00259 
00266 char *
00267 db_get_error_msg()
00268 {
00269     return err_flag ? err_msg : (char *)NULL;
00270 }
00271 
00278 void
00279 db_auto_print_errors (flag)
00280 {
00281     auto_print_errors = flag;
00282     auto_print_protocol_errors = flag;
00283 }
00284 
00291 void
00292 db_auto_print_protocol_errors (flag)
00293 {
00294     auto_print_protocol_errors = flag;
00295 }

Generated on Sun Apr 6 17:31:38 2008 for GRASS by  doxygen 1.5.5