8
|
1 |
#ifndef LIB_LOG_H
|
|
2 |
#define LIB_LOG_H
|
1
|
3 |
|
|
4 |
/*
|
|
5 |
* error handling
|
|
6 |
*/
|
|
7 |
|
16
|
8 |
enum log_display_flags {
|
|
9 |
LOG_DISPLAY_STDOUT = 0x00,
|
|
10 |
LOG_DISPLAY_STDERR = 0x01,
|
|
11 |
|
|
12 |
LOG_DISPLAY_PERR = 0x02,
|
|
13 |
|
|
14 |
LOG_DISPLAY_NONL = 0x04,
|
|
15 |
};
|
|
16 |
|
|
17 |
|
|
18 |
void _generic_err (int flags, const char *func, int err, const char *fmt, ...)
|
1
|
19 |
__attribute__ ((format (printf, 4, 5)));
|
|
20 |
|
|
21 |
// needs to be defined as its own function for the noreturn attribute
|
16
|
22 |
void _generic_err_exit (int flags, const char *func, int err, const char *fmt, ...)
|
1
|
23 |
__attribute__ ((format (printf, 4, 5)))
|
|
24 |
__attribute__ ((noreturn));
|
|
25 |
|
|
26 |
enum _debug_level {
|
|
27 |
DEBUG_FATAL,
|
|
28 |
DEBUG_ERROR,
|
|
29 |
DEBUG_WARNING,
|
|
30 |
DEBUG_INFO,
|
|
31 |
DEBUG_DEBUG,
|
|
32 |
};
|
|
33 |
|
|
34 |
// not currently used
|
|
35 |
extern enum _debug_level _cur_debug_level;
|
|
36 |
|
|
37 |
// various kinds of ways to handle an error, 2**3 of them, *g*
|
16
|
38 |
#define info(...) _generic_err( LOG_DISPLAY_STDOUT, NULL, 0, __VA_ARGS__ )
|
|
39 |
#define error(...) _generic_err( LOG_DISPLAY_STDERR, NULL, 0, __VA_ARGS__ )
|
|
40 |
#define err_exit(...) _generic_err_exit( LOG_DISPLAY_STDERR, NULL, 0, __VA_ARGS__ )
|
|
41 |
#define perr(...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, NULL, 0, __VA_ARGS__ )
|
|
42 |
#define perr_exit(...) _generic_err_exit( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, NULL, 0, __VA_ARGS__ )
|
|
43 |
#define err_func(func, ...) _generic_err( LOG_DISPLAY_STDERR, func, 0, __VA_ARGS__ )
|
|
44 |
#define err_func_exit(func, ...) _generic_err_exit( LOG_DISPLAY_STDERR, func, 0, __VA_ARGS__ )
|
|
45 |
#define perr_func(func, ...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, func, 0, __VA_ARGS__ )
|
|
46 |
#define perr_func_exit(func, ...) _generic_err_exit( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, func, 0, __VA_ARGS__ )
|
|
47 |
#define eerr_func(func, err, ...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, func, err, __VA_ARGS__ )
|
|
48 |
#define debug(func, ...) _generic_err( LOG_DISPLAY_STDERR, func, 0, __VA_ARGS__ )
|
|
49 |
#define debug_nonl(func, ...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_NONL, func, 0, __VA_ARGS__ )
|
1
|
50 |
|
16
|
51 |
// logging includes errors
|
8
|
52 |
#include "error.h"
|
|
53 |
|
1
|
54 |
#define WARNING(...) err_func(__func__, __VA_ARGS__)
|
|
55 |
#define PWARNING(...) perr_func(__func__, __VA_ARGS__)
|
2
|
56 |
#define EWARNING(err, ...) eerr_func(__func__, (err), __VA_ARGS__)
|
1
|
57 |
|
|
58 |
#ifdef DEBUG_ENABLED
|
16
|
59 |
#define DEBUG(...) debug(__func__, __VA_ARGS__)
|
|
60 |
#define DEBUGF(...) debug(NULL, __VA_ARGS__)
|
|
61 |
#define DEBUGN(...) debug_nonl(__func__, __VA_ARGS__)
|
|
62 |
#define DEBUGNF(...) debug_nonl(NULL, __VA_ARGS__)
|
1
|
63 |
#else
|
|
64 |
#define DEBUG(...) (void) (0)
|
16
|
65 |
#define DEBUGF(...) (void) (0)
|
|
66 |
#define DEBUGN(...) (void) (0)
|
|
67 |
#define DEBUGNF(...) (void) (0)
|
1
|
68 |
#endif
|
|
69 |
|
|
70 |
// default is to enable INFO
|
|
71 |
#ifdef INFO_DISABLED
|
|
72 |
#define INFO_ENABLED 0
|
|
73 |
#else
|
|
74 |
#ifndef INFO_ENABLED
|
|
75 |
#define INFO_ENABLED 1
|
|
76 |
#endif
|
|
77 |
#endif
|
|
78 |
|
|
79 |
#if INFO_ENABLED
|
|
80 |
#define INFO(...) info(__VA_ARGS__)
|
|
81 |
#else
|
16
|
82 |
#define INFO(...) (void) (__VA_ARGS__)
|
1
|
83 |
#endif
|
|
84 |
|
8
|
85 |
#endif /* LIB_LOG_H */
|