/[public]/psiconv/trunk/lib/psiconv/error.c
ViewVC logotype

Contents of /psiconv/trunk/lib/psiconv/error.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 270 - (show annotations)
Tue Nov 15 15:52:34 2005 UTC (18 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 4552 byte(s)
(Frodo) Updated all copyright notices for 2005
        Changed email addresses
        Made the examples part of the archive

1 /*
2 error.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999-2005 Frodo Looijaard <frodo@frodo.looijaard.name>
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
29 #ifdef DMALLOC
30 #include <dmalloc.h>
31 #endif
32
33 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 1024
40
41 void psiconv_fatal(psiconv_config config, int level, psiconv_u32 off,
42 const char *format,...)
43 {
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 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 va_end(ap);
58
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);
82 }
83
84 void psiconv_warn(psiconv_config config, int level, psiconv_u32 off,
85 const char *format,...)
86 {
87 char buffer[MAX_MESSAGE];
88 va_list ap;
89 size_t curlen;
90
91 va_start(ap,format);
92
93 if (config->verbosity >= PSICONV_VERB_WARN) {
94 snprintf(buffer,MAX_MESSAGE,"WARNING (offset %08x): ",off);
95 curlen = strlen(buffer);
96
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
101 psiconv_default_error_handler(PSICONV_VERB_WARN,off,buffer);
102 }
103 va_end(ap);
104 }
105
106 void psiconv_progress(psiconv_config config,int level, psiconv_u32 off,
107 const char *format,...)
108 {
109 char buffer[MAX_MESSAGE];
110 va_list ap;
111 size_t curlen;
112 int i;
113
114 va_start(ap,format);
115 if (config->verbosity >= PSICONV_VERB_PROGRESS) {
116 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 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 }
135
136 va_end(ap);
137 }
138
139
140 void psiconv_debug(psiconv_config config, int level, psiconv_u32 off,
141 const char *format,...)
142 {
143 char buffer[MAX_MESSAGE];
144 va_list ap;
145 size_t curlen;
146 int i;
147
148 va_start(ap,format);
149 if (config->verbosity >= PSICONV_VERB_DEBUG) {
150 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 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 }
169 va_end(ap);
170 }

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26