author | Tero Marttila <terom@fixme.fi> |
Wed, 15 Oct 2008 01:14:22 +0300 | |
changeset 28 | e944453ca924 |
parent 27 | 461be4cd34a3 |
child 31 | 7804cd7b5cd5 |
permissions | -rw-r--r-- |
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, |
|
27 | 15 |
|
16 |
LOG_DISPLAY_FATAL = 0x08, |
|
16 | 17 |
}; |
18 |
||
19 |
||
20 |
void _generic_err (int flags, const char *func, int err, const char *fmt, ...) |
|
1 | 21 |
__attribute__ ((format (printf, 4, 5))); |
22 |
||
23 |
// needs to be defined as its own function for the noreturn attribute |
|
16 | 24 |
void _generic_err_exit (int flags, const char *func, int err, const char *fmt, ...) |
1 | 25 |
__attribute__ ((format (printf, 4, 5))) |
26 |
__attribute__ ((noreturn)); |
|
27 |
||
28
e944453ca924
split off dbfs components into a separate dir, improve dirop docs, error handling, etc
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
28 |
static inline void debug_dummy (int dummy, ...) { /* no-op */ } |
e944453ca924
split off dbfs components into a separate dir, improve dirop docs, error handling, etc
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
29 |
|
1 | 30 |
enum _debug_level { |
31 |
DEBUG_FATAL, |
|
32 |
DEBUG_ERROR, |
|
33 |
DEBUG_WARNING, |
|
34 |
DEBUG_INFO, |
|
35 |
DEBUG_DEBUG, |
|
36 |
}; |
|
37 |
||
38 |
// not currently used |
|
39 |
extern enum _debug_level _cur_debug_level; |
|
40 |
||
41 |
// various kinds of ways to handle an error, 2**3 of them, *g* |
|
16 | 42 |
#define info(...) _generic_err( LOG_DISPLAY_STDOUT, NULL, 0, __VA_ARGS__ ) |
43 |
#define error(...) _generic_err( LOG_DISPLAY_STDERR, NULL, 0, __VA_ARGS__ ) |
|
44 |
#define err_exit(...) _generic_err_exit( LOG_DISPLAY_STDERR, NULL, 0, __VA_ARGS__ ) |
|
45 |
#define perr(...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, NULL, 0, __VA_ARGS__ ) |
|
46 |
#define perr_exit(...) _generic_err_exit( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, NULL, 0, __VA_ARGS__ ) |
|
47 |
#define err_func(func, ...) _generic_err( LOG_DISPLAY_STDERR, func, 0, __VA_ARGS__ ) |
|
24 | 48 |
#define err_func_nonl(func, ...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_NONL, func, 0, __VA_ARGS__ ) |
16 | 49 |
#define err_func_exit(func, ...) _generic_err_exit( LOG_DISPLAY_STDERR, func, 0, __VA_ARGS__ ) |
50 |
#define perr_func(func, ...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, func, 0, __VA_ARGS__ ) |
|
51 |
#define perr_func_exit(func, ...) _generic_err_exit( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, func, 0, __VA_ARGS__ ) |
|
52 |
#define eerr_func(func, err, ...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, func, err, __VA_ARGS__ ) |
|
28
e944453ca924
split off dbfs components into a separate dir, improve dirop docs, error handling, etc
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
53 |
#define eerr_func_exit(func,err,...) _generic_err_exit( LOG_DISPLAY_STDERR | LOG_DISPLAY_PERR, func, err, __VA_ARGS__ ) |
16 | 54 |
#define debug(func, ...) _generic_err( LOG_DISPLAY_STDERR, func, 0, __VA_ARGS__ ) |
55 |
#define debug_nonl(func, ...) _generic_err( LOG_DISPLAY_STDERR | LOG_DISPLAY_NONL, func, 0, __VA_ARGS__ ) |
|
1 | 56 |
|
16 | 57 |
// logging includes errors |
8 | 58 |
#include "error.h" |
59 |
||
1 | 60 |
#define WARNING(...) err_func(__func__, __VA_ARGS__) |
27 | 61 |
#define NWARNING(...) err_func_nonl(__func__, __VA_ARGS__) |
1 | 62 |
#define PWARNING(...) perr_func(__func__, __VA_ARGS__) |
2 | 63 |
#define EWARNING(err, ...) eerr_func(__func__, (err), __VA_ARGS__) |
1 | 64 |
|
65 |
#ifdef DEBUG_ENABLED |
|
16 | 66 |
#define DEBUG(...) debug(__func__, __VA_ARGS__) |
67 |
#define DEBUGF(...) debug(NULL, __VA_ARGS__) |
|
68 |
#define DEBUGN(...) debug_nonl(__func__, __VA_ARGS__) |
|
69 |
#define DEBUGNF(...) debug_nonl(NULL, __VA_ARGS__) |
|
1 | 70 |
#else |
28
e944453ca924
split off dbfs components into a separate dir, improve dirop docs, error handling, etc
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
71 |
#define DEBUG(...) debug_dummy(0, __VA_ARGS__) |
e944453ca924
split off dbfs components into a separate dir, improve dirop docs, error handling, etc
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
72 |
#define DEBUGF(...) debug_dummy(0, __VA_ARGS__) |
e944453ca924
split off dbfs components into a separate dir, improve dirop docs, error handling, etc
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
73 |
#define DEBUGN(...) debug_dummy(0, __VA_ARGS__) |
e944453ca924
split off dbfs components into a separate dir, improve dirop docs, error handling, etc
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
74 |
#define DEBUGNF(...) debug_dummy(0, __VA_ARGS__) |
1 | 75 |
#endif |
76 |
||
77 |
// default is to enable INFO |
|
78 |
#ifdef INFO_DISABLED |
|
79 |
#define INFO_ENABLED 0 |
|
80 |
#else |
|
81 |
#ifndef INFO_ENABLED |
|
82 |
#define INFO_ENABLED 1 |
|
83 |
#endif |
|
84 |
#endif |
|
85 |
||
86 |
#if INFO_ENABLED |
|
87 |
#define INFO(...) info(__VA_ARGS__) |
|
88 |
#else |
|
16 | 89 |
#define INFO(...) (void) (__VA_ARGS__) |
1 | 90 |
#endif |
91 |
||
8 | 92 |
#endif /* LIB_LOG_H */ |