1 | /* |
1 | /* |
2 | error.c - Part of psiconv, a PSION 5 file formats converter |
2 | error.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
23 | #include <stdio.h> |
23 | #include <stdio.h> |
24 | #include <stdlib.h> |
24 | #include <stdlib.h> |
25 | #include <string.h> |
25 | #include <string.h> |
26 | |
26 | |
27 | #include "error.h" |
27 | #include "error.h" |
28 | #include "config.h" |
|
|
29 | |
28 | |
30 | #ifdef DMALLOC |
29 | #ifdef DMALLOC |
31 | #include <dmalloc.h> |
30 | #include <dmalloc.h> |
32 | #endif |
31 | #endif |
33 | |
32 | |
… | |
… | |
35 | const char *message) |
34 | const char *message) |
36 | { |
35 | { |
37 | fprintf(stderr,"%s\n",message); |
36 | fprintf(stderr,"%s\n",message); |
38 | } |
37 | } |
39 | |
38 | |
40 | psiconv_error_handler_t psiconv_error_handler = psiconv_default_error_handler; |
|
|
41 | |
|
|
42 | |
|
|
43 | #define MAX_MESSAGE 160 |
39 | #define MAX_MESSAGE 1024 |
44 | |
40 | |
45 | void psiconv_fatal(psiconv_config config, int level, psiconv_u32 off, |
41 | void psiconv_fatal(psiconv_config config, int level, psiconv_u32 off, |
46 | const char *format,...) |
42 | const char *format,...) |
47 | { |
43 | { |
48 | char buffer[MAX_MESSAGE]; |
44 | char buffer[MAX_MESSAGE]; |
… | |
… | |
52 | va_start(ap,format); |
48 | va_start(ap,format); |
53 | snprintf(buffer,MAX_MESSAGE,"Fatal error (offset %08x): ",off); |
49 | snprintf(buffer,MAX_MESSAGE,"Fatal error (offset %08x): ",off); |
54 | curlen = strlen(buffer); |
50 | curlen = strlen(buffer); |
55 | |
51 | |
56 | vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
52 | vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
|
|
53 | if (config->error_handler) |
|
|
54 | config->error_handler(PSICONV_VERB_FATAL,off,buffer); |
|
|
55 | else |
57 | psiconv_error_handler(PSICONV_VERB_FATAL,off,buffer); |
56 | psiconv_default_error_handler(PSICONV_VERB_FATAL,off,buffer); |
58 | va_end(ap); |
57 | va_end(ap); |
59 | |
58 | |
60 | exit(1); |
59 | exit(1); |
|
|
60 | } |
|
|
61 | |
|
|
62 | 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); |
61 | } |
82 | } |
62 | |
83 | |
63 | void psiconv_warn(psiconv_config config, int level, psiconv_u32 off, |
84 | void psiconv_warn(psiconv_config config, int level, psiconv_u32 off, |
64 | const char *format,...) |
85 | const char *format,...) |
65 | { |
86 | { |
… | |
… | |
72 | if (config->verbosity >= PSICONV_VERB_WARN) { |
93 | if (config->verbosity >= PSICONV_VERB_WARN) { |
73 | snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off); |
94 | snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off); |
74 | curlen = strlen(buffer); |
95 | curlen = strlen(buffer); |
75 | |
96 | |
76 | vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
97 | vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
|
|
98 | if (config->error_handler) |
|
|
99 | config->error_handler(PSICONV_VERB_WARN,off,buffer); |
|
|
100 | else |
77 | psiconv_error_handler(PSICONV_VERB_WARN,off,buffer); |
101 | psiconv_default_error_handler(PSICONV_VERB_WARN,off,buffer); |
78 | } |
102 | } |
79 | va_end(ap); |
103 | va_end(ap); |
80 | } |
104 | } |
81 | |
105 | |
82 | void psiconv_progress(psiconv_config config,int level, psiconv_u32 off, |
106 | void psiconv_progress(psiconv_config config,int level, psiconv_u32 off, |
… | |
… | |
101 | buffer[curlen+2] = '\0'; |
125 | buffer[curlen+2] = '\0'; |
102 | curlen += 2; |
126 | curlen += 2; |
103 | |
127 | |
104 | vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
128 | vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
105 | |
129 | |
|
|
130 | if (config->error_handler) |
|
|
131 | config->error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
|
|
132 | else |
106 | psiconv_error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
133 | psiconv_default_error_handler(PSICONV_VERB_PROGRESS,off,buffer); |
107 | } |
134 | } |
108 | |
135 | |
109 | va_end(ap); |
136 | va_end(ap); |
110 | } |
137 | } |
111 | |
138 | |
… | |
… | |
132 | buffer[curlen+2] = '\0'; |
159 | buffer[curlen+2] = '\0'; |
133 | curlen += 2; |
160 | curlen += 2; |
134 | |
161 | |
135 | vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
162 | vsnprintf(buffer+curlen,MAX_MESSAGE-curlen,format,ap); |
136 | |
163 | |
|
|
164 | if (config->error_handler) |
|
|
165 | config->error_handler(PSICONV_VERB_DEBUG,off,buffer); |
|
|
166 | else |
137 | psiconv_error_handler(PSICONV_VERB_DEBUG,off,buffer); |
167 | psiconv_default_error_handler(PSICONV_VERB_DEBUG,off,buffer); |
138 | } |
168 | } |
139 | va_end(ap); |
169 | va_end(ap); |
140 | } |
170 | } |