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 "compat.h" |
22 |
#include <stdarg.h> |
23 |
#include <stdio.h> |
24 |
#include <stdlib.h> |
25 |
#include <string.h> |
26 |
|
27 |
#include "error.h" |
28 |
#include "config.h" |
29 |
|
30 |
#ifdef DMALLOC |
31 |
#include <dmalloc.h> |
32 |
#endif |
33 |
|
34 |
static void psiconv_default_error_handler(int kind, psiconv_u32 off, |
35 |
const char *message) |
36 |
{ |
37 |
fprintf(stderr,"%s\n",message); |
38 |
} |
39 |
|
40 |
psiconv_error_handler_t psiconv_error_handler = psiconv_default_error_handler; |
41 |
|
42 |
|
43 |
#define MAX_MESSAGE 160 |
44 |
|
45 |
void psiconv_fatal(psiconv_config config, int level, psiconv_u32 off, |
46 |
const char *format,...) |
47 |
{ |
48 |
char buffer[MAX_MESSAGE]; |
49 |
va_list ap; |
50 |
size_t curlen; |
51 |
|
52 |
va_start(ap,format); |
53 |
snprintf(buffer,MAX_MESSAGE,"Fatal error (offset %08x): ",off); |
54 |
curlen = strlen(buffer); |
55 |
|
56 |
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
57 |
psiconv_error_handler(PSICONV_VERB_FATAL,off,buffer); |
58 |
va_end(ap); |
59 |
|
60 |
exit(1); |
61 |
} |
62 |
|
63 |
void psiconv_warn(psiconv_config config, int level, psiconv_u32 off, |
64 |
const char *format,...) |
65 |
{ |
66 |
char buffer[MAX_MESSAGE]; |
67 |
va_list ap; |
68 |
size_t curlen; |
69 |
|
70 |
va_start(ap,format); |
71 |
|
72 |
if (config->verbosity >= PSICONV_VERB_WARN) { |
73 |
snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off); |
74 |
curlen = strlen(buffer); |
75 |
|
76 |
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
77 |
psiconv_error_handler(PSICONV_VERB_WARN,off,buffer); |
78 |
} |
79 |
va_end(ap); |
80 |
} |
81 |
|
82 |
void psiconv_progress(psiconv_config config,int level, psiconv_u32 off, |
83 |
const char *format,...) |
84 |
{ |
85 |
char buffer[MAX_MESSAGE]; |
86 |
va_list ap; |
87 |
size_t curlen; |
88 |
int i; |
89 |
|
90 |
va_start(ap,format); |
91 |
if (config->verbosity >= PSICONV_VERB_PROGRESS) { |
92 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
93 |
curlen = strlen(buffer); |
94 |
|
95 |
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
96 |
buffer[i+curlen] = '='; |
97 |
curlen += i; |
98 |
|
99 |
buffer[curlen] = '>'; |
100 |
buffer[curlen+1] = ' '; |
101 |
buffer[curlen+2] = '\0'; |
102 |
curlen += 2; |
103 |
|
104 |
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
105 |
|
106 |
psiconv_error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
107 |
} |
108 |
|
109 |
va_end(ap); |
110 |
} |
111 |
|
112 |
|
113 |
void psiconv_debug(psiconv_config config, int level, psiconv_u32 off, |
114 |
const char *format,...) |
115 |
{ |
116 |
char buffer[MAX_MESSAGE]; |
117 |
va_list ap; |
118 |
size_t curlen; |
119 |
int i; |
120 |
|
121 |
va_start(ap,format); |
122 |
if (config->verbosity >= PSICONV_VERB_DEBUG) { |
123 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
124 |
curlen = strlen(buffer); |
125 |
|
126 |
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
127 |
buffer[i+curlen] = '-'; |
128 |
curlen += i; |
129 |
|
130 |
buffer[curlen] = '>'; |
131 |
buffer[curlen+1] = ' '; |
132 |
buffer[curlen+2] = '\0'; |
133 |
curlen += 2; |
134 |
|
135 |
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
136 |
|
137 |
psiconv_error_handler(PSICONV_VERB_DEBUG,off,buffer); |
138 |
} |
139 |
va_end(ap); |
140 |
} |