lib: remove unused includes
[babeltrace.git] / src / lib / assert-cond.c
CommitLineData
1778c2a4
PP
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"
5a3fec55 14#include "common/common.h"
1778c2a4
PP
15#include "assert-cond-base.h"
16
17static
18GString *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
1778c2a4
PP
56void bt_lib_assert_cond_failed(const char *cond_type, const char *func,
57 const char *id_suffix, const char *fmt, ...)
58{
59 va_list args;
60 GString *cond_id = format_cond_id(cond_type, func, id_suffix);
61
62 BT_ASSERT(cond_id);
63 BT_ASSERT_COND_MSG("Babeltrace 2 library %scondition not satisfied.",
64 cond_type);
65 BT_ASSERT_COND_MSG("------------------------------------------------------------------------");
66 BT_ASSERT_COND_MSG("Condition ID: `%s`.", cond_id->str);
67 g_string_free(cond_id, TRUE);
68 BT_ASSERT_COND_MSG("Function: %s().", func);
69 BT_ASSERT_COND_MSG("------------------------------------------------------------------------");
70 BT_ASSERT_COND_MSG("Error is:");
71 va_start(args, fmt);
71436ae4 72 bt_lib_log_v(__FILE__, __func__, __LINE__, BT_LOG_FATAL,
1778c2a4
PP
73 BT_LOG_TAG, fmt, &args);
74 va_end(args);
75 BT_ASSERT_COND_MSG("Aborting...");
76 bt_common_abort();
77}
This page took 0.04416 seconds and 4 git commands to generate.