--- psiconv/trunk/lib/psiconv/error.c 2000/12/22 22:31:50 71 +++ psiconv/trunk/lib/psiconv/error.c 2004/02/04 12:19:09 196 @@ -1,6 +1,6 @@ /* error.c - Part of psiconv, a PSION 5 file formats converter - Copyright (c) 1999, 2000 Frodo Looijaard + Copyright (c) 1999-2004 Frodo Looijaard This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,23 +22,24 @@ #include #include #include +#include #include "error.h" +#ifdef DMALLOC +#include +#endif + static void psiconv_default_error_handler(int kind, psiconv_u32 off, const char *message) { fprintf(stderr,"%s\n",message); } -int psiconv_verbosity = PSICONV_VERB_WARN; - -psiconv_error_handler_t psiconv_error_handler = psiconv_default_error_handler; - - #define MAX_MESSAGE 160 -void psiconv_fatal(int level, psiconv_u32 off, const char *format,...) +void psiconv_fatal(psiconv_config config, int level, psiconv_u32 off, + const char *format,...) { char buffer[MAX_MESSAGE]; va_list ap; @@ -49,13 +50,39 @@ curlen = strlen(buffer); vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); - psiconv_error_handler(PSICONV_VERB_FATAL,off,buffer); + if (config->error_handler) + config->error_handler(PSICONV_VERB_FATAL,off,buffer); + else + psiconv_default_error_handler(PSICONV_VERB_FATAL,off,buffer); va_end(ap); exit(1); } -void psiconv_warn(int level, psiconv_u32 off, const char *format,...) +void psiconv_error(psiconv_config config, int level, psiconv_u32 off, + const char *format,...) +{ + char buffer[MAX_MESSAGE]; + va_list ap; + size_t curlen; + + va_start(ap,format); + + if (config->verbosity >= PSICONV_VERB_ERROR) { + snprintf(buffer,MAX_MESSAGE,"ERROR (offset %08x): ",off); + curlen = strlen(buffer); + + vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); + if (config->error_handler) + config->error_handler(PSICONV_VERB_ERROR,off,buffer); + else + psiconv_default_error_handler(PSICONV_VERB_ERROR,off,buffer); + } + va_end(ap); +} + +void psiconv_warn(psiconv_config config, int level, psiconv_u32 off, + const char *format,...) { char buffer[MAX_MESSAGE]; va_list ap; @@ -63,17 +90,21 @@ va_start(ap,format); - if (psiconv_verbosity >= PSICONV_VERB_WARN) { + if (config->verbosity >= PSICONV_VERB_WARN) { snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off); curlen = strlen(buffer); vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); - psiconv_error_handler(PSICONV_VERB_WARN,off,buffer); + if (config->error_handler) + config->error_handler(PSICONV_VERB_WARN,off,buffer); + else + psiconv_default_error_handler(PSICONV_VERB_WARN,off,buffer); } va_end(ap); } -void psiconv_progress(int level, psiconv_u32 off, const char *format,...) +void psiconv_progress(psiconv_config config,int level, psiconv_u32 off, + const char *format,...) { char buffer[MAX_MESSAGE]; va_list ap; @@ -81,7 +112,7 @@ int i; va_start(ap,format); - if (psiconv_verbosity >= PSICONV_VERB_PROGRESS) { + if (config->verbosity >= PSICONV_VERB_PROGRESS) { snprintf(buffer,MAX_MESSAGE,"%08x ",off); curlen = strlen(buffer); @@ -96,14 +127,18 @@ vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); - psiconv_error_handler(PSICONV_VERB_PROGRESS,off,buffer); + if (config->error_handler) + config->error_handler(PSICONV_VERB_PROGRESS,off,buffer); + else + psiconv_default_error_handler(PSICONV_VERB_PROGRESS,off,buffer); } va_end(ap); } -void psiconv_debug(int level, psiconv_u32 off, const char *format,...) +void psiconv_debug(psiconv_config config, int level, psiconv_u32 off, + const char *format,...) { char buffer[MAX_MESSAGE]; va_list ap; @@ -111,7 +146,7 @@ int i; va_start(ap,format); - if (psiconv_verbosity >= PSICONV_VERB_DEBUG) { + if (config->verbosity >= PSICONV_VERB_DEBUG) { snprintf(buffer,MAX_MESSAGE,"%08x ",off); curlen = strlen(buffer); @@ -126,7 +161,10 @@ vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); - psiconv_error_handler(PSICONV_VERB_DEBUG,off,buffer); + if (config->error_handler) + config->error_handler(PSICONV_VERB_DEBUG,off,buffer); + else + psiconv_default_error_handler(PSICONV_VERB_DEBUG,off,buffer); } va_end(ap); }