| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2020 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #define BT_LOG_TAG "LIB/ASSERT-COND" |
| 8 | #include "lib/logging.h" |
| 9 | |
| 10 | #include <string.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <glib.h> |
| 13 | #include "common/assert.h" |
| 14 | #include "assert-cond-base.h" |
| 15 | |
| 16 | static |
| 17 | GString *format_cond_id(const char *cond_type, const char *func, |
| 18 | const char *id_suffix) |
| 19 | { |
| 20 | static const char func_prefix[] = "bt_"; |
| 21 | GString *id = g_string_new(NULL); |
| 22 | const char *src_ch; |
| 23 | |
| 24 | BT_ASSERT(id); |
| 25 | |
| 26 | /* Condition type */ |
| 27 | BT_ASSERT(cond_type); |
| 28 | g_string_append_printf(id, "%s:", cond_type); |
| 29 | |
| 30 | /* Function name: no prefix */ |
| 31 | BT_ASSERT(func); |
| 32 | BT_ASSERT(strstr(func, func_prefix) == func); |
| 33 | src_ch = &func[strlen(func_prefix)]; |
| 34 | |
| 35 | /* Function name: `_` replaced with `-` */ |
| 36 | for (; *src_ch; src_ch++) { |
| 37 | char dst_ch; |
| 38 | |
| 39 | if (*src_ch == '_') { |
| 40 | dst_ch = '-'; |
| 41 | } else { |
| 42 | dst_ch = *src_ch; |
| 43 | } |
| 44 | |
| 45 | g_string_append_c(id, dst_ch); |
| 46 | } |
| 47 | |
| 48 | /* Suffix */ |
| 49 | BT_ASSERT(id_suffix); |
| 50 | g_string_append_printf(id, ":%s", id_suffix); |
| 51 | |
| 52 | return id; |
| 53 | } |
| 54 | |
| 55 | void bt_lib_assert_cond_failed(const char *cond_type, const char *func, |
| 56 | const char *id_suffix, const char *fmt, ...) |
| 57 | { |
| 58 | va_list args; |
| 59 | GString *cond_id = format_cond_id(cond_type, func, id_suffix); |
| 60 | |
| 61 | BT_ASSERT(cond_id); |
| 62 | BT_ASSERT_COND_MSG("Babeltrace 2 library %scondition not satisfied.", |
| 63 | cond_type); |
| 64 | BT_ASSERT_COND_MSG("------------------------------------------------------------------------"); |
| 65 | BT_ASSERT_COND_MSG("Condition ID: `%s`.", cond_id->str); |
| 66 | g_string_free(cond_id, TRUE); |
| 67 | BT_ASSERT_COND_MSG("Function: %s().", func); |
| 68 | BT_ASSERT_COND_MSG("------------------------------------------------------------------------"); |
| 69 | BT_ASSERT_COND_MSG("Error is:"); |
| 70 | va_start(args, fmt); |
| 71 | bt_lib_log_v(__FILE__, __func__, __LINE__, BT_LOG_FATAL, |
| 72 | BT_LOG_TAG, fmt, &args); |
| 73 | va_end(args); |
| 74 | BT_ASSERT_COND_MSG("Aborting..."); |
| 75 | bt_common_abort(); |
| 76 | } |