-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.h
65 lines (56 loc) · 1.87 KB
/
debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Debugging for symbiont execution. Example usage:
*
* DECLARE_CHANNEL(stuff);
* TRACE(stuff, "is happening!");
* ERR(stuff, "something really bad happened.");
* WARN(stuff, "i think something's wrong?");
*
* The user could enable / disable the above channel by setting the
* LIBSITU_DEBUG environment variable:
*
* export LIBSITU_DEBUG="stuff=+err,-warn,+trace" */
#ifndef FP_SYMBIONT_DEBUG_H
#define FP_SYMBIONT_DEBUG_H 1
#include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
enum SymbiontChanClass {
SymbiontErr=0,
SymbiontWarn,
SymbiontTrace,
SymbiontFixme,
};
struct symbdbgchannel {
unsigned flags;
char name[32];
};
#define DEFAULT_CHFLAGS \
(1U << SymbiontErr) | (1U << SymbiontWarn) | (1U << SymbiontFixme)
/* creates a new debug channel. debug channels are private to implementation,
* and should not be declared in header files. */
#define DECLARE_CHANNEL(ch) \
static struct symbdbgchannel symb_chn_##ch = { DEFAULT_CHFLAGS, #ch }; \
__attribute__((constructor(200))) static void \
ch_init_##ch() { \
const char* dbg_ = getenv("LIBSITU_DEBUG"); \
symb_parse_options(&symb_chn_##ch, dbg_); \
}
#define TRACE(ch, args...) \
symb_dbg(SymbiontTrace, &symb_chn_##ch, __FUNCTION__, args)
#define ERR(ch, args...) \
symb_dbg(SymbiontErr, &symb_chn_##ch, __FUNCTION__, args)
#define WARN(ch, args...) \
symb_dbg(SymbiontWarn, &symb_chn_##ch, __FUNCTION__, args)
#define FIXME(ch, args...) \
symb_dbg(SymbiontFixme, &symb_chn_##ch, __FUNCTION__, args)
/* for internal use only. */
extern void symb_dbg(enum SymbiontChanClass, const struct symbdbgchannel*,
const char* func, const char* format, ...)
__attribute__((format(printf, 4, 5)));
extern void symb_parse_options(struct symbdbgchannel*, const char* opt);
#ifdef __cplusplus
}
#endif
#endif /* FP_SYMBIONT_DEBUG_H */