From 0e08695d031ebb2e00a13582a75e1f27c2d6c73a Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 24 Aug 2025 03:11:20 +0200 Subject: Parameterize/improve failure and check functions Signed-off-by: Mel --- boot/common.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'boot/common.c') diff --git a/boot/common.c b/boot/common.c index 4f9c2ad..b8587bc 100644 --- a/boot/common.c +++ b/boot/common.c @@ -79,31 +79,50 @@ #define ANSI_UNDERLINE ANSI("4") #define ANSI_NO_UNDERLINE ANSI("24") +#define FAILURE_MESSAGE ANSI_BOLD ";( sorry, a failure has occurred..." ANSI_NO_BOLD + // call on irrecoverable failure. // prints a very sad, apologetic message for // the user and aborts program with failure status. NORETURN void -failure(const ascii* message) +failure(const ascii* message, ...) { fflush(stdout); // flush stdout to ensure any message is printed before the error. - const ascii* format = ANSI_RED ANSI_BOLD - ";( sorry, a failure has occurred..." ANSI_NO_BOLD "\n" - "-> %s!" - "\n" ANSI_RESET; - fprintf(stderr, format, message); + fprintf(stderr, ANSI_RED FAILURE_MESSAGE "\n-> "); + va_list args; + va_start(args, message); + vfprintf(stderr, message, args); + va_end(args); + + fprintf(stderr, "!\n" ANSI_RESET); exit(EXIT_FAILURE); } -// check a condition, triggering a failure if it's false. +// internal check function, use the check() macro instead. void -check(bool condition, const ascii* message) -{ - if (!condition) failure(message); +_check(bool condition, const ascii* condition_str, const ascii* message, ...) +{ + if (!condition) { + va_list args; + va_start(args, message); + fflush(stdout); + fprintf(stderr, ANSI_RED FAILURE_MESSAGE "\n"); + fprintf(stderr, "-> check failed: %s\n", condition_str); + fprintf(stderr, "-> "); + vfprintf(stderr, message, args); + fprintf(stderr, "!\n" ANSI_RESET); + va_end(args); + exit(EXIT_FAILURE); + } } +// check a condition, triggering a failure if it's false. +// prints both the condition and the formatted message. +#define check(condition, message, ...) _check((condition), #condition, (message), ##__VA_ARGS__) + NORETURN void unreachable() -- cgit 1.4.1