lib: merge `assert-pre.h` and `assert-post.h` into `assert-cond.h`
[babeltrace.git] / src / lib / current-thread.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_LOG_TAG "LIB/CUR-THREAD"
8 #include "lib/logging.h"
9
10 #include <babeltrace2/babeltrace.h>
11 #include <stdint.h>
12 #include <stdarg.h>
13
14 #include "error.h"
15 #include "common/assert.h"
16 #include "lib/assert-cond.h"
17 #include "lib/func-status.h"
18
19 /*
20 * This points to the thread's error object, or it's `NULL` if there's
21 * no current error object.
22 */
23 static __thread struct bt_error *thread_error;
24
25 const struct bt_error *bt_current_thread_take_error(void)
26 {
27 struct bt_error *error = thread_error;
28
29 thread_error = NULL;
30 BT_LOGD("Took current thread's error object: addr=%p",
31 error);
32 return error;
33 }
34
35 void bt_current_thread_clear_error(void)
36 {
37 bt_error_destroy(thread_error);
38 BT_LOGD("Cleared current thread's error object: addr=%p",
39 thread_error);
40 thread_error = NULL;
41 }
42
43 void bt_current_thread_move_error(const struct bt_error *error)
44 {
45 BT_ASSERT_PRE_NON_NULL(error, "Error");
46 bt_current_thread_clear_error();
47 thread_error = (void *) error;
48 BT_LOGD("Moved error object as current thread's error: addr=%p",
49 thread_error);
50 }
51
52 /*
53 * Creates the current thread's error object if it does not already
54 * exist.
55 */
56 static
57 int try_create_thread_error(void)
58 {
59 int status = BT_FUNC_STATUS_OK;
60
61 if (thread_error) {
62 goto end;
63 }
64
65 BT_LOGD_STR("Creating current thread's error object.");
66 thread_error = bt_error_create();
67 if (!thread_error) {
68 /* bt_error_create() logs errors */
69 status = BT_FUNC_STATUS_MEMORY_ERROR;
70 goto end;
71 }
72
73 BT_LOGD("Created current thread's error object: addr=%p", thread_error);
74
75 end:
76 return status;
77 }
78
79 enum bt_current_thread_error_append_cause_status
80 bt_current_thread_error_append_cause_from_unknown(
81 const char *module_name, const char *file_name,
82 uint64_t line_no, const char *msg_fmt, ...)
83 {
84 enum bt_current_thread_error_append_cause_status status =
85 try_create_thread_error();
86 va_list args;
87
88 BT_ASSERT_PRE_NON_NULL(module_name, "Module name");
89 BT_ASSERT_PRE_NON_NULL(file_name, "File name");
90 BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string");
91
92 if (status) {
93 goto end;
94 }
95
96 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
97 "error-addr=%p", thread_error);
98 va_start(args, msg_fmt);
99 status = bt_error_append_cause_from_unknown(thread_error, module_name,
100 file_name, line_no, msg_fmt, args);
101 va_end(args);
102
103 end:
104 return status;
105 }
106
107 enum bt_current_thread_error_append_cause_status
108 bt_current_thread_error_append_cause_from_component(
109 bt_self_component *self_comp, const char *file_name,
110 uint64_t line_no, const char *msg_fmt, ...)
111 {
112 enum bt_current_thread_error_append_cause_status status =
113 try_create_thread_error();
114 va_list args;
115
116 BT_ASSERT_PRE_NON_NULL(self_comp, "Component");
117 BT_ASSERT_PRE_NON_NULL(file_name, "File name");
118 BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string");
119
120 if (status) {
121 goto end;
122 }
123
124 BT_LOGD("Appending error cause to current thread's error from component: "
125 "error-addr=%p", thread_error);
126 va_start(args, msg_fmt);
127 status = bt_error_append_cause_from_component(thread_error, self_comp,
128 file_name, line_no, msg_fmt, args);
129 va_end(args);
130
131 end:
132 return status;
133 }
134
135 enum bt_current_thread_error_append_cause_status
136 bt_current_thread_error_append_cause_from_component_class(
137 bt_self_component_class *self_comp_class, const char *file_name,
138 uint64_t line_no, const char *msg_fmt, ...)
139 {
140 enum bt_current_thread_error_append_cause_status status =
141 try_create_thread_error();
142 va_list args;
143
144 BT_ASSERT_PRE_NON_NULL(self_comp_class, "Component class");
145 BT_ASSERT_PRE_NON_NULL(file_name, "File name");
146 BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string");
147
148 if (status) {
149 goto end;
150 }
151
152 BT_LOGD("Appending error cause to current thread's error from component class actor: "
153 "error-addr=%p", thread_error);
154 va_start(args, msg_fmt);
155 status = bt_error_append_cause_from_component_class(thread_error,
156 self_comp_class, file_name, line_no, msg_fmt, args);
157 va_end(args);
158
159 end:
160 return status;
161 }
162
163 enum bt_current_thread_error_append_cause_status
164 bt_current_thread_error_append_cause_from_message_iterator(
165 bt_self_message_iterator *self_iter, const char *file_name,
166 uint64_t line_no, const char *msg_fmt, ...)
167 {
168 enum bt_current_thread_error_append_cause_status status =
169 try_create_thread_error();
170 va_list args;
171
172 BT_ASSERT_PRE_NON_NULL(self_iter, "Message iterator");
173 BT_ASSERT_PRE_NON_NULL(file_name, "File name");
174 BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string");
175
176 if (status) {
177 goto end;
178 }
179
180 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
181 "error-addr=%p", thread_error);
182 va_start(args, msg_fmt);
183 status = bt_error_append_cause_from_message_iterator(thread_error,
184 self_iter, file_name, line_no, msg_fmt, args);
185 va_end(args);
186
187 end:
188 return status;
189 }
This page took 0.03209 seconds and 4 git commands to generate.