1 |
frodo |
62 |
/* |
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 |
frodo |
71 |
#include "compat.h" |
22 |
frodo |
62 |
#include <stdarg.h> |
23 |
|
|
#include <stdio.h> |
24 |
|
|
#include <stdlib.h> |
25 |
frodo |
168 |
#include <string.h> |
26 |
frodo |
62 |
|
27 |
|
|
#include "error.h" |
28 |
frodo |
168 |
#include "config.h" |
29 |
frodo |
62 |
|
30 |
frodo |
142 |
#ifdef DMALLOC |
31 |
|
|
#include <dmalloc.h> |
32 |
|
|
#endif |
33 |
|
|
|
34 |
frodo |
62 |
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 |
|
|
#define MAX_MESSAGE 160 |
41 |
|
|
|
42 |
frodo |
168 |
void psiconv_fatal(psiconv_config config, int level, psiconv_u32 off, |
43 |
|
|
const char *format,...) |
44 |
frodo |
62 |
{ |
45 |
|
|
char buffer[MAX_MESSAGE]; |
46 |
|
|
va_list ap; |
47 |
|
|
size_t curlen; |
48 |
|
|
|
49 |
|
|
va_start(ap,format); |
50 |
|
|
snprintf(buffer,MAX_MESSAGE,"Fatal error (offset %08x): ",off); |
51 |
|
|
curlen = strlen(buffer); |
52 |
|
|
|
53 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
54 |
frodo |
173 |
if (config->error_handler) |
55 |
|
|
config->error_handler(PSICONV_VERB_FATAL,off,buffer); |
56 |
|
|
else |
57 |
|
|
psiconv_default_error_handler(PSICONV_VERB_FATAL,off,buffer); |
58 |
frodo |
62 |
va_end(ap); |
59 |
|
|
|
60 |
|
|
exit(1); |
61 |
|
|
} |
62 |
|
|
|
63 |
frodo |
184 |
void psiconv_error(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_ERROR) { |
73 |
|
|
snprintf(buffer,MAX_MESSAGE,"ERROR (offset %08x): ",off); |
74 |
|
|
curlen = strlen(buffer); |
75 |
|
|
|
76 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
77 |
|
|
if (config->error_handler) |
78 |
|
|
config->error_handler(PSICONV_VERB_ERROR,off,buffer); |
79 |
|
|
else |
80 |
|
|
psiconv_default_error_handler(PSICONV_VERB_ERROR,off,buffer); |
81 |
|
|
} |
82 |
|
|
va_end(ap); |
83 |
|
|
} |
84 |
|
|
|
85 |
frodo |
168 |
void psiconv_warn(psiconv_config config, int level, psiconv_u32 off, |
86 |
|
|
const char *format,...) |
87 |
frodo |
62 |
{ |
88 |
|
|
char buffer[MAX_MESSAGE]; |
89 |
|
|
va_list ap; |
90 |
|
|
size_t curlen; |
91 |
|
|
|
92 |
|
|
va_start(ap,format); |
93 |
|
|
|
94 |
frodo |
168 |
if (config->verbosity >= PSICONV_VERB_WARN) { |
95 |
frodo |
62 |
snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off); |
96 |
|
|
curlen = strlen(buffer); |
97 |
|
|
|
98 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
99 |
frodo |
173 |
if (config->error_handler) |
100 |
|
|
config->error_handler(PSICONV_VERB_WARN,off,buffer); |
101 |
|
|
else |
102 |
|
|
psiconv_default_error_handler(PSICONV_VERB_WARN,off,buffer); |
103 |
frodo |
62 |
} |
104 |
|
|
va_end(ap); |
105 |
|
|
} |
106 |
|
|
|
107 |
frodo |
168 |
void psiconv_progress(psiconv_config config,int level, psiconv_u32 off, |
108 |
|
|
const char *format,...) |
109 |
frodo |
62 |
{ |
110 |
|
|
char buffer[MAX_MESSAGE]; |
111 |
|
|
va_list ap; |
112 |
|
|
size_t curlen; |
113 |
|
|
int i; |
114 |
|
|
|
115 |
|
|
va_start(ap,format); |
116 |
frodo |
168 |
if (config->verbosity >= PSICONV_VERB_PROGRESS) { |
117 |
frodo |
62 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
118 |
|
|
curlen = strlen(buffer); |
119 |
|
|
|
120 |
|
|
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
121 |
|
|
buffer[i+curlen] = '='; |
122 |
|
|
curlen += i; |
123 |
|
|
|
124 |
|
|
buffer[curlen] = '>'; |
125 |
|
|
buffer[curlen+1] = ' '; |
126 |
|
|
buffer[curlen+2] = '\0'; |
127 |
|
|
curlen += 2; |
128 |
|
|
|
129 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
130 |
|
|
|
131 |
frodo |
173 |
if (config->error_handler) |
132 |
|
|
config->error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
133 |
|
|
else |
134 |
|
|
psiconv_default_error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
135 |
frodo |
62 |
} |
136 |
frodo |
65 |
|
137 |
frodo |
62 |
va_end(ap); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
|
141 |
frodo |
168 |
void psiconv_debug(psiconv_config config, int level, psiconv_u32 off, |
142 |
|
|
const char *format,...) |
143 |
frodo |
62 |
{ |
144 |
|
|
char buffer[MAX_MESSAGE]; |
145 |
|
|
va_list ap; |
146 |
|
|
size_t curlen; |
147 |
|
|
int i; |
148 |
|
|
|
149 |
|
|
va_start(ap,format); |
150 |
frodo |
168 |
if (config->verbosity >= PSICONV_VERB_DEBUG) { |
151 |
frodo |
62 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
152 |
|
|
curlen = strlen(buffer); |
153 |
|
|
|
154 |
|
|
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
155 |
|
|
buffer[i+curlen] = '-'; |
156 |
|
|
curlen += i; |
157 |
|
|
|
158 |
|
|
buffer[curlen] = '>'; |
159 |
|
|
buffer[curlen+1] = ' '; |
160 |
|
|
buffer[curlen+2] = '\0'; |
161 |
|
|
curlen += 2; |
162 |
|
|
|
163 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
164 |
|
|
|
165 |
frodo |
173 |
if (config->error_handler) |
166 |
|
|
config->error_handler(PSICONV_VERB_DEBUG,off,buffer); |
167 |
|
|
else |
168 |
|
|
psiconv_default_error_handler(PSICONV_VERB_DEBUG,off,buffer); |
169 |
frodo |
62 |
} |
170 |
|
|
va_end(ap); |
171 |
|
|
} |