summaryrefslogtreecommitdiff
path: root/shared/debug.h
blob: 18f9f0e670ccc347becfafcd29c460d509a940bc (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef foodebughfoo
#define foodebughfoo

/* FIXME: Should these be shared? */
/* clang-format off */
enum debug_lvl {
	DBG_ERROR	= (0x1 << 1),
	DBG_INFO	= (0x1 << 2),
	DBG_VERBOSE	= (0x1 << 3),
	DBG_CFG		= (0x1 << 4),
	DBG_REF		= (0x1 << 5),
	DBG_MALLOC	= (0x1 << 6),
	DBG_ANN		= (0x1 << 7),
	DBG_SIG		= (0x1 << 8),
	DBG_UR		= (0x1 << 9),
	DBG_SRV		= (0x1 << 10),
	DBG_PROXY	= (0x1 << 11),
	DBG_RCON	= (0x1 << 12),
	DBG_IDLE	= (0x1 << 13),
	DBG_IGMP	= (0x1 << 14),
	DBG_SYSD	= (0x1 << 15),
	DBG_DNS		= (0x1 << 16),
	DBG_TIMER	= (0x1 << 17),
};
/* clang-format on */

static inline bool debug_enabled(enum debug_lvl lvl)
{
	return !!(lvl & debug_mask);
}

#define __ifdebug(lvl, fmt, ...)                                               \
	do {                                                                   \
		if (debug_enabled((lvl)))                                      \
			__debug((lvl), fmt "\n"__VA_OPT__(, )__VA_ARGS__);     \
	} while (0)

#define debug(lvl, fmt, ...)                                                   \
	__ifdebug((lvl), "%s:%s:%i: " fmt, __FILE__, __func__,                 \
		  __LINE__ __VA_OPT__(, ) __VA_ARGS__)
#define verbose(fmt, ...) __ifdebug(DBG_VERBOSE, fmt, __VA_ARGS__)
#define info(fmt, ...)	  __ifdebug(DBG_INFO, fmt, __VA_ARGS__)
#define error(fmt, ...)                                                        \
	__ifdebug(DBG_ERROR, "%s:%s:%i: " fmt, __FILE__, __func__,             \
		  __LINE__ __VA_OPT__(, ) __VA_ARGS__)

#define die(fmt, ...)                                                          \
	__die("%s:%s:%i: " fmt "\n", __FILE__, __func__,                       \
	      __LINE__ __VA_OPT__(, ) __VA_ARGS__)

#define assert_log(expr, msg)                                                  \
	((expr) ? (true) :                                                     \
			(__debug(DBG_ERROR,                                          \
			   "%s:%s:%i: assertion \"" msg "\" failed\n",         \
			   __FILE__, __func__, __LINE__),                      \
		   false))

#define assert_return(expr, ...)                                               \
	do {                                                                   \
		if (!assert_log(expr, #expr))                                  \
			return __VA_ARGS__;                                    \
	} while (0)

#define assert_return_silent(expr, ...)                                        \
	do {                                                                   \
		if (!(expr))                                                   \
			return __VA_ARGS__;                                    \
	} while (0)

#define assert_die(expr, msg)                                                  \
	do {                                                                   \
		if (!assert_log(expr, #expr))                                  \
			die(msg);                                              \
	} while (0)

#define assert_task_alive_or(lvl, t, cmd)                                      \
	do {                                                                   \
		if (!(t)) {                                                    \
			error("invalid task");                                 \
			cmd;                                                   \
		}                                                              \
                                                                               \
		if ((t)->dead) {                                               \
			debug((lvl), "task dead");                             \
			cmd;                                                   \
		}                                                              \
	} while (0)

#define assert_task_alive(lvl, t) assert_task_alive_or((lvl), (t), return )

#endif