From 7656744877f55658c205b1c5d2bf76651def10b2 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Tue, 9 Feb 2021 12:32:33 +0000 Subject: [PATCH] debug: make use of log_msg() in fatal_error() and bug() functions Instead of writing directly to stderr and/or syslog(3). --- debug.c | 17 ++++++++--------- debug.h | 4 +++- xmalloc.c | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/debug.c b/debug.c index 03ead23b..8a86e9df 100644 --- a/debug.c +++ b/debug.c @@ -5,7 +5,7 @@ #include #include #include -#include +#include "log.h" #if defined(__SANITIZE_ADDRESS__) || HAS_FEATURE(address_sanitizer) #include @@ -22,26 +22,25 @@ print_stack_trace(void) } noreturn void -fatal_error(const char *msg, int err) +fatal_error(const char *file, int line, const char *msg, int err) { - syslog(LOG_ERR, "%s: %s", msg, strerror(err)); - errno = err; - perror(msg); + log_msg(LOG_CLASS_ERROR, "debug", file, line, "%s: %s", msg, strerror(err)); print_stack_trace(); + fflush(stderr); abort(); } noreturn void bug(const char *file, int line, const char *func, const char *fmt, ...) { - fprintf(stderr, "\n%s:%d: BUG in %s(): ", file, line, func); - + char buf[4096]; va_list ap; va_start(ap, fmt); - vfprintf(stderr, fmt, ap); + int n = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - fputc('\n', stderr); + const char *msg = likely(n >= 0) ? buf : "??"; + log_msg(LOG_CLASS_ERROR, "debug", file, line, "BUG in %s(): %s", func, msg); print_stack_trace(); fflush(stderr); abort(); diff --git a/debug.h b/debug.h index fcc09830..92a0e091 100644 --- a/debug.h +++ b/debug.h @@ -2,6 +2,8 @@ #include "macros.h" +#define FATAL_ERROR(...) fatal_error(__FILE__, __LINE__, __VA_ARGS__) + #ifdef NDEBUG #define BUG(...) UNREACHABLE() #else @@ -26,5 +28,5 @@ #endif #endif -noreturn void fatal_error(const char *msg, int err) COLD; +noreturn void fatal_error(const char *file, int line, const char *msg, int err) COLD; noreturn void bug(const char *file, int line, const char *func, const char *fmt, ...) PRINTF(4) COLD; diff --git a/xmalloc.c b/xmalloc.c index 16b3565a..28af50f9 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -11,7 +11,7 @@ static void * check_alloc(void *alloc) { if (unlikely(alloc == NULL)) { - fatal_error(__func__, ENOMEM); + FATAL_ERROR(__func__, ENOMEM); } return alloc; } @@ -64,7 +64,7 @@ xvasprintf_(char **strp, const char *format, va_list ap) va_copy(ap2, ap); int n = vsnprintf(NULL, 0, format, ap2); if (unlikely(n < 0)) { - fatal_error("vsnprintf", EILSEQ); + FATAL_ERROR("vsnprintf", EILSEQ); } va_end(ap2); *strp = xmalloc(n + 1);