--- psiconv/trunk/lib/psiconv/error.c 2002/01/29 18:38:38 142 +++ psiconv/trunk/lib/psiconv/error.c 2004/02/22 22:24:39 217 @@ -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,6 +22,7 @@ #include #include #include +#include #include "error.h" @@ -35,14 +36,10 @@ fprintf(stderr,"%s\n",message); } -int psiconv_verbosity = PSICONV_VERB_WARN; +#define MAX_MESSAGE 1024 -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; @@ -53,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; @@ -67,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; @@ -85,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); @@ -100,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; @@ -115,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); @@ -130,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); }