From ab51ac11e68ce0b075688bf17fc89e0ba645b2ed Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Sat, 20 Jun 2020 19:34:58 +0200 Subject: Add new assert macros, convert server.c to use them --- main.h | 55 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 20 deletions(-) (limited to 'main.h') diff --git a/main.h b/main.h index 9d50bc8..ccd7221 100644 --- a/main.h +++ b/main.h @@ -46,17 +46,47 @@ void __debug(enum debug_lvl lvl, const char *fmt, ...) __attribute__((format(pri } while (0) #define debug(lvl, fmt, ...) __ifdebug((lvl), "%s:%s:%i: " fmt, \ - __func__, __FILE__, __LINE__ \ + __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: " fmt, \ - __func__ __VA_OPT__(,) __VA_ARGS__) +#define error(fmt, ...) __ifdebug(DBG_ERROR, "%s:%s:%i: " fmt, \ + __FILE__, __func__, __LINE__ \ + __VA_OPT__(,) __VA_ARGS__) void __die(const char *fmt, ...) __attribute__((format(printf, 1, 2))); -#define die(fmt, ...) __die("%s:%i: " fmt "\n", __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_task_alive_or(lvl, t, cmd) \ +do { \ + if (!(t)) { \ + error("invalid task\n"); \ + cmd; \ + } \ + \ + if ((t)->dead) { \ + debug((lvl), "task dead\n"); \ + cmd; \ + } \ +} while(0) + +#define assert_task_alive(lvl, t) assert_task_alive_or((lvl), (t), return) struct uring_task; @@ -95,21 +125,6 @@ struct uring_task { void *priv; }; -#define assert_task_alive_or(lvl, t, cmd) \ -do { \ - if (!(t)) { \ - error("invalid task\n"); \ - cmd; \ - } \ - \ - if ((t)->dead) { \ - debug((lvl), "task dead\n"); \ - cmd; \ - } \ -} while(0) - -#define assert_task_alive(lvl, t) assert_task_alive_or((lvl), (t), return) - struct cfg { uid_t uid; gid_t gid; -- cgit v1.2.3