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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.71  
changed lines
  Added in v.196

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