diff options
author | rl1987 <rl1987@sdf.lonestar.org> | 2018-03-18 14:56:59 +0100 |
---|---|---|
committer | rl1987 <rl1987@sdf.lonestar.org> | 2019-03-05 16:46:40 +0200 |
commit | f236c9e7f91f8844d004164d6202915a5c78e6d6 (patch) | |
tree | 78a8e2cb00599404f441b8715e4929837d5bc92f /src/lib/log | |
parent | c5da1f1cd52dfbf6dc68fba70df42ec4976a589c (diff) | |
download | tor-f236c9e7f91f8844d004164d6202915a5c78e6d6.tar.gz tor-f236c9e7f91f8844d004164d6202915a5c78e6d6.zip |
Introduce tor_assertf() to allow logging extra error message on assert failure
With format string support!
Diffstat (limited to 'src/lib/log')
-rw-r--r-- | src/lib/log/util_bug.c | 17 | ||||
-rw-r--r-- | src/lib/log/util_bug.h | 16 |
2 files changed, 26 insertions, 7 deletions
diff --git a/src/lib/log/util_bug.c b/src/lib/log/util_bug.c index f42d2d2ab4..93a460156d 100644 --- a/src/lib/log/util_bug.c +++ b/src/lib/log/util_bug.c @@ -70,14 +70,25 @@ tor_set_failed_assertion_callback(void (*fn)(void)) /** Helper for tor_assert: report the assertion failure. */ void tor_assertion_failed_(const char *fname, unsigned int line, - const char *func, const char *expr) + const char *func, const char *expr, + const char *fmt, ...) { char buf[256]; + char *extra = NULL; + va_list ap; + + if (fmt) { + va_start(ap,fmt); + tor_vasprintf(&extra, fmt, ap); + va_end(ap); + } + log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.", fname, line, func, expr); tor_snprintf(buf, sizeof(buf), - "Assertion %s failed in %s at %s:%u", - expr, func, fname, line); + "Assertion %s failed in %s at %s:%u: %s", + expr, func, fname, line, extra ? extra : ""); + tor_free(extra); log_backtrace(LOG_ERR, LD_BUG, buf); } diff --git a/src/lib/log/util_bug.h b/src/lib/log/util_bug.h index 18d40bbf39..c0670a50a4 100644 --- a/src/lib/log/util_bug.h +++ b/src/lib/log/util_bug.h @@ -92,13 +92,20 @@ #define tor_assert(a) STMT_BEGIN \ (void)(a); \ STMT_END +#define tor_assertf(a, fmt, ...) STMT_BEGIN \ + (void)(a); \ + (void)(fmt); \ + STMT_END #else /** Like assert(3), but send assertion failures to the log as well as to * stderr. */ -#define tor_assert(expr) STMT_BEGIN \ +#define tor_assert(expr) tor_assertf(expr, NULL) + +#define tor_assertf(expr, fmt, ...) STMT_BEGIN \ if (ASSERT_PREDICT_LIKELY_(expr)) { \ } else { \ - tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \ + tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr, \ + fmt, ##__VA_ARGS__); \ abort(); \ } STMT_END #endif /* defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS) */ @@ -106,7 +113,7 @@ #define tor_assert_unreached() \ STMT_BEGIN { \ tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, \ - "line should be unreached"); \ + "line should be unreached", NULL); \ abort(); \ } STMT_END @@ -221,7 +228,8 @@ #define tor_fragile_assert() tor_assert_nonfatal_unreached_once() void tor_assertion_failed_(const char *fname, unsigned int line, - const char *func, const char *expr); + const char *func, const char *expr, + const char *fmt, ...); void tor_bug_occurred_(const char *fname, unsigned int line, const char *func, const char *expr, int once); |