summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-05-17 13:13:06 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-05-17 13:57:01 -0400
commitc70e7375b1808b23fe4db2018d5e8c9bcb70464d (patch)
treeb84fca909d89c7d5a7f461c7cafeae0058bdca79 /src
parentc3d6f07b8e4d22df533ce7a90660ae1da2df5476 (diff)
downloadbfs-c70e7375b1808b23fe4db2018d5e8c9bcb70464d.tar.xz
diag: New helpers to include xstrerror(errno) automatically
Diffstat (limited to 'src')
-rw-r--r--src/diag.c4
-rw-r--r--src/diag.h43
-rw-r--r--src/sighook.c4
-rw-r--r--src/xspawn.c2
-rw-r--r--src/xtime.c4
5 files changed, 51 insertions, 6 deletions
diff --git a/src/diag.c b/src/diag.c
index deb6f26..bb744f6 100644
--- a/src/diag.c
+++ b/src/diag.c
@@ -38,6 +38,10 @@ noreturn void bfs_abortf(const struct bfs_loc *loc, const char *format, ...) {
abort();
}
+const char *bfs_errstr(void) {
+ return xstrerror(errno);
+}
+
const char *debug_flag_name(enum debug_flags flag) {
switch (flag) {
case DEBUG_COST:
diff --git a/src/diag.h b/src/diag.h
index 2b13609..f2498b5 100644
--- a/src/diag.h
+++ b/src/diag.h
@@ -55,6 +55,20 @@ void bfs_diagf(const struct bfs_loc *loc, const char *format, ...);
#define bfs_diag(...) bfs_diagf(bfs_location(), __VA_ARGS__)
/**
+ * Get the last error message.
+ */
+const char *bfs_errstr(void);
+
+/**
+ * Print a diagnostic message including the last error.
+ */
+#define bfs_ediag(...) \
+ bfs_ediag_("" __VA_ARGS__, bfs_errstr())
+
+#define bfs_ediag_(format, ...) \
+ bfs_diag(sizeof(format) > 1 ? format ": %s" : "%s", __VA_ARGS__)
+
+/**
* Print a message to standard error and abort.
*/
attr(cold, printf(2, 3))
@@ -63,15 +77,27 @@ noreturn void bfs_abortf(const struct bfs_loc *loc, const char *format, ...);
/**
* Unconditional abort with a message.
*/
-#define bfs_abort(...) bfs_abortf(bfs_location(), __VA_ARGS__)
+#define bfs_abort(...) \
+ bfs_abortf(bfs_location(), __VA_ARGS__)
+
+/**
+ * Abort with a message including the last error.
+ */
+#define bfs_eabort(...) \
+ bfs_eabort_("" __VA_ARGS__, bfs_errstr())
+
+#define bfs_eabort_(format, ...) \
+ bfs_abort(sizeof(format) > 1 ? format ": %s" : "%s", __VA_ARGS__)
/**
* Abort in debug builds; no-op in release builds.
*/
#ifdef NDEBUG
# define bfs_bug(...) ((void)0)
+# define bfs_ebug(...) ((void)0)
#else
# define bfs_bug bfs_abort
+# define bfs_ebug bfs_eabort
#endif
/**
@@ -88,12 +114,27 @@ noreturn void bfs_abortf(const struct bfs_loc *loc, const char *format, ...);
str, __VA_ARGS__))
/**
+ * Unconditional assert, including the last error.
+ */
+#define bfs_everify(...) \
+ bfs_everify_(#__VA_ARGS__, __VA_ARGS__, "", bfs_errstr())
+
+#define bfs_everify_(str, cond, format, ...) \
+ ((cond) ? (void)0 : bfs_abort( \
+ sizeof(format) > 1 \
+ ? "%.0s" format "%s: %s" \
+ : "Assertion failed: `%s`: %s", \
+ str, __VA_ARGS__))
+
+/**
* Assert in debug builds; no-op in release builds.
*/
#ifdef NDEBUG
# define bfs_assert(...) ((void)0)
+# define bfs_eassert(...) ((void)0)
#else
# define bfs_assert bfs_verify
+# define bfs_eassert bfs_everify
#endif
struct bfs_ctx;
diff --git a/src/sighook.c b/src/sighook.c
index 745ccb9..ff5b96f 100644
--- a/src/sighook.c
+++ b/src/sighook.c
@@ -104,7 +104,7 @@ static int arc_sem_wait(struct arc *arc) {
#if _POSIX_SEMAPHORES > 0
if (arc->sem_status == 0) {
while (sem_wait(&arc->sem) != 0) {
- bfs_verify(errno == EINTR, "sem_wait(): %s", xstrerror(errno));
+ bfs_everify(errno == EINTR, "sem_wait()");
}
return 0;
}
@@ -146,7 +146,7 @@ static void arc_destroy(struct arc *arc) {
#if _POSIX_SEMAPHORES > 0
if (arc->sem_status == 0) {
- bfs_verify(sem_destroy(&arc->sem) == 0, "sem_destroy(): %s", xstrerror(errno));
+ bfs_everify(sem_destroy(&arc->sem) == 0, "sem_destroy()");
}
#endif
}
diff --git a/src/xspawn.c b/src/xspawn.c
index 5a8277d..33e5a4a 100644
--- a/src/xspawn.c
+++ b/src/xspawn.c
@@ -611,7 +611,7 @@ static pid_t bfs_fork_spawn(struct bfs_resolver *res, const struct bfs_spawn *ct
// Restore the original signal mask
int ret = pthread_sigmask(SIG_SETMASK, &old_mask, NULL);
- bfs_verify(ret == 0, "pthread_sigmask(): %s", xstrerror(ret));
+ bfs_everify(ret == 0, "pthread_sigmask()");
if (pid < 0) {
// fork() failed
diff --git a/src/xtime.c b/src/xtime.c
index eb11afa..2808455 100644
--- a/src/xtime.c
+++ b/src/xtime.c
@@ -20,7 +20,7 @@ int xmktime(struct tm *tm, time_t *timep) {
struct tm tmp;
if (!localtime_r(&time, &tmp)) {
- bfs_bug("localtime_r(-1): %s", xstrerror(errno));
+ bfs_ebug("localtime_r(-1)");
return -1;
}
@@ -46,7 +46,7 @@ int xtimegm(struct tm *tm, time_t *timep) {
struct tm tmp;
if (!gmtime_r(&time, &tmp)) {
- bfs_bug("gmtime_r(-1): %s", xstrerror(errno));
+ bfs_ebug("gmtime_r(-1)");
return -1;
}