From bf56062df55226e44afb574791dbbe67d3632642 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 26 Dec 2011 04:20:52 -0500 Subject: Print errno in dmnsn_report_error(). --- libdimension/error.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'libdimension/error.c') diff --git a/libdimension/error.c b/libdimension/error.c index 5b82548..c738639 100644 --- a/libdimension/error.c +++ b/libdimension/error.c @@ -25,8 +25,9 @@ #include "dimension-internal.h" #include -#include /* For fprintf() */ -#include /* For exit() */ +#include +#include +#include /** Report internal errors in this file. */ #define DMNSN_LOCAL_ERROR(str) \ @@ -55,9 +56,11 @@ dmnsn_local_unlock_mutex_impl(pthread_mutex_t *mutex) } /** Lock a mutex, bailing out without dmnsn_error() on error. */ -#define dmnsn_local_lock_mutex(mutex) dmnsn_local_lock_mutex_impl((mutex)); { +#define dmnsn_local_lock_mutex(mutex) \ + dmnsn_local_lock_mutex_impl((mutex)); { /** Unlock a mutex, bailing out without dmnsn_error() on error. */ -#define dmnsn_local_unlock_mutex(mutex) dmnsn_local_unlock_mutex_impl((mutex)); } +#define dmnsn_local_unlock_mutex(mutex) \ + dmnsn_local_unlock_mutex_impl((mutex)); } /** The default fatal error handler. */ static void dmnsn_default_fatal_error_fn(void); @@ -69,7 +72,7 @@ static pthread_mutex_t dmnsn_fatal_mutex = PTHREAD_MUTEX_INITIALIZER; /** The current resilience. */ static bool dmnsn_always_die = false; -/** Mutex which protexts \c dmnsn_always_die. */ +/** Mutex which protects \c dmnsn_always_die. */ static pthread_mutex_t dmnsn_always_die_mutex = PTHREAD_MUTEX_INITIALIZER; /* Called by dmnsn_error macro (don't call directly) */ @@ -77,6 +80,8 @@ void dmnsn_report_error(bool die, const char *func, const char *file, unsigned int line, const char *str) { + int err = errno; + bool always_die; dmnsn_local_lock_mutex(&dmnsn_always_die_mutex); always_die = dmnsn_always_die; @@ -84,6 +89,15 @@ dmnsn_report_error(bool die, const char *func, const char *file, fprintf(stderr, "Dimension %s: %s, %s:%u: %s\n", die ? "ERROR" : "WARNING", func, file, line, str); + if (err != 0) { + fprintf(stderr, "Last error: %d", err); +#if DMNSN_SYS_ERRLIST + if (err >= 0 && err < sys_nerr) { + fprintf(stderr, " (%s)", sys_errlist[err]); + } +#endif + fprintf(stderr, "\n"); + } if (die || always_die) { /* Prevent infinite recursion if the fatal error function itself calls -- cgit v1.2.3