Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / bindings / python / bt2 / bt2 / native_bt_log_and_append_error.h
1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include <stdbool.h>
26
27 #include "logging/comp-logging.h"
28
29 static
30 void restore_current_thread_error_and_append_exception_chain_recursive(
31 int active_log_level, PyObject *py_exc_value,
32 bt_self_component_class *self_component_class,
33 bt_self_component *self_component,
34 bt_self_message_iterator *self_message_iterator,
35 const char *module_name)
36 {
37 PyObject *py_exc_cause_value;
38 PyObject *py_exc_type = NULL;
39 PyObject *py_exc_tb = NULL;
40 GString *gstr = NULL;
41
42 /* If this exception has a cause, handle that one first. */
43 py_exc_cause_value = PyException_GetCause(py_exc_value);
44 if (py_exc_cause_value) {
45 restore_current_thread_error_and_append_exception_chain_recursive(
46 active_log_level, py_exc_cause_value,
47 self_component_class, self_component,
48 self_message_iterator, module_name);
49 }
50
51 /*
52 * If the raised exception is a bt2._Error, restore the wrapped error.
53 */
54 if (PyErr_GivenExceptionMatches(py_exc_value, py_mod_bt2_exc_error_type)) {
55 PyObject *py_error_swig_ptr;
56 const bt_error *error;
57 int ret;
58
59 /*
60 * We never raise a bt2._Error with a cause: it should be the
61 * end of the chain.
62 */
63 BT_ASSERT(!py_exc_cause_value);
64
65 /*
66 * We steal the error object from the exception, to move
67 * it back as the current thread's error.
68 */
69 py_error_swig_ptr = PyObject_GetAttrString(py_exc_value, "_ptr");
70 BT_ASSERT(py_error_swig_ptr);
71
72 ret = PyObject_SetAttrString(py_exc_value, "_ptr", Py_None);
73 BT_ASSERT(ret == 0);
74
75 ret = SWIG_ConvertPtr(py_error_swig_ptr, (void **) &error,
76 SWIGTYPE_p_bt_error, 0);
77 BT_ASSERT(ret == 0);
78
79 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error);
80
81 Py_DECREF(py_error_swig_ptr);
82 }
83
84 py_exc_type = PyObject_Type(py_exc_value);
85 py_exc_tb = PyException_GetTraceback(py_exc_value);
86
87 gstr = bt_py_common_format_exception(py_exc_type, py_exc_value,
88 py_exc_tb, active_log_level, false);
89 if (!gstr) {
90 /* bt_py_common_format_exception has already warned. */
91 goto end;
92 }
93
94 if (self_component_class) {
95 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
96 self_component_class, "%s", gstr->str);
97 } else if (self_component) {
98 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
99 self_component, "%s", gstr->str);
100 } else if (self_message_iterator) {
101 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
102 self_message_iterator, "%s", gstr->str);
103 } else {
104 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
105 module_name, "%s", gstr->str);
106 }
107
108 end:
109 if (gstr) {
110 g_string_free(gstr, TRUE);
111 }
112
113 Py_XDECREF(py_exc_cause_value);
114 Py_XDECREF(py_exc_type);
115 Py_XDECREF(py_exc_tb);
116 }
117
118 /*
119 * If you have the following code:
120 *
121 * try:
122 * try:
123 * something_that_raises_bt2_error()
124 * except bt2._Error as e1:
125 * raise ValueError from e1
126 * except ValueError as e2:
127 * raise TypeError from e2
128 *
129 * We will have the following exception chain:
130 *
131 * TypeError -> ValueError -> bt2._Error
132 *
133 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
134 *
135 * The bt2._Error contains a `struct bt_error *` that used to be the current
136 * thread's error, at the moment the exception was raised.
137 *
138 * This function gets to the bt2._Error and restores the wrapped
139 * `struct bt_error *` as the current thread's error.
140 *
141 * Then, for each exception in the chain, starting with the oldest one, it adds
142 * an error cause to the current thread's error.
143 */
144 static
145 void restore_bt_error_and_append_current_exception_chain(
146 int active_log_level,
147 bt_self_component_class *self_component_class,
148 bt_self_component *self_component,
149 bt_self_message_iterator *self_message_iterator,
150 const char *module_name)
151 {
152 BT_ASSERT(PyErr_Occurred());
153
154 /* Used to access and restore the current exception. */
155 PyObject *py_exc_type;
156 PyObject *py_exc_value;
157 PyObject *py_exc_tb;
158
159 /* Fetch and normalize the Python exception. */
160 PyErr_Fetch(&py_exc_type, &py_exc_value, &py_exc_tb);
161 PyErr_NormalizeException(&py_exc_type, &py_exc_value, &py_exc_tb);
162 BT_ASSERT(py_exc_type);
163 BT_ASSERT(py_exc_value);
164 BT_ASSERT(py_exc_tb);
165
166 /*
167 * Set the exception's traceback so it's possible to get it using
168 * PyException_GetTraceback in
169 * restore_current_thread_error_and_append_exception_chain_recursive.
170 */
171 PyException_SetTraceback(py_exc_value, py_exc_tb);
172
173 restore_current_thread_error_and_append_exception_chain_recursive(
174 active_log_level, py_exc_value, self_component_class,
175 self_component, self_message_iterator, module_name);
176
177 PyErr_Restore(py_exc_type, py_exc_value, py_exc_tb);
178 }
179
180 static inline
181 void log_exception_and_maybe_append_cause(
182 int func_log_level,
183 int active_log_level,
184 bool append_error,
185 bt_self_component_class *self_component_class,
186 bt_self_component *self_component,
187 bt_self_message_iterator *self_message_iterator,
188 const char *module_name)
189 {
190 GString *gstr;
191
192 BT_ASSERT(PyErr_Occurred());
193 gstr = bt_py_common_format_current_exception(active_log_level);
194 if (!gstr) {
195 /* bt_py_common_format_current_exception() logs errors */
196 goto end;
197 }
198
199 BT_COMP_LOG_CUR_LVL(func_log_level, active_log_level, self_component,
200 "%s", gstr->str);
201
202 if (append_error) {
203 restore_bt_error_and_append_current_exception_chain(
204 active_log_level, self_component_class, self_component,
205 self_message_iterator, module_name);
206
207 }
208
209 end:
210 if (gstr) {
211 g_string_free(gstr, TRUE);
212 }
213 }
214
215 static
216 bt_logging_level get_self_component_log_level(bt_self_component *self_comp)
217 {
218 return bt_component_get_logging_level(
219 bt_self_component_as_component(self_comp));
220 }
221
222 static
223 bt_logging_level get_self_message_iterator_log_level(
224 bt_self_message_iterator *self_msg_iter)
225 {
226 bt_self_component *self_comp =
227 bt_self_message_iterator_borrow_component(self_msg_iter);
228
229 return get_self_component_log_level(self_comp);
230 }
231
232 static inline
233 void loge_exception_append_cause_clear(const char *module_name, int active_log_level)
234 {
235 log_exception_and_maybe_append_cause(BT_LOG_ERROR, active_log_level,
236 true, NULL, NULL, NULL, module_name);
237 PyErr_Clear();
238 }
239
240 static inline
241 void logw_exception_clear(int active_log_level)
242 {
243 log_exception_and_maybe_append_cause(BT_LOG_WARNING, active_log_level,
244 false, NULL, NULL, NULL, NULL);
245 PyErr_Clear();
246 }
This page took 0.038434 seconds and 4 git commands to generate.