Fix: lib: pass down API function name to some helpers
[babeltrace.git] / src / lib / assert-cond.c
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 "common/macros.h"
15 #include "assert-cond-base.h"
16
17 static
18 GString *format_cond_id(const char *cond_type, const char *func,
19 const char *id_suffix)
20 {
21 static const char func_prefix[] = "bt_";
22 GString *id = g_string_new(NULL);
23 const char *src_ch;
24
25 BT_ASSERT(id);
26
27 /* Condition type */
28 BT_ASSERT(cond_type);
29 g_string_append_printf(id, "%s:", cond_type);
30
31 /* Function name: no prefix */
32 BT_ASSERT(func);
33 BT_ASSERT(strstr(func, func_prefix) == func);
34 src_ch = &func[strlen(func_prefix)];
35
36 /* Function name: `_` replaced with `-` */
37 for (; *src_ch; src_ch++) {
38 char dst_ch;
39
40 if (*src_ch == '_') {
41 dst_ch = '-';
42 } else {
43 dst_ch = *src_ch;
44 }
45
46 g_string_append_c(id, dst_ch);
47 }
48
49 /* Suffix */
50 BT_ASSERT(id_suffix);
51 g_string_append_printf(id, ":%s", id_suffix);
52
53 return id;
54 }
55
56 BT_HIDDEN
57 void bt_lib_assert_cond_failed(const char *cond_type, const char *func,
58 const char *id_suffix, const char *fmt, ...)
59 {
60 va_list args;
61 GString *cond_id = format_cond_id(cond_type, func, id_suffix);
62
63 BT_ASSERT(cond_id);
64 BT_ASSERT_COND_MSG("Babeltrace 2 library %scondition not satisfied.",
65 cond_type);
66 BT_ASSERT_COND_MSG("------------------------------------------------------------------------");
67 BT_ASSERT_COND_MSG("Condition ID: `%s`.", cond_id->str);
68 g_string_free(cond_id, TRUE);
69 BT_ASSERT_COND_MSG("Function: %s().", func);
70 BT_ASSERT_COND_MSG("------------------------------------------------------------------------");
71 BT_ASSERT_COND_MSG("Error is:");
72 va_start(args, fmt);
73 bt_lib_log_v(_BT_LOG_SRCLOC_FUNCTION, __FILE__, __LINE__, BT_LOG_FATAL,
74 BT_LOG_TAG, fmt, &args);
75 va_end(args);
76 BT_ASSERT_COND_MSG("Aborting...");
77 bt_common_abort();
78 }
This page took 0.031126 seconds and 4 git commands to generate.