cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[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 "lib/assert-cond.h"
16 #include "lib/func-status.h"
17
18 #define BT_ASSERT_PRE_FILE_NAME_NON_NULL(_file_name) \
19 BT_ASSERT_PRE_NON_NULL("file-name", (_file_name), "File name");
20
21 #define BT_ASSERT_PRE_MSG_FMT_NON_NULL(_msg_fmt) \
22 BT_ASSERT_PRE_NON_NULL("message-format", (_msg_fmt), "Message format");
23
24 /*
25 * This points to the thread's error object, or it's `NULL` if there's
26 * no current error object.
27 */
28 static __thread struct bt_error *thread_error;
29
30 BT_EXPORT
31 const struct bt_error *bt_current_thread_take_error(void)
32 {
33 struct bt_error *error = thread_error;
34
35 thread_error = NULL;
36 BT_LOGD("Took current thread's error object: addr=%p",
37 error);
38 return error;
39 }
40
41 BT_EXPORT
42 void bt_current_thread_clear_error(void)
43 {
44 bt_error_destroy(thread_error);
45 BT_LOGD("Cleared current thread's error object: addr=%p",
46 thread_error);
47 thread_error = NULL;
48 }
49
50 BT_EXPORT
51 void bt_current_thread_move_error(const struct bt_error *error)
52 {
53 BT_ASSERT_PRE_ERROR_NON_NULL(error);
54 bt_current_thread_clear_error();
55 thread_error = (void *) error;
56 BT_LOGD("Moved error object as current thread's error: addr=%p",
57 thread_error);
58 }
59
60 /*
61 * Creates the current thread's error object if it does not already
62 * exist.
63 */
64 static
65 int try_create_thread_error(void)
66 {
67 int status = BT_FUNC_STATUS_OK;
68
69 if (thread_error) {
70 goto end;
71 }
72
73 BT_LOGD_STR("Creating current thread's error object.");
74 thread_error = bt_error_create();
75 if (!thread_error) {
76 /* bt_error_create() logs errors */
77 status = BT_FUNC_STATUS_MEMORY_ERROR;
78 goto end;
79 }
80
81 BT_LOGD("Created current thread's error object: addr=%p", thread_error);
82
83 end:
84 return status;
85 }
86
87 BT_EXPORT
88 enum bt_current_thread_error_append_cause_status
89 bt_current_thread_error_append_cause_from_unknown(
90 const char *module_name, const char *file_name,
91 uint64_t line_no, const char *msg_fmt, ...)
92 {
93 enum bt_current_thread_error_append_cause_status status =
94 try_create_thread_error();
95 va_list args;
96
97 BT_ASSERT_PRE_NON_NULL("module-name", module_name, "Module name");
98 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name);
99 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt);
100
101 if (status) {
102 goto end;
103 }
104
105 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
106 "error-addr=%p", thread_error);
107 va_start(args, msg_fmt);
108 status = bt_error_append_cause_from_unknown(thread_error, module_name,
109 file_name, line_no, msg_fmt, args);
110 va_end(args);
111
112 end:
113 return status;
114 }
115
116 BT_EXPORT
117 enum bt_current_thread_error_append_cause_status
118 bt_current_thread_error_append_cause_from_component(
119 bt_self_component *self_comp, const char *file_name,
120 uint64_t line_no, const char *msg_fmt, ...)
121 {
122 enum bt_current_thread_error_append_cause_status status =
123 try_create_thread_error();
124 va_list args;
125
126 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
127 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name);
128 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt);
129
130 if (status) {
131 goto end;
132 }
133
134 BT_LOGD("Appending error cause to current thread's error from component: "
135 "error-addr=%p", thread_error);
136 va_start(args, msg_fmt);
137 status = bt_error_append_cause_from_component(thread_error, self_comp,
138 file_name, line_no, msg_fmt, args);
139 va_end(args);
140
141 end:
142 return status;
143 }
144
145 BT_EXPORT
146 enum bt_current_thread_error_append_cause_status
147 bt_current_thread_error_append_cause_from_component_class(
148 bt_self_component_class *self_comp_class, const char *file_name,
149 uint64_t line_no, const char *msg_fmt, ...)
150 {
151 enum bt_current_thread_error_append_cause_status status =
152 try_create_thread_error();
153 va_list args;
154
155 BT_ASSERT_PRE_COMP_CLS_NON_NULL(self_comp_class);
156 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name);
157 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt);
158
159 if (status) {
160 goto end;
161 }
162
163 BT_LOGD("Appending error cause to current thread's error from component class actor: "
164 "error-addr=%p", thread_error);
165 va_start(args, msg_fmt);
166 status = bt_error_append_cause_from_component_class(thread_error,
167 self_comp_class, file_name, line_no, msg_fmt, args);
168 va_end(args);
169
170 end:
171 return status;
172 }
173
174 BT_EXPORT
175 enum bt_current_thread_error_append_cause_status
176 bt_current_thread_error_append_cause_from_message_iterator(
177 bt_self_message_iterator *self_iter, const char *file_name,
178 uint64_t line_no, const char *msg_fmt, ...)
179 {
180 enum bt_current_thread_error_append_cause_status status =
181 try_create_thread_error();
182 va_list args;
183
184 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_iter);
185 BT_ASSERT_PRE_FILE_NAME_NON_NULL(file_name);
186 BT_ASSERT_PRE_MSG_FMT_NON_NULL(msg_fmt);
187
188 if (status) {
189 goto end;
190 }
191
192 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
193 "error-addr=%p", thread_error);
194 va_start(args, msg_fmt);
195 status = bt_error_append_cause_from_message_iterator(thread_error,
196 self_iter, file_name, line_no, msg_fmt, args);
197 va_end(args);
198
199 end:
200 return status;
201 }
This page took 0.032931 seconds and 4 git commands to generate.