lib: add bt_lib_log_v()
[babeltrace.git] / src / lib / current-thread.c
CommitLineData
553c4bab 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
553c4bab 3 *
0235b0db 4 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
553c4bab
PP
5 */
6
7#define BT_LOG_TAG "LIB/CUR-THREAD"
8#include "lib/logging.h"
9
4fa90f32 10#include <babeltrace2/babeltrace.h>
553c4bab
PP
11#include <stdint.h>
12#include <stdarg.h>
13
14#include "error.h"
15#include "common/assert.h"
d98421f2 16#include "lib/assert-cond.h"
553c4bab
PP
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 */
23static __thread struct bt_error *thread_error;
24
25const 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
35void 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
43void bt_current_thread_move_error(const struct bt_error *error)
44{
d5b13b9b 45 BT_ASSERT_PRE_ERROR_NON_NULL(error);
553c4bab
PP
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 */
56static
57int 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
75end:
76 return status;
77}
78
79enum bt_current_thread_error_append_cause_status
80bt_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
c77d03eb
PP
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
553c4bab
PP
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
103end:
104 return status;
105}
106
107enum bt_current_thread_error_append_cause_status
108bt_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
d5b13b9b 116 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
c77d03eb
PP
117 BT_ASSERT_PRE_NON_NULL(file_name, "File name");
118 BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string");
119
553c4bab
PP
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
131end:
132 return status;
133}
134
135enum bt_current_thread_error_append_cause_status
136bt_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
d5b13b9b 144 BT_ASSERT_PRE_COMP_CLS_NON_NULL(self_comp_class);
c77d03eb
PP
145 BT_ASSERT_PRE_NON_NULL(file_name, "File name");
146 BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string");
147
553c4bab
PP
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
159end:
160 return status;
161}
162
163enum bt_current_thread_error_append_cause_status
164bt_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
d5b13b9b 172 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_iter);
c77d03eb
PP
173 BT_ASSERT_PRE_NON_NULL(file_name, "File name");
174 BT_ASSERT_PRE_NON_NULL(msg_fmt, "Message format string");
175
553c4bab
PP
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
187end:
188 return status;
189}
This page took 0.05168 seconds and 4 git commands to generate.