debug: make use of log_msg() in fatal_error() and bug() functions

Instead of writing directly to stderr and/or syslog(3).
This commit is contained in:
Craig Barnes 2021-02-09 12:32:33 +00:00
parent e19db15104
commit 7656744877
3 changed files with 13 additions and 12 deletions

17
debug.c
View file

@ -5,7 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include "log.h"
#if defined(__SANITIZE_ADDRESS__) || HAS_FEATURE(address_sanitizer) #if defined(__SANITIZE_ADDRESS__) || HAS_FEATURE(address_sanitizer)
#include <sanitizer/asan_interface.h> #include <sanitizer/asan_interface.h>
@ -22,26 +22,25 @@ print_stack_trace(void)
} }
noreturn 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)); log_msg(LOG_CLASS_ERROR, "debug", file, line, "%s: %s", msg, strerror(err));
errno = err;
perror(msg);
print_stack_trace(); print_stack_trace();
fflush(stderr);
abort(); abort();
} }
noreturn void noreturn void
bug(const char *file, int line, const char *func, const char *fmt, ...) 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_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stderr, fmt, ap); int n = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(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(); print_stack_trace();
fflush(stderr); fflush(stderr);
abort(); abort();

View file

@ -2,6 +2,8 @@
#include "macros.h" #include "macros.h"
#define FATAL_ERROR(...) fatal_error(__FILE__, __LINE__, __VA_ARGS__)
#ifdef NDEBUG #ifdef NDEBUG
#define BUG(...) UNREACHABLE() #define BUG(...) UNREACHABLE()
#else #else
@ -26,5 +28,5 @@
#endif #endif
#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; noreturn void bug(const char *file, int line, const char *func, const char *fmt, ...) PRINTF(4) COLD;

View file

@ -11,7 +11,7 @@ static void *
check_alloc(void *alloc) check_alloc(void *alloc)
{ {
if (unlikely(alloc == NULL)) { if (unlikely(alloc == NULL)) {
fatal_error(__func__, ENOMEM); FATAL_ERROR(__func__, ENOMEM);
} }
return alloc; return alloc;
} }
@ -64,7 +64,7 @@ xvasprintf_(char **strp, const char *format, va_list ap)
va_copy(ap2, ap); va_copy(ap2, ap);
int n = vsnprintf(NULL, 0, format, ap2); int n = vsnprintf(NULL, 0, format, ap2);
if (unlikely(n < 0)) { if (unlikely(n < 0)) {
fatal_error("vsnprintf", EILSEQ); FATAL_ERROR("vsnprintf", EILSEQ);
} }
va_end(ap2); va_end(ap2);
*strp = xmalloc(n + 1); *strp = xmalloc(n + 1);