1 |
frodo |
62 |
/* |
2 |
|
|
error.c - Part of psiconv, a PSION 5 file formats converter |
3 |
frodo |
196 |
Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl> |
4 |
frodo |
62 |
|
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 |
|
|
|
29 |
frodo |
142 |
#ifdef DMALLOC |
30 |
|
|
#include <dmalloc.h> |
31 |
|
|
#endif |
32 |
|
|
|
33 |
frodo |
62 |
static void psiconv_default_error_handler(int kind, psiconv_u32 off, |
34 |
|
|
const char *message) |
35 |
|
|
{ |
36 |
|
|
fprintf(stderr,"%s\n",message); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
#define MAX_MESSAGE 160 |
40 |
|
|
|
41 |
frodo |
168 |
void psiconv_fatal(psiconv_config config, int level, psiconv_u32 off, |
42 |
|
|
const char *format,...) |
43 |
frodo |
62 |
{ |
44 |
|
|
char buffer[MAX_MESSAGE]; |
45 |
|
|
va_list ap; |
46 |
|
|
size_t curlen; |
47 |
|
|
|
48 |
|
|
va_start(ap,format); |
49 |
|
|
snprintf(buffer,MAX_MESSAGE,"Fatal error (offset %08x): ",off); |
50 |
|
|
curlen = strlen(buffer); |
51 |
|
|
|
52 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
53 |
frodo |
173 |
if (config->error_handler) |
54 |
|
|
config->error_handler(PSICONV_VERB_FATAL,off,buffer); |
55 |
|
|
else |
56 |
|
|
psiconv_default_error_handler(PSICONV_VERB_FATAL,off,buffer); |
57 |
frodo |
62 |
va_end(ap); |
58 |
|
|
|
59 |
|
|
exit(1); |
60 |
|
|
} |
61 |
|
|
|
62 |
frodo |
184 |
void psiconv_error(psiconv_config config, int level, psiconv_u32 off, |
63 |
|
|
const char *format,...) |
64 |
|
|
{ |
65 |
|
|
char buffer[MAX_MESSAGE]; |
66 |
|
|
va_list ap; |
67 |
|
|
size_t curlen; |
68 |
|
|
|
69 |
|
|
va_start(ap,format); |
70 |
|
|
|
71 |
|
|
if (config->verbosity >= PSICONV_VERB_ERROR) { |
72 |
|
|
snprintf(buffer,MAX_MESSAGE,"ERROR (offset %08x): ",off); |
73 |
|
|
curlen = strlen(buffer); |
74 |
|
|
|
75 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
76 |
|
|
if (config->error_handler) |
77 |
|
|
config->error_handler(PSICONV_VERB_ERROR,off,buffer); |
78 |
|
|
else |
79 |
|
|
psiconv_default_error_handler(PSICONV_VERB_ERROR,off,buffer); |
80 |
|
|
} |
81 |
|
|
va_end(ap); |
82 |
|
|
} |
83 |
|
|
|
84 |
frodo |
168 |
void psiconv_warn(psiconv_config config, int level, psiconv_u32 off, |
85 |
|
|
const char *format,...) |
86 |
frodo |
62 |
{ |
87 |
|
|
char buffer[MAX_MESSAGE]; |
88 |
|
|
va_list ap; |
89 |
|
|
size_t curlen; |
90 |
|
|
|
91 |
|
|
va_start(ap,format); |
92 |
|
|
|
93 |
frodo |
168 |
if (config->verbosity >= PSICONV_VERB_WARN) { |
94 |
frodo |
62 |
snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off); |
95 |
|
|
curlen = strlen(buffer); |
96 |
|
|
|
97 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
98 |
frodo |
173 |
if (config->error_handler) |
99 |
|
|
config->error_handler(PSICONV_VERB_WARN,off,buffer); |
100 |
|
|
else |
101 |
|
|
psiconv_default_error_handler(PSICONV_VERB_WARN,off,buffer); |
102 |
frodo |
62 |
} |
103 |
|
|
va_end(ap); |
104 |
|
|
} |
105 |
|
|
|
106 |
frodo |
168 |
void psiconv_progress(psiconv_config config,int level, psiconv_u32 off, |
107 |
|
|
const char *format,...) |
108 |
frodo |
62 |
{ |
109 |
|
|
char buffer[MAX_MESSAGE]; |
110 |
|
|
va_list ap; |
111 |
|
|
size_t curlen; |
112 |
|
|
int i; |
113 |
|
|
|
114 |
|
|
va_start(ap,format); |
115 |
frodo |
168 |
if (config->verbosity >= PSICONV_VERB_PROGRESS) { |
116 |
frodo |
62 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
117 |
|
|
curlen = strlen(buffer); |
118 |
|
|
|
119 |
|
|
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
120 |
|
|
buffer[i+curlen] = '='; |
121 |
|
|
curlen += i; |
122 |
|
|
|
123 |
|
|
buffer[curlen] = '>'; |
124 |
|
|
buffer[curlen+1] = ' '; |
125 |
|
|
buffer[curlen+2] = '\0'; |
126 |
|
|
curlen += 2; |
127 |
|
|
|
128 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
129 |
|
|
|
130 |
frodo |
173 |
if (config->error_handler) |
131 |
|
|
config->error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
132 |
|
|
else |
133 |
|
|
psiconv_default_error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
134 |
frodo |
62 |
} |
135 |
frodo |
65 |
|
136 |
frodo |
62 |
va_end(ap); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
|
140 |
frodo |
168 |
void psiconv_debug(psiconv_config config, int level, psiconv_u32 off, |
141 |
|
|
const char *format,...) |
142 |
frodo |
62 |
{ |
143 |
|
|
char buffer[MAX_MESSAGE]; |
144 |
|
|
va_list ap; |
145 |
|
|
size_t curlen; |
146 |
|
|
int i; |
147 |
|
|
|
148 |
|
|
va_start(ap,format); |
149 |
frodo |
168 |
if (config->verbosity >= PSICONV_VERB_DEBUG) { |
150 |
frodo |
62 |
snprintf(buffer,MAX_MESSAGE,"%08x ",off); |
151 |
|
|
curlen = strlen(buffer); |
152 |
|
|
|
153 |
|
|
for (i = 0; (i < level) && (i+curlen+3 < MAX_MESSAGE); i++) |
154 |
|
|
buffer[i+curlen] = '-'; |
155 |
|
|
curlen += i; |
156 |
|
|
|
157 |
|
|
buffer[curlen] = '>'; |
158 |
|
|
buffer[curlen+1] = ' '; |
159 |
|
|
buffer[curlen+2] = '\0'; |
160 |
|
|
curlen += 2; |
161 |
|
|
|
162 |
|
|
vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
163 |
|
|
|
164 |
frodo |
173 |
if (config->error_handler) |
165 |
|
|
config->error_handler(PSICONV_VERB_DEBUG,off,buffer); |
166 |
|
|
else |
167 |
|
|
psiconv_default_error_handler(PSICONV_VERB_DEBUG,off,buffer); |
168 |
frodo |
62 |
} |
169 |
|
|
va_end(ap); |
170 |
|
|
} |