| 1 |
/* |
| 2 |
error.c - Part of psiconv, a PSION 5 file formats converter |
| 3 |
Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program; if not, write to the Free Software |
| 17 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include <stdarg.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
#include "data.h" |
| 26 |
#include "error.h" |
| 27 |
|
| 28 |
static void psiconv_default_error_handler(int kind, psiconv_u32 off, |
| 29 |
const char *message) |
| 30 |
{ |
| 31 |
fprintf(stderr,"%s\n",message); |
| 32 |
} |
| 33 |
|
| 34 |
int psiconv_verbosity = PSICONV_VERB_WARN; |
| 35 |
|
| 36 |
psiconv_error_handler_t psiconv_error_handler = psiconv_default_error_handler; |
| 37 |
|
| 38 |
|
| 39 |
#define MAX_MESSAGE 160 |
| 40 |
|
| 41 |
void psiconv_fatal(int level, psiconv_u32 off, const char *format,...) |
| 42 |
{ |
| 43 |
char buffer[MAX_MESSAGE]; |
| 44 |
va_list ap; |
| 45 |
size_t curlen; |
| 46 |
|
| 47 |
va_start(ap,format); |
| 48 |
snprintf(buffer,MAX_MESSAGE,"Fatal error (offset %08x): ",off); |
| 49 |
curlen = strlen(buffer); |
| 50 |
|
| 51 |
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
| 52 |
psiconv_error_handler(PSICONV_VERB_FATAL,off,buffer); |
| 53 |
va_end(ap); |
| 54 |
|
| 55 |
exit(1); |
| 56 |
} |
| 57 |
|
| 58 |
void psiconv_warn(int level, psiconv_u32 off, const char *format,...) |
| 59 |
{ |
| 60 |
char buffer[MAX_MESSAGE]; |
| 61 |
va_list ap; |
| 62 |
size_t curlen; |
| 63 |
|
| 64 |
va_start(ap,format); |
| 65 |
|
| 66 |
if (psiconv_verbosity >= PSICONV_VERB_WARN) { |
| 67 |
snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off); |
| 68 |
curlen = strlen(buffer); |
| 69 |
|
| 70 |
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
| 71 |
psiconv_error_handler(PSICONV_VERB_WARN,off,buffer); |
| 72 |
} |
| 73 |
va_end(ap); |
| 74 |
} |
| 75 |
|
| 76 |
void psiconv_progress(int level, psiconv_u32 off, const char *format,...) |
| 77 |
{ |
| 78 |
char buffer[MAX_MESSAGE]; |
| 79 |
va_list ap; |
| 80 |
size_t curlen; |
| 81 |
int i; |
| 82 |
|
| 83 |
va_start(ap,format); |
| 84 |
if (psiconv_verbosity >= PSICONV_VERB_PROGRESS) { |
| 85 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
| 86 |
curlen = strlen(buffer); |
| 87 |
|
| 88 |
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
| 89 |
buffer[i+curlen] = '='; |
| 90 |
curlen += i; |
| 91 |
|
| 92 |
buffer[curlen] = '>'; |
| 93 |
buffer[curlen+1] = ' '; |
| 94 |
buffer[curlen+2] = '\0'; |
| 95 |
curlen += 2; |
| 96 |
|
| 97 |
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
| 98 |
|
| 99 |
psiconv_error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
| 100 |
} |
| 101 |
|
| 102 |
va_end(ap); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
void psiconv_debug(int level, psiconv_u32 off, const char *format,...) |
| 107 |
{ |
| 108 |
char buffer[MAX_MESSAGE]; |
| 109 |
va_list ap; |
| 110 |
size_t curlen; |
| 111 |
int i; |
| 112 |
|
| 113 |
va_start(ap,format); |
| 114 |
if (psiconv_verbosity >= PSICONV_VERB_DEBUG) { |
| 115 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
| 116 |
curlen = strlen(buffer); |
| 117 |
|
| 118 |
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
| 119 |
buffer[i+curlen] = '-'; |
| 120 |
curlen += i; |
| 121 |
|
| 122 |
buffer[curlen] = '>'; |
| 123 |
buffer[curlen+1] = ' '; |
| 124 |
buffer[curlen+2] = '\0'; |
| 125 |
curlen += 2; |
| 126 |
|
| 127 |
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
| 128 |
|
| 129 |
psiconv_error_handler(PSICONV_VERB_DEBUG,off,buffer); |
| 130 |
} |
| 131 |
va_end(ap); |
| 132 |
} |