|
1 |
|
2 /* |
|
3 * error handling |
|
4 */ |
|
5 |
|
6 void _generic_err ( /*int level, */ int use_stderr, const char *func, int perr, const char *fmt, ...) |
|
7 __attribute__ ((format (printf, 4, 5))); |
|
8 |
|
9 // needs to be defined as its own function for the noreturn attribute |
|
10 void _generic_err_exit ( /* int level, */ int used_stderr, const char *func, int perr, const char *fmt, ...) |
|
11 __attribute__ ((format (printf, 4, 5))) |
|
12 __attribute__ ((noreturn)); |
|
13 |
|
14 enum _debug_level { |
|
15 DEBUG_FATAL, |
|
16 DEBUG_ERROR, |
|
17 DEBUG_WARNING, |
|
18 DEBUG_INFO, |
|
19 DEBUG_DEBUG, |
|
20 }; |
|
21 |
|
22 // not currently used |
|
23 extern enum _debug_level _cur_debug_level; |
|
24 |
|
25 // various kinds of ways to handle an error, 2**3 of them, *g* |
|
26 #define info(...) _generic_err( 0, NULL, 0, __VA_ARGS__ ) |
|
27 #define error(...) _generic_err( 1, NULL, 0, __VA_ARGS__ ) |
|
28 #define err_exit(...) _generic_err_exit( 1, NULL, 0, __VA_ARGS__ ) |
|
29 #define perr(...) _generic_err( 1, NULL, 1, __VA_ARGS__ ) |
|
30 #define perr_exit(...) _generic_err_exit( 1, NULL, 1, __VA_ARGS__ ) |
|
31 #define err_func(func, ...) _generic_err( 1, func, 0, __VA_ARGS__ ) |
|
32 #define err_func_exit(func, ...) _generic_err_exit( 1, func, 0, __VA_ARGS__ ) |
|
33 #define perr_func(func, ...) _generic_err( 1, func, 1, __VA_ARGS__ ) |
|
34 #define perr_func_exit(func, ...) _generic_err_exit( 1, func, 1, __VA_ARGS__ ) |
|
35 |
|
36 // error(func + colon + msg, ...) + goto error |
|
37 #define ERROR(...) do { err_func(__func__, __VA_ARGS__); goto error; } while (0) |
|
38 #define PERROR(...) do { perr_func(__func__, __VA_ARGS__); goto error; } while (0) |
|
39 #define FATAL(...) err_func_exit(__func__, __VA_ARGS__) |
|
40 #define PFATAL(...) perr_func_exit(__func__, __VA_ARGS__) |
|
41 #define WARNING(...) err_func(__func__, __VA_ARGS__) |
|
42 #define PWARNING(...) perr_func(__func__, __VA_ARGS__) |
|
43 |
|
44 #ifdef DEBUG_ENABLED |
|
45 #define DEBUG(...) err_func(__func__, __VA_ARGS__) |
|
46 #else |
|
47 #define DEBUG(...) (void) (0) |
|
48 #endif |
|
49 |
|
50 // default is to enable INFO |
|
51 #ifdef INFO_DISABLED |
|
52 #define INFO_ENABLED 0 |
|
53 #else |
|
54 #ifndef INFO_ENABLED |
|
55 #define INFO_ENABLED 1 |
|
56 #endif |
|
57 #endif |
|
58 |
|
59 #if INFO_ENABLED |
|
60 #define INFO(...) info(__VA_ARGS__) |
|
61 #else |
|
62 #define INFO(...) (void) (0) |
|
63 #endif |
|
64 |