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