diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2021-02-09 18:52:53 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2021-02-09 18:52:53 +0000 |
| commit | d16217eca9a3a0fa6f34dbb2b2450775fb58357c (patch) | |
| tree | f93bd378247a2e0e678de1eb98678a95dac96c11 | |
| parent | render: use portable HAS_INCLUDE() wrapper instead of __has_include() (diff) | |
| parent | debug: make use of log_msg() in fatal_error() and bug() functions (diff) | |
Merge remote-tracking branch 'origin/debug-log'
| -rw-r--r-- | debug.c | 17 | ||||
| -rw-r--r-- | debug.h | 4 | ||||
| -rw-r--r-- | xmalloc.c | 4 |
3 files changed, 13 insertions, 12 deletions
@@ -5,7 +5,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <syslog.h> +#include "log.h" #if defined(__SANITIZE_ADDRESS__) || HAS_FEATURE(address_sanitizer) #include <sanitizer/asan_interface.h> @@ -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(); @@ -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; @@ -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); |
