include/error/printf.h
1
2
3 #ifndef HT_ERROR_EPRINTF_H
4 #define HT_ERROR_EPRINTF_H
5
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <time.h>
9
10 #ifdef IN_LIBERROR_COMPILING
11 # include "error/erwin/defs.h"
12 # include "error/erwin/forwards.h"
13 #else
14 # include <error/erwin/defs.h>
15 # include <error/erwin/forwards.h>
16 #endif
17
18
19
20
21
22
23
24
25 #ifdef ERWIN_UNSIGNED_LONG_LONG
26 typedef ERWIN_UNSIGNED_LONG_LONG err_address_t;
27 #define ERR_FORMAT_ADDRESS "0x%Lx"
28 #else
29 typedef unsigned long err_address_t;
30 #define ERR_FORMAT_ADDRESS "0x%lx"
31 #endif
32
33
34
35 struct err_ext_address_t;
36 #ifdef __cplusplus
37 typedef struct err_ext_address_t ErrExtAddress;
38 #else
39 typedef struct err_ext_address_t err_ext_address_t;
40 #endif
41
42 enum err_preposition_t {
43 ERR_PREP_NONE,
44 ERR_PREP_CAPITAL_AT,
45 ERR_PREP_CAPITAL_FROM,
46 ERR_PREP_CAPITAL_TO,
47 ERR_PREP_LOWER_AT,
48 ERR_PREP_LOWER_FROM,
49 ERR_PREP_LOWER_TO
50 };
51 #ifdef __cplusplus
52 typedef enum err_preposition_t ErrPreposition;
53 #else
54 typedef enum err_preposition_t err_preposition_t;
55 #endif
56
57 enum err_format_style_t {
58 ERR_FS_HUMAN,
59
60 ERR_FS_ADDRESS,
61 ERR_FS_LOCATION_ID,
62 ERR_FS_ROUTINE,
63 ERR_FS_ROUTINE_ID
64 };
65 #ifdef __cplusplus
66 typedef enum err_format_style_t ErrFormatStyle;
67 #else
68 typedef enum err_format_style_t err_format_style_t;
69 #endif
70
71 struct err_position_t;
72 #ifdef __cplusplus
73 typedef struct err_position_t ErrPosition;
74 #else
75 typedef struct err_position_t err_position_t;
76 #endif
77
78 struct err_ext_address_class_t {
79 void (* destruct) (err_ext_address_t *);
80
81 err_ext_address_t * (* clone) (err_ext_address_t const *);
82
83 void (* format) (err_v_char_t *,
84 err_ext_address_t const *,
85 err_preposition_t);
86
87 err_address_t (* to_linear) (err_ext_address_t const *);
88
89
90 void (* resolve) (int ,
91 int ,
92 int ,
93 err_position_t * );
94
95 void (* format_ext) (err_v_char_t *,
96 err_ext_address_t const *,
97 err_preposition_t,
98 err_format_style_t);
99
100
101
102 };
103
104 #ifdef __cplusplus
105 typedef struct err_ext_address_class_t ErrExtAddressClass;
106 #else
107 typedef struct err_ext_address_class_t err_ext_address_class_t;
108 #endif
109
110 struct err_ext_address_t {
111 err_ext_address_class_t const *klass;
112 #ifdef __cplusplus
113 err_ext_address_t (err_ext_address_class_t const *a):
114 klass (a)
115 {}
116
117
118 void destruct ()
119 { if (this) klass->destruct (this); }
120
121 err_ext_address_t *clone() const
122 { return klass->clone (this); }
123
124 void format (err_v_char_t *v,
125 ErrPreposition p = ERR_PREP_NONE,
126 ErrFormatStyle s = ERR_FS_HUMAN) const
127 {
128 if (this) {
129 if (klass->format_ext)
130 klass->format_ext (v, this, p, s);
131 else
132 if (s == ERR_FS_HUMAN)
133 klass->format (v, this, p);
134
135 }
136 }
137
138 ErrVChar to_vchar(ErrPreposition p = ERR_PREP_NONE) const;
139
140 err_address_t to_linear (void) const
141 { return klass->to_linear (this); }
142
143 void resolve (int tag, int orig_tag, int number, err_position_t *pos) const
144 { if (this && klass->resolve)
145 klass->resolve (tag, orig_tag, number, pos);
146 }
147 #endif
148 };
149
150 #ifdef __cplusplus
151 extern "C" {
152 #endif
153
154
155 extern err_ext_address_class_t const err_normal_address_class_s;
156 #define err_normal_address_class (&err_normal_address_class_s)
157
158 #ifdef __cplusplus
159 }
160 #endif
161
162 struct err_normal_address_t {
163 err_ext_address_t super;
164 err_address_t address;
165 #ifdef __cplusplus
166 err_normal_address_t (err_address_t a):
167 super (err_normal_address_class),
168 address (a)
169 {}
170 #endif
171 };
172 #ifdef __cplusplus
173 typedef struct err_normal_address_t ErrNormalAddress;
174 #else
175 typedef struct err_normal_address_t err_normal_address_t;
176 #endif
177
178
179
180
181 struct err_area_t;
182 #ifdef __cplusplus
183 typedef struct err_area_t ErrArea;
184 #else
185 typedef struct err_area_t err_area_t;
186 #endif
187
188 struct err_location_t;
189 #ifdef __cplusplus
190 typedef struct err_location_t ErrLocation;
191 #else
192 typedef struct err_location_t err_location_t;
193 #endif
194
195
196
197 struct err_position_t {
198 int *refcount;
199 char *file;
200 err_ext_address_t *address;
201 int line;
202 int pos;
203 #ifdef __cplusplus
204 private:
205 void release ();
206 void import (char const *, int, int, ErrExtAddress *);
207 void import (int *, char *, int, int, ErrExtAddress *);
208 public:
209 err_position_t (char const * = NULL, int = 0, int = 0);
210 err_position_t (err_address_t);
211 err_position_t (err_ext_address_t const &);
212 err_position_t (err_ext_address_t const *);
213
214 void set_position (char const * = NULL, int = 0, int = 0);
215
216 err_position_t (err_position_t const &);
217
218 void operator= (err_position_t const &);
219 void operator= (err_area_t const &);
220 void operator= (err_location_t const &);
221
222 ~err_position_t ();
223
224 bool valid() const { return file != NULL || address != NULL; }
225
226 operator err_position_t const *() const { return this; }
227
228 operator err_area_t const *() const;
229 operator err_area_t () const;
230
231 operator err_location_t const *() const;
232 operator err_location_t () const;
233 #endif
234 };
235
236 struct err_area_t {
237 err_position_t first;
238 err_position_t last;
239 #ifdef __cplusplus
240 err_area_t (err_position_t const &);
241 err_area_t (err_address_t);
242 err_area_t (err_ext_address_t const &);
243 err_area_t (err_ext_address_t const *);
244 err_area_t (char const * = NULL, int = 0, int = 0);
245
246
247
248 err_area_t (err_position_t const &, err_position_t const &);
249
250
251 err_area_t &upto (char const *a= NULL, int b= 0, int c= 0)
252 {
253 last= ErrPosition (a, b, c);
254 return *this;
255 }
256
257 err_area_t &upto (err_address_t a)
258 {
259 last= ErrPosition (a);
260 return *this;
261 }
262
263 err_area_t (err_area_t const &);
264 void operator= (err_position_t const &);
265 void operator= (err_area_t const &);
266 void operator= (err_location_t const &);
267 ~err_area_t ();
268
269 operator err_position_t const *() const { return &first; }
270 operator err_position_t const &() const { return first; }
271
272 operator err_area_t const *() const { return this; }
273
274 operator err_location_t const *() const;
275 operator err_location_t () const;
276
277 bool valid() const { return first.valid(); }
278 #endif
279 };
280
281 typedef void (*ErrLocationPrinter)(err_v_char_t *, err_location_t const *);
282 typedef ErrLocationPrinter err_location_printer_t;
283
284 struct err_location_t {
285 err_area_t here;
286 err_area_t orig;
287
288
289
290
291
292
293 err_location_t *next_loc;
294
295 ErrLocationPrinter print_next;
296
297
298
299
300
301
302
303
304 #ifdef __cplusplus
305
306 err_location_t (err_area_t const &);
307 err_location_t (err_position_t const &);
308 err_location_t (err_address_t);
309 err_location_t (err_ext_address_t const &);
310 err_location_t (err_ext_address_t const *);
311 err_location_t (char const * = NULL, int = 0, int = 0);
312
313
314
315
316 err_location_t (err_position_t const &, err_position_t const &);
317
318
319 err_location_t (err_area_t const &, err_area_t const &);
320
321
322
323
324
325
326
327
328
329
330
331 err_area_t &free_upto()
332 {
333 return here.last.valid() ? orig : here;
334 }
335
336 err_position_t &free_orig()
337 {
338 return orig.first.valid() ? orig.last : orig.first;
339 }
340
341 err_location_t &upto (char const *a, int b= 0, int c= 0)
342 {
343 free_upto().last= ErrPosition (a, b, c);
344 return *this;
345 }
346
347
348
349 err_location_t &upto (int a, int b)
350 {
351 free_upto().last= ErrPosition (free_upto().first.file, a, b);
352 return *this;
353 }
354
355 err_location_t &upto (err_address_t a)
356 {
357 free_upto().last= ErrPosition (a);
358 return *this;
359 }
360
361 err_location_t &originally (char const *a, int b= 0, int c= 0)
362 {
363 free_orig()= ErrPosition (a, b, c);
364 return *this;
365 }
366
367 err_location_t &originally (int a, int b)
368 {
369 free_orig()= ErrPosition (orig.first.file, a, b);
370 return *this;
371 }
372
373 err_location_t &originally (err_address_t a)
374 {
375 free_orig()= ErrPosition (a);
376 return *this;
377 }
378
379 err_location_t &set_next (
380 err_location_t const *,
381 ErrLocationPrinter);
382
383
384 err_location_t (err_location_t const &);
385
386 void operator= (err_position_t const &);
387 void operator= (err_area_t const &);
388 void operator= (err_location_t const &);
389
390 ~err_location_t ();
391
392 operator err_position_t const *() const { return &here.first; }
393 operator err_position_t const &() const { return here.first; }
394
395 operator err_area_t const *() const { return &here; }
396 operator err_area_t const &() const { return here; }
397
398 operator err_location_t const *() const { return this; }
399
400 bool valid() const { return here.valid(); }
401 #endif
402 };
403
404 struct err_msg_info_t;
405 #ifdef __cplusplus
406 typedef struct err_msg_info_t ErrMsgInfo;
407 #else
408 typedef struct err_msg_info_t err_msg_info_t;
409 #endif
410
411 struct err_msg_info_t {
412 time_t issue_time;
413 #ifdef __cplusplus
414 err_msg_info_t ():
415 issue_time (time(NULL))
416 {}
417 #endif
418 };
419
420
421
422 #ifdef __cplusplus
423 extern "C" {
424 #endif
425
426
427 extern void err_ext_address_init (err_ext_address_t *, err_ext_address_class_t const *);
428
429
430 extern void err_normal_address_destruct (err_ext_address_t *);
431 extern err_ext_address_t *err_normal_address_clone (err_ext_address_t const *);
432 extern void err_normal_address_format (
433 err_v_char_t *, err_ext_address_t const *, err_preposition_t);
434 extern void err_normal_address_format_ext (
435 err_v_char_t *, err_ext_address_t const *, err_preposition_t, err_format_style_t);
436 extern err_address_t err_normal_address_to_linear (err_ext_address_t const *);
437 extern void err_normal_address_format_preposition (err_v_char_t *, err_preposition_t);
438
439
440
441
442
443
444
445
446 extern err_location_t *err_in_file (char const *);
447 extern err_location_t *err_in_line (char const *, int);
448 extern err_location_t *err_at_position (char const *, int, int);
449 extern err_location_t *err_at_address (err_address_t);
450 extern err_location_t *err_at_ext_address (err_ext_address_t const *);
451
452
453
454
455 extern err_location_t *err_location_copy (err_location_t const *) ATTR_MALLOC;
456 extern void err_location_delete (err_location_t *);
457
458 extern err_location_t *err_upto_file (err_location_t *, char const *);
459 extern err_location_t *err_upto_line (err_location_t *, char const *, int);
460 extern err_location_t *err_upto_position (err_location_t *, char const *, int, int);
461 extern err_location_t *err_upto_address (err_location_t *, err_address_t);
462 extern err_location_t *err_upto_ext_address (err_location_t *, err_ext_address_t const *);
463
464
465 extern err_location_t *err_originally_in_file (err_location_t *, err_location_t *, char const *);
466 extern err_location_t *err_originally_in_line (err_location_t *, char const *, int);
467 extern err_location_t *err_originally_at_position (err_location_t *, char const *, int, int);
468 extern err_location_t *err_originally_at_address (err_location_t *, err_address_t);
469 extern err_location_t *err_originally_at_ext_address(err_location_t *, err_ext_address_t const *);
470
471
472
473 extern err_location_t *err_set_next_loc (
474 err_location_t *,
475 err_location_t const *,
476 err_location_printer_t);
477
478 extern void err_set_in_file (err_position_t *, char const *);
479 extern void err_set_in_line (err_position_t *, char const *, int);
480 extern void err_set_at_position (err_position_t *, char const *, int, int);
481
482
483
484
485 extern int fxprintf (FILE *, char const *format, ...) ATTR_FORMAT_PRINTF (2,3);
486
487
488
489
490
491
492
493
494
495
496
497
498
499 extern int vfxprintf (FILE *, char const *format, va_list) ATTR_FORMAT_VPRINTF(2);
500
501
502 extern int foxprintf (FILE *, int, char const *format, ...) ATTR_FORMAT_PRINTF (3,4);
503
504
505 extern int vfoxprintf (FILE *, int, char const *format, va_list) ATTR_FORMAT_VPRINTF(3);
506
507
508 #ifdef ERR_HAVE_STDERR
509
510 extern int xprintf (char const *format, ...) ATTR_FORMAT_PRINTF (1,2);
511
512
513 extern int vxprintf (char const *format, va_list) ATTR_FORMAT_VPRINTF(1);
514
515
516 extern int oxprintf (int, char const *format, ...) ATTR_FORMAT_PRINTF (2,3);
517
518
519 extern int voxprintf (int, char const *format, va_list) ATTR_FORMAT_VPRINTF(2);
520
521
522 #endif
523
524 extern int snxprintf (char *, int, char const *format, ...) ATTR_FORMAT_PRINTF (3,4);
525
526
527
528
529
530
531
532
533
534 extern int vsnxprintf (char *, int, char const *format, va_list) ATTR_FORMAT_VPRINTF(3);
535
536
537 extern int snoxprintf (char *, int, int, char const *format, ...) ATTR_FORMAT_PRINTF (4,5);
538
539
540 extern int vsnoxprintf (char *, int, int, char const *format, va_list) ATTR_FORMAT_VPRINTF(4);
541
542
543
544
545
546 #ifdef IN_LIBERROR_COMPILE
547 # include "error/printf-gen.h"
548 #else
549 # include <error/printf-gen.h>
550 #endif
551
552
553 extern int voleprintf (
554 int tag, int number, int , err_location_t const *,
555 char const *, va_list) ATTR_FORMAT_VPRINTF(5);
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679 #ifdef __cplusplus
680 }
681 #endif
682
683 #endif