diff options
Diffstat (limited to 'libdimension/error.c')
-rw-r--r-- | libdimension/error.c | 93 |
1 files changed, 35 insertions, 58 deletions
diff --git a/libdimension/error.c b/libdimension/error.c index 974503a..b83dec4 100644 --- a/libdimension/error.c +++ b/libdimension/error.c @@ -1,5 +1,5 @@ /************************************************************************* - * Copyright (C) 2009-2010 Tavian Barnes <tavianator@gmail.com> * + * Copyright (C) 2009-2011 Tavian Barnes <tavianator@gmail.com> * * * * This file is part of The Dimension Library. * * * @@ -37,78 +37,55 @@ static dmnsn_fatal_error_fn *dmnsn_fatal = &dmnsn_default_fatal_error_fn; static pthread_mutex_t dmnsn_fatal_mutex = PTHREAD_MUTEX_INITIALIZER; /** The current resilience. */ -static dmnsn_severity dmnsn_resilience = DMNSN_SEVERITY_MEDIUM; -/** Mutex which protexts \c dmnsn_resilience. */ -static pthread_mutex_t dmnsn_resilience_mutex = PTHREAD_MUTEX_INITIALIZER; +static bool dmnsn_always_die = false; +/** Mutex which protexts \c dmnsn_always_die. */ +static pthread_mutex_t dmnsn_always_die_mutex = PTHREAD_MUTEX_INITIALIZER; -/* Called by dmnsn_error macro (don't call directly). */ +/* Called by dmnsn_error macro (don't call directly) */ void -dmnsn_report_error(dmnsn_severity severity, - const char *func, const char *file, unsigned int line, - const char *str) +dmnsn_report_error(bool die, const char *func, const char *file, + unsigned int line, const char *str) { - if (severity >= dmnsn_get_resilience()) { - /* An error more severe than our resilience happened, bail out */ - fprintf(stderr, "Dimension ERROR: %s, %s:%u: %s\n", - func, file, line, str); - dmnsn_fatal_error_fn *fatal = dmnsn_get_fatal_error_fn(); - (*fatal)(); - exit(EXIT_FAILURE); /* Failsafe in case *dmnsn_fatal doesn't exit */ - } else { - /* A trivial error happened, warn and continue */ - fprintf(stderr, "Dimension WARNING: %s, %s:%u: %s\n", - func, file, line, str); - } -} - -/* Return the current resilience, thread-safely. */ -dmnsn_severity -dmnsn_get_resilience(void) -{ - dmnsn_severity resilience; - if (pthread_mutex_lock(&dmnsn_resilience_mutex) != 0) { - /* Couldn't lock the mutex, so warn and continue. */ - fprintf(stderr, "Dimension WARNING: %s, line %u: %s\n", + if (pthread_mutex_lock(&dmnsn_always_die_mutex) != 0) { + fprintf(stderr, "Dimension ERROR: %s, line %u: %s\n", DMNSN_FUNC, __LINE__, - "Couldn't lock resilience mutex."); + "Couldn't lock mutex."); + exit(EXIT_FAILURE); } - resilience = dmnsn_resilience; /* Copy the static variable to a local */ - if (pthread_mutex_unlock(&dmnsn_resilience_mutex) != 0) { - /* Couldn't unlock the mutex, so warn and continue. If the mutex was locked - earlier, the next dmnsn_get/set_resilience is likely to hang. */ - fprintf(stderr, "Dimension WARNING: %s, line %u: %s\n", + bool always_die = dmnsn_always_die; + if (pthread_mutex_unlock(&dmnsn_always_die_mutex) != 0) { + fprintf(stderr, "Dimension ERROR: %s, line %u: %s\n", DMNSN_FUNC, __LINE__, - "Couldn't unlock resilience mutex."); + "Couldn't unlock mutex."); + exit(EXIT_FAILURE); } - return resilience; -} -/* Set the resilience, thread-safely */ -void -dmnsn_set_resilience(dmnsn_severity resilience) -{ - if (resilience < DMNSN_SEVERITY_LOW || resilience > DMNSN_SEVERITY_HIGH) { - /* Tried to set an illegal resilience, bail out */ - fprintf(stderr, "Dimension ERROR: %s, line %u: %s\n", DMNSN_FUNC, __LINE__, - "Resilience has wrong value."); + fprintf(stderr, "Dimension %s: %s, %s:%u: %s\n", + die ? "ERROR" : "WARNING", func, file, line, str); + + if (die || always_die) { + /* An error happened, bail out */ dmnsn_fatal_error_fn *fatal = dmnsn_get_fatal_error_fn(); (*fatal)(); - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); /* Failsafe in case *dmnsn_fatal doesn't exit */ } +} - if (pthread_mutex_lock(&dmnsn_resilience_mutex) != 0) { - /* Couldn't lock the mutex, so warn and continue. */ - fprintf(stderr, "Dimension WARNING: %s, line %u: %s\n", +void +dmnsn_die_on_warnings(bool always_die) +{ + if (pthread_mutex_lock(&dmnsn_always_die_mutex) != 0) { + fprintf(stderr, "Dimension ERROR: %s, line %u: %s\n", DMNSN_FUNC, __LINE__, - "Couldn't lock resilience mutex."); + "Couldn't lock mutex."); + exit(EXIT_FAILURE); } - dmnsn_resilience = resilience; - if (pthread_mutex_unlock(&dmnsn_resilience_mutex) != 0) { - /* Couldn't unlock the mutex, so warn and continue. If the mutex was locked - earlier, the next dmnsn_get/set_resilience is likely to hang. */ - fprintf(stderr, "Dimension WARNING: %s, line %u: %s\n", + dmnsn_always_die = always_die; + if (pthread_mutex_unlock(&dmnsn_always_die_mutex) != 0) { + fprintf(stderr, "Dimension ERROR: %s, line %u: %s\n", DMNSN_FUNC, __LINE__, - "Couldn't unlock resilience mutex."); + "Couldn't unlock mutex."); + exit(EXIT_FAILURE); } } |