2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
7 #ifndef BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_LOG_AND_APPEND_ERROR_H
8 #define BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_LOG_AND_APPEND_ERROR_H
12 #include "logging/comp-logging.h"
15 void restore_current_thread_error_and_append_exception_chain_recursive(
16 int active_log_level
, PyObject
*py_exc_value
,
17 bt_self_component_class
*self_component_class
,
18 bt_self_component
*self_component
,
19 bt_self_message_iterator
*self_message_iterator
,
20 const char *module_name
)
22 PyObject
*py_exc_cause_value
;
23 PyObject
*py_exc_type
= NULL
;
24 PyObject
*py_exc_tb
= NULL
;
25 PyObject
*py_bt_error_msg
= NULL
;
28 /* If this exception has a (Python) cause, handle that one first. */
29 py_exc_cause_value
= PyException_GetCause(py_exc_value
);
30 if (py_exc_cause_value
) {
31 restore_current_thread_error_and_append_exception_chain_recursive(
32 active_log_level
, py_exc_cause_value
,
33 self_component_class
, self_component
,
34 self_message_iterator
, module_name
);
37 py_exc_tb
= PyException_GetTraceback(py_exc_value
);
39 if (PyErr_GivenExceptionMatches(py_exc_value
, py_mod_bt2_exc_error_type
)) {
41 * If the raised exception is a bt2._Error, restore the wrapped
44 PyObject
*py_error_swig_ptr
;
45 const bt_error
*error
;
49 * We never raise a bt2._Error with a (Python) cause: it should
50 * be the end of the chain.
52 BT_ASSERT(!py_exc_cause_value
);
55 * We steal the error object from the exception, to move
56 * it back as the current thread's error.
58 py_error_swig_ptr
= PyObject_GetAttrString(py_exc_value
, "_ptr");
59 BT_ASSERT(py_error_swig_ptr
);
61 ret
= PyObject_SetAttrString(py_exc_value
, "_ptr", Py_None
);
64 ret
= SWIG_ConvertPtr(py_error_swig_ptr
, (void **) &error
,
65 SWIGTYPE_p_bt_error
, 0);
68 Py_DECREF(py_error_swig_ptr
);
70 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error
);
73 * Append a cause with just the traceback and message, not the
74 * full str() of the bt2._Error. We don't want the causes of
75 * this bt2._Error to be included in the cause we create.
77 gstr
= bt_py_common_format_tb(py_exc_tb
, active_log_level
);
79 /* bt_py_common_format_tb has already warned. */
83 g_string_prepend(gstr
, "Traceback (most recent call last):\n");
85 py_bt_error_msg
= PyObject_GetAttrString(py_exc_value
, "_msg");
86 BT_ASSERT(py_bt_error_msg
);
88 g_string_append_printf(gstr
, "\nbt2._Error: %s",
89 PyUnicode_AsUTF8(py_bt_error_msg
));
91 py_exc_type
= PyObject_Type(py_exc_value
);
93 gstr
= bt_py_common_format_exception(py_exc_type
, py_exc_value
,
94 py_exc_tb
, active_log_level
, false);
96 /* bt_py_common_format_exception has already warned. */
101 if (self_component_class
) {
102 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
103 self_component_class
, "%s", gstr
->str
);
104 } else if (self_component
) {
105 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
106 self_component
, "%s", gstr
->str
);
107 } else if (self_message_iterator
) {
108 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
109 self_message_iterator
, "%s", gstr
->str
);
111 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
112 module_name
, "%s", gstr
->str
);
117 g_string_free(gstr
, TRUE
);
120 Py_XDECREF(py_exc_cause_value
);
121 Py_XDECREF(py_exc_type
);
122 Py_XDECREF(py_exc_tb
);
123 Py_XDECREF(py_bt_error_msg
);
127 * If you have the following code:
131 * something_that_raises_bt2_error()
132 * except bt2._Error as e1:
133 * raise ValueError from e1
134 * except ValueError as e2:
135 * raise TypeError from e2
137 * We will have the following exception chain:
139 * TypeError -> ValueError -> bt2._Error
141 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
143 * The bt2._Error contains a `struct bt_error *` that used to be the current
144 * thread's error, at the moment the exception was raised.
146 * This function gets to the bt2._Error and restores the wrapped
147 * `struct bt_error *` as the current thread's error.
149 * Then, for each exception in the chain, starting with the oldest one, it adds
150 * an error cause to the current thread's error.
153 void restore_bt_error_and_append_current_exception_chain(
154 int active_log_level
,
155 bt_self_component_class
*self_component_class
,
156 bt_self_component
*self_component
,
157 bt_self_message_iterator
*self_message_iterator
,
158 const char *module_name
)
160 BT_ASSERT(PyErr_Occurred());
162 /* Used to access and restore the current exception. */
163 PyObject
*py_exc_type
;
164 PyObject
*py_exc_value
;
167 /* Fetch and normalize the Python exception. */
168 PyErr_Fetch(&py_exc_type
, &py_exc_value
, &py_exc_tb
);
169 PyErr_NormalizeException(&py_exc_type
, &py_exc_value
, &py_exc_tb
);
170 BT_ASSERT(py_exc_type
);
171 BT_ASSERT(py_exc_value
);
172 BT_ASSERT(py_exc_tb
);
175 * Set the exception's traceback so it's possible to get it using
176 * PyException_GetTraceback in
177 * restore_current_thread_error_and_append_exception_chain_recursive.
179 PyException_SetTraceback(py_exc_value
, py_exc_tb
);
181 restore_current_thread_error_and_append_exception_chain_recursive(
182 active_log_level
, py_exc_value
, self_component_class
,
183 self_component
, self_message_iterator
, module_name
);
185 PyErr_Restore(py_exc_type
, py_exc_value
, py_exc_tb
);
189 void log_exception_and_maybe_append_cause(
191 int active_log_level
,
193 bt_self_component_class
*self_component_class
,
194 bt_self_component
*self_component
,
195 bt_self_message_iterator
*self_message_iterator
,
196 const char *module_name
)
200 BT_ASSERT(PyErr_Occurred());
201 gstr
= bt_py_common_format_current_exception(active_log_level
);
203 /* bt_py_common_format_current_exception() logs errors */
207 BT_COMP_LOG_CUR_LVL(func_log_level
, active_log_level
, self_component
,
211 restore_bt_error_and_append_current_exception_chain(
212 active_log_level
, self_component_class
, self_component
,
213 self_message_iterator
, module_name
);
219 g_string_free(gstr
, TRUE
);
224 bt_logging_level
get_self_component_log_level(bt_self_component
*self_comp
)
226 return bt_component_get_logging_level(
227 bt_self_component_as_component(self_comp
));
231 bt_logging_level
get_self_message_iterator_log_level(
232 bt_self_message_iterator
*self_msg_iter
)
234 bt_self_component
*self_comp
=
235 bt_self_message_iterator_borrow_component(self_msg_iter
);
237 return get_self_component_log_level(self_comp
);
241 void loge_exception_append_cause_clear(const char *module_name
, int active_log_level
)
243 log_exception_and_maybe_append_cause(BT_LOG_ERROR
, active_log_level
,
244 true, NULL
, NULL
, NULL
, module_name
);
249 void logw_exception_clear(int active_log_level
)
251 log_exception_and_maybe_append_cause(BT_LOG_WARNING
, active_log_level
,
252 false, NULL
, NULL
, NULL
, NULL
);
256 #endif /* BABELTRACE_BINDINGS_PYTHON_BT2_BT2_NATIVE_BT_LOG_AND_APPEND_ERROR_H */