lib: add thread-safe error reporting API
[babeltrace.git] / src / lib / current-thread.c
CommitLineData
553c4bab
PP
1/*
2 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#define BT_LOG_TAG "LIB/CUR-THREAD"
24#include "lib/logging.h"
25
26#include <babeltrace2/current-thread.h>
27#include <stdint.h>
28#include <stdarg.h>
29
30#include "error.h"
31#include "common/assert.h"
32#include "lib/assert-pre.h"
33#include "lib/func-status.h"
34
35/*
36 * This points to the thread's error object, or it's `NULL` if there's
37 * no current error object.
38 */
39static __thread struct bt_error *thread_error;
40
41const struct bt_error *bt_current_thread_take_error(void)
42{
43 struct bt_error *error = thread_error;
44
45 thread_error = NULL;
46 BT_LOGD("Took current thread's error object: addr=%p",
47 error);
48 return error;
49}
50
51void bt_current_thread_clear_error(void)
52{
53 bt_error_destroy(thread_error);
54 BT_LOGD("Cleared current thread's error object: addr=%p",
55 thread_error);
56 thread_error = NULL;
57}
58
59void bt_current_thread_move_error(const struct bt_error *error)
60{
61 bt_current_thread_clear_error();
62 thread_error = (void *) error;
63 BT_LOGD("Moved error object as current thread's error: addr=%p",
64 thread_error);
65}
66
67/*
68 * Creates the current thread's error object if it does not already
69 * exist.
70 */
71static
72int try_create_thread_error(void)
73{
74 int status = BT_FUNC_STATUS_OK;
75
76 if (thread_error) {
77 goto end;
78 }
79
80 BT_LOGD_STR("Creating current thread's error object.");
81 thread_error = bt_error_create();
82 if (!thread_error) {
83 /* bt_error_create() logs errors */
84 status = BT_FUNC_STATUS_MEMORY_ERROR;
85 goto end;
86 }
87
88 BT_LOGD("Created current thread's error object: addr=%p", thread_error);
89
90end:
91 return status;
92}
93
94enum bt_current_thread_error_append_cause_status
95bt_current_thread_error_append_cause_from_unknown(
96 const char *module_name, const char *file_name,
97 uint64_t line_no, const char *msg_fmt, ...)
98{
99 enum bt_current_thread_error_append_cause_status status =
100 try_create_thread_error();
101 va_list args;
102
103 if (status) {
104 goto end;
105 }
106
107 BT_LOGD("Appending error cause to current thread's error from unknown actor: "
108 "error-addr=%p", thread_error);
109 va_start(args, msg_fmt);
110 status = bt_error_append_cause_from_unknown(thread_error, module_name,
111 file_name, line_no, msg_fmt, args);
112 va_end(args);
113
114end:
115 return status;
116}
117
118enum bt_current_thread_error_append_cause_status
119bt_current_thread_error_append_cause_from_component(
120 bt_self_component *self_comp, const char *file_name,
121 uint64_t line_no, const char *msg_fmt, ...)
122{
123 enum bt_current_thread_error_append_cause_status status =
124 try_create_thread_error();
125 va_list args;
126
127 if (status) {
128 goto end;
129 }
130
131 BT_LOGD("Appending error cause to current thread's error from component: "
132 "error-addr=%p", thread_error);
133 va_start(args, msg_fmt);
134 status = bt_error_append_cause_from_component(thread_error, self_comp,
135 file_name, line_no, msg_fmt, args);
136 va_end(args);
137
138end:
139 return status;
140}
141
142enum bt_current_thread_error_append_cause_status
143bt_current_thread_error_append_cause_from_component_class(
144 bt_self_component_class *self_comp_class, const char *file_name,
145 uint64_t line_no, const char *msg_fmt, ...)
146{
147 enum bt_current_thread_error_append_cause_status status =
148 try_create_thread_error();
149 va_list args;
150
151 if (status) {
152 goto end;
153 }
154
155 BT_LOGD("Appending error cause to current thread's error from component class actor: "
156 "error-addr=%p", thread_error);
157 va_start(args, msg_fmt);
158 status = bt_error_append_cause_from_component_class(thread_error,
159 self_comp_class, file_name, line_no, msg_fmt, args);
160 va_end(args);
161
162end:
163 return status;
164}
165
166enum bt_current_thread_error_append_cause_status
167bt_current_thread_error_append_cause_from_message_iterator(
168 bt_self_message_iterator *self_iter, const char *file_name,
169 uint64_t line_no, const char *msg_fmt, ...)
170{
171 enum bt_current_thread_error_append_cause_status status =
172 try_create_thread_error();
173 va_list args;
174
175 if (status) {
176 goto end;
177 }
178
179 BT_LOGD("Appending error cause to current thread's error from message iterator actor: "
180 "error-addr=%p", thread_error);
181 va_start(args, msg_fmt);
182 status = bt_error_append_cause_from_message_iterator(thread_error,
183 self_iter, file_name, line_no, msg_fmt, args);
184 va_end(args);
185
186end:
187 return status;
188}
This page took 0.030027 seconds and 4 git commands to generate.