Wed, 28 May 2003 23:48:16 +0200
trunk: changeset 57
License changed to Artistic/LGPL dual license.
0 | 1 | /* |
2 | * libtu/output.c | |
3 | * | |
36 | 4 | * Copyright (c) Tuomo Valkonen 1999-2002. |
53 | 5 | * |
6 | * You may distribute and modify this library under the terms of either | |
7 | * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. | |
0 | 8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <errno.h> | |
13 | #include <strings.h> | |
14 | #include <string.h> | |
15 | ||
5 | 16 | #include <libtu/misc.h> |
17 | #include <libtu/output.h> | |
18 | #include <libtu/util.h> | |
0 | 19 | |
35 | 20 | #if !defined(HAS_SYSTEM_ASPRINTF) |
25 | 21 | #include "../snprintf_2.2/snprintf.h" |
22 | #endif | |
23 | ||
0 | 24 | |
52 | 25 | static void default_warn_handler(const char *message); |
26 | ||
0 | 27 | |
28 | static bool verbose_mode=FALSE; | |
29 | static int verbose_indent_lvl=0; | |
30 | static bool progname_enable=TRUE; | |
52 | 31 | static WarnHandler *current_warn_handler=default_warn_handler; |
0 | 32 | |
33 | #define INDENTATOR_LENGTH 4 | |
34 | ||
35 | static char indentator[]={' ', ' ', ' ', ' '}; | |
36 | ||
35 | 37 | static void do_dispatch_message(const char *message); |
38 | ||
0 | 39 | |
40 | void verbose(const char *p, ...) | |
41 | { | |
42 | va_list args; | |
43 | ||
44 | va_start(args, p); | |
45 | ||
46 | verbose_v(p, args); | |
47 | ||
48 | va_end(args); | |
49 | } | |
50 | ||
51 | ||
52 | void verbose_v(const char *p, va_list args) | |
53 | { | |
54 | int i; | |
55 | ||
56 | if(verbose_mode){ | |
57 | for(i=0; i<verbose_indent_lvl; i++) | |
58 | writef(stdout, indentator, INDENTATOR_LENGTH); | |
59 | ||
60 | vprintf(p, args); | |
61 | fflush(stdout); | |
62 | } | |
63 | } | |
64 | ||
65 | ||
66 | void verbose_enable(bool enable) | |
67 | { | |
68 | verbose_mode=enable; | |
69 | } | |
70 | ||
71 | ||
72 | int verbose_indent(int depth) | |
73 | { | |
74 | int old=verbose_indent_lvl; | |
75 | ||
76 | if(depth>=0) | |
77 | verbose_indent_lvl=depth; | |
78 | ||
79 | return old; | |
80 | } | |
81 | ||
82 | ||
83 | void warn_progname_enable(bool enable) | |
84 | { | |
85 | progname_enable=enable; | |
86 | } | |
87 | ||
88 | ||
89 | static void put_prog_name() | |
90 | { | |
91 | const char*progname; | |
92 | ||
93 | if(!progname_enable) | |
94 | return; | |
95 | ||
96 | progname=prog_execname(); | |
97 | ||
98 | if(progname==NULL) | |
99 | return; | |
100 | ||
101 | fprintf(stderr, "%s: ", (char*)progname); | |
102 | } | |
103 | ||
27 | 104 | /* warn |
105 | */ | |
0 | 106 | |
35 | 107 | |
108 | static void fallback_warn() | |
109 | { | |
110 | put_prog_name(); | |
111 | fprintf(stderr, "Oops. Error string compilation failed: %s", | |
112 | strerror(errno)); | |
113 | } | |
114 | ||
115 | ||
25 | 116 | #define CALL_V(NAME, ARGS) \ |
35 | 117 | do { va_list args; va_start(args, p); NAME ARGS; va_end(args); } while(0) |
27 | 118 | |
35 | 119 | #define DO_DISPATCH(NAME, ARGS) \ |
120 | do{ \ | |
121 | char *msg; \ | |
122 | if((msg=NAME ARGS)!=NULL){ \ | |
123 | do_dispatch_message(msg); \ | |
124 | free(msg);\ | |
125 | }else{ \ | |
126 | fallback_warn(); \ | |
127 | } \ | |
128 | }while(0) | |
25 | 129 | |
33 | 130 | |
31 | 131 | void libtu_asprintf(char **ret, const char *p, ...) |
132 | { | |
133 | CALL_V(vasprintf, (ret, p, args)); | |
134 | } | |
135 | ||
136 | ||
137 | void libtu_vasprintf(char **ret, const char *p, va_list args) | |
138 | { | |
139 | vasprintf(ret, p, args); | |
140 | } | |
141 | ||
33 | 142 | |
0 | 143 | void warn(const char *p, ...) |
144 | { | |
25 | 145 | CALL_V(warn_v, (p, args)); |
146 | } | |
147 | ||
148 | ||
149 | void warn_obj(const char *obj, const char *p, ...) | |
150 | { | |
151 | CALL_V(warn_obj_v, (obj, p, args)); | |
152 | } | |
153 | ||
154 | ||
155 | void warn_obj_line(const char *obj, int line, const char *p, ...) | |
156 | { | |
157 | CALL_V(warn_obj_line_v, (obj, line, p, args)); | |
158 | } | |
159 | ||
160 | ||
161 | void warn_obj_v(const char *obj, const char *p, va_list args) | |
162 | { | |
163 | warn_obj_line_v(obj, -1, p, args); | |
164 | } | |
165 | ||
166 | ||
0 | 167 | void warn_v(const char *p, va_list args) |
168 | { | |
35 | 169 | DO_DISPATCH(errmsg_v, (p, args)); |
0 | 170 | } |
171 | ||
172 | ||
173 | void warn_obj_line_v(const char *obj, int line, const char *p, va_list args) | |
174 | { | |
35 | 175 | DO_DISPATCH(errmsg_obj_line_v, (obj, line, p, args)); |
0 | 176 | } |
177 | ||
178 | ||
27 | 179 | void warn_err() |
180 | { | |
35 | 181 | DO_DISPATCH(errmsg_err, ()); |
27 | 182 | } |
183 | ||
184 | ||
185 | void warn_err_obj(const char *obj) | |
186 | { | |
35 | 187 | DO_DISPATCH(errmsg_err_obj, (obj)); |
27 | 188 | } |
189 | ||
190 | void warn_err_obj_line(const char *obj, int line) | |
191 | { | |
35 | 192 | DO_DISPATCH(errmsg_err_obj_line, (obj, line)); |
27 | 193 | } |
194 | ||
195 | ||
196 | /* errmsg | |
197 | */ | |
198 | ||
199 | #define CALL_V_RET(NAME, ARGS) \ | |
35 | 200 | char *ret; va_list args; va_start(args, p); ret=NAME ARGS; \ |
201 | va_end(args); return ret; | |
27 | 202 | |
203 | ||
204 | char* errmsg(const char *p, ...) | |
205 | { | |
206 | CALL_V_RET(errmsg_v, (p, args)); | |
207 | } | |
208 | ||
209 | ||
210 | char *errmsg_obj(const char *obj, const char *p, ...) | |
211 | { | |
212 | CALL_V_RET(errmsg_obj_v, (obj, p, args)); | |
213 | } | |
214 | ||
215 | ||
216 | char *errmsg_obj_line(const char *obj, int line, const char *p, ...) | |
217 | { | |
218 | CALL_V_RET(errmsg_obj_line_v, (obj, line, p, args)); | |
219 | } | |
220 | ||
221 | ||
222 | char* errmsg_obj_v(const char *obj, const char *p, va_list args) | |
223 | { | |
224 | return errmsg_obj_line_v(obj, -1, p, args); | |
225 | } | |
226 | ||
227 | ||
228 | char *errmsg_v(const char *p, va_list args) | |
229 | { | |
230 | char *res; | |
231 | vasprintf(&res, p, args); | |
232 | return res; | |
233 | } | |
234 | ||
235 | ||
25 | 236 | char *errmsg_obj_line_v(const char *obj, int line, const char *p, va_list args) |
237 | { | |
238 | char *res1=NULL, *res2, *res3; | |
35 | 239 | |
25 | 240 | if(obj!=NULL){ |
241 | if(line>0) | |
242 | asprintf(&res1, TR("%s:%d: "), obj, line); | |
243 | else | |
244 | asprintf(&res1, "%s: ", obj); | |
245 | }else{ | |
246 | if(line>0) | |
247 | asprintf(&res1, TR("%d: "), line); | |
248 | } | |
35 | 249 | vasprintf(&res2, p, args); |
25 | 250 | if(res1!=NULL){ |
251 | if(res2==NULL) | |
252 | return NULL; | |
253 | res3=scat(res1, res2); | |
254 | free(res1); | |
255 | free(res2); | |
256 | return res3; | |
257 | } | |
258 | return res2; | |
259 | } | |
260 | ||
261 | ||
262 | char *errmsg_err() | |
263 | { | |
264 | char *res; | |
265 | asprintf(&res, "%s\n", strerror(errno)); | |
266 | return res; | |
267 | } | |
268 | ||
269 | ||
270 | char *errmsg_err_obj(const char *obj) | |
271 | { | |
272 | char *res; | |
273 | if(obj!=NULL) | |
274 | asprintf(&res, "%s: %s\n", obj, strerror(errno)); | |
275 | else | |
276 | asprintf(&res, "%s\n", strerror(errno)); | |
277 | return res; | |
0 | 278 | } |
279 | ||
280 | ||
25 | 281 | char *errmsg_err_obj_line(const char *obj, int line) |
282 | { | |
283 | char *res; | |
284 | if(obj!=NULL){ | |
285 | if(line>0) | |
286 | asprintf(&res, TR("%s:%d: %s\n"), obj, line, strerror(errno)); | |
287 | else | |
288 | asprintf(&res, "%s: %s\n", obj, strerror(errno)); | |
289 | }else{ | |
290 | if(line>0) | |
291 | asprintf(&res, TR("%d: %s\n"), line, strerror(errno)); | |
292 | else | |
293 | asprintf(&res, TR("%s\n"), strerror(errno)); | |
294 | } | |
295 | return res; | |
296 | } | |
31 | 297 | |
25 | 298 | |
0 | 299 | /* die |
300 | */ | |
301 | ||
35 | 302 | |
0 | 303 | void die(const char *p, ...) |
304 | { | |
35 | 305 | set_warn_handler(NULL); |
25 | 306 | CALL_V(die_v, (p, args)); |
0 | 307 | } |
308 | ||
309 | ||
310 | void die_v(const char *p, va_list args) | |
311 | { | |
35 | 312 | set_warn_handler(NULL); |
0 | 313 | warn_v(p, args); |
314 | exit(EXIT_FAILURE); | |
315 | } | |
316 | ||
317 | ||
318 | void die_obj(const char *obj, const char *p, ...) | |
319 | { | |
35 | 320 | set_warn_handler(NULL); |
25 | 321 | CALL_V(die_obj_v, (obj, p, args)); |
0 | 322 | } |
323 | ||
324 | ||
325 | void die_obj_line(const char *obj, int line, const char *p, ...) | |
326 | { | |
35 | 327 | set_warn_handler(NULL); |
25 | 328 | CALL_V(die_obj_line_v, (obj, line, p, args)); |
0 | 329 | } |
330 | ||
331 | ||
332 | void die_obj_v(const char *obj, const char *p, va_list args) | |
333 | { | |
35 | 334 | set_warn_handler(NULL); |
0 | 335 | warn_obj_v(obj, p, args); |
336 | exit(EXIT_FAILURE); | |
337 | } | |
338 | ||
339 | ||
340 | void die_obj_line_v(const char *obj, int line, const char *p, va_list args) | |
341 | { | |
35 | 342 | set_warn_handler(NULL); |
0 | 343 | warn_obj_line_v(obj, line, p, args); |
344 | exit(EXIT_FAILURE); | |
345 | } | |
346 | ||
347 | ||
348 | void die_err() | |
349 | { | |
35 | 350 | set_warn_handler(NULL); |
0 | 351 | warn_err(); |
352 | exit(EXIT_FAILURE); | |
353 | } | |
354 | ||
355 | ||
356 | void die_err_obj(const char *obj) | |
357 | { | |
35 | 358 | set_warn_handler(NULL); |
0 | 359 | warn_err_obj(obj); |
360 | exit(EXIT_FAILURE); | |
361 | } | |
362 | ||
363 | ||
364 | void die_err_obj_line(const char *obj, int line) | |
365 | { | |
35 | 366 | set_warn_handler(NULL); |
0 | 367 | warn_err_obj_line(obj, line); |
368 | exit(EXIT_FAILURE); | |
369 | } | |
35 | 370 | |
371 | ||
372 | static void default_warn_handler(const char *message) | |
373 | { | |
374 | put_prog_name(); | |
375 | fprintf(stderr, "%s", message); | |
376 | putc('\n', stderr); | |
377 | } | |
378 | ||
379 | ||
380 | static void do_dispatch_message(const char *message) | |
381 | { | |
382 | if(current_warn_handler!=NULL) | |
383 | current_warn_handler(message); | |
384 | else | |
385 | default_warn_handler(message); | |
386 | } | |
387 | ||
388 | ||
389 | WarnHandler *set_warn_handler(WarnHandler *handler) | |
390 | { | |
391 | WarnHandler *old=current_warn_handler; | |
52 | 392 | current_warn_handler=(handler!=NULL ? handler : default_warn_handler); |
35 | 393 | return old; |
394 | } |