2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
9 #include "logging/comp-logging.h"
12 void restore_current_thread_error_and_append_exception_chain_recursive(
13 int active_log_level
, PyObject
*py_exc_value
,
14 bt_self_component_class
*self_component_class
,
15 bt_self_component
*self_component
,
16 bt_self_message_iterator
*self_message_iterator
,
17 const char *module_name
)
19 PyObject
*py_exc_cause_value
;
20 PyObject
*py_exc_type
= NULL
;
21 PyObject
*py_exc_tb
= NULL
;
22 PyObject
*py_bt_error_msg
= NULL
;
25 /* If this exception has a (Python) cause, handle that one first. */
26 py_exc_cause_value
= PyException_GetCause(py_exc_value
);
27 if (py_exc_cause_value
) {
28 restore_current_thread_error_and_append_exception_chain_recursive(
29 active_log_level
, py_exc_cause_value
,
30 self_component_class
, self_component
,
31 self_message_iterator
, module_name
);
34 py_exc_tb
= PyException_GetTraceback(py_exc_value
);
36 if (PyErr_GivenExceptionMatches(py_exc_value
, py_mod_bt2_exc_error_type
)) {
38 * If the raised exception is a bt2._Error, restore the wrapped
41 PyObject
*py_error_swig_ptr
;
42 const bt_error
*error
;
46 * We never raise a bt2._Error with a (Python) cause: it should
47 * be the end of the chain.
49 BT_ASSERT(!py_exc_cause_value
);
52 * We steal the error object from the exception, to move
53 * it back as the current thread's error.
55 py_error_swig_ptr
= PyObject_GetAttrString(py_exc_value
, "_ptr");
56 BT_ASSERT(py_error_swig_ptr
);
58 ret
= PyObject_SetAttrString(py_exc_value
, "_ptr", Py_None
);
61 ret
= SWIG_ConvertPtr(py_error_swig_ptr
, (void **) &error
,
62 SWIGTYPE_p_bt_error
, 0);
65 Py_DECREF(py_error_swig_ptr
);
67 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error
);
70 * Append a cause with just the traceback and message, not the
71 * full str() of the bt2._Error. We don't want the causes of
72 * this bt2._Error to be included in the cause we create.
74 gstr
= bt_py_common_format_tb(py_exc_tb
, active_log_level
);
76 /* bt_py_common_format_tb has already warned. */
80 g_string_prepend(gstr
, "Traceback (most recent call last):\n");
82 py_bt_error_msg
= PyObject_GetAttrString(py_exc_value
, "_msg");
83 BT_ASSERT(py_bt_error_msg
);
85 g_string_append_printf(gstr
, "\nbt2._Error: %s",
86 PyUnicode_AsUTF8(py_bt_error_msg
));
88 py_exc_type
= PyObject_Type(py_exc_value
);
90 gstr
= bt_py_common_format_exception(py_exc_type
, py_exc_value
,
91 py_exc_tb
, active_log_level
, false);
93 /* bt_py_common_format_exception has already warned. */
98 if (self_component_class
) {
99 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT_CLASS(
100 self_component_class
, "%s", gstr
->str
);
101 } else if (self_component
) {
102 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(
103 self_component
, "%s", gstr
->str
);
104 } else if (self_message_iterator
) {
105 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_MESSAGE_ITERATOR(
106 self_message_iterator
, "%s", gstr
->str
);
108 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
109 module_name
, "%s", gstr
->str
);
114 g_string_free(gstr
, TRUE
);
117 Py_XDECREF(py_exc_cause_value
);
118 Py_XDECREF(py_exc_type
);
119 Py_XDECREF(py_exc_tb
);
120 Py_XDECREF(py_bt_error_msg
);
124 * If you have the following code:
128 * something_that_raises_bt2_error()
129 * except bt2._Error as e1:
130 * raise ValueError from e1
131 * except ValueError as e2:
132 * raise TypeError from e2
134 * We will have the following exception chain:
136 * TypeError -> ValueError -> bt2._Error
138 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
140 * The bt2._Error contains a `struct bt_error *` that used to be the current
141 * thread's error, at the moment the exception was raised.
143 * This function gets to the bt2._Error and restores the wrapped
144 * `struct bt_error *` as the current thread's error.
146 * Then, for each exception in the chain, starting with the oldest one, it adds
147 * an error cause to the current thread's error.
150 void restore_bt_error_and_append_current_exception_chain(
151 int active_log_level
,
152 bt_self_component_class
*self_component_class
,
153 bt_self_component
*self_component
,
154 bt_self_message_iterator
*self_message_iterator
,
155 const char *module_name
)
157 BT_ASSERT(PyErr_Occurred());
159 /* Used to access and restore the current exception. */
160 PyObject
*py_exc_type
;
161 PyObject
*py_exc_value
;
164 /* Fetch and normalize the Python exception. */
165 PyErr_Fetch(&py_exc_type
, &py_exc_value
, &py_exc_tb
);
166 PyErr_NormalizeException(&py_exc_type
, &py_exc_value
, &py_exc_tb
);
167 BT_ASSERT(py_exc_type
);
168 BT_ASSERT(py_exc_value
);
169 BT_ASSERT(py_exc_tb
);
172 * Set the exception's traceback so it's possible to get it using
173 * PyException_GetTraceback in
174 * restore_current_thread_error_and_append_exception_chain_recursive.
176 PyException_SetTraceback(py_exc_value
, py_exc_tb
);
178 restore_current_thread_error_and_append_exception_chain_recursive(
179 active_log_level
, py_exc_value
, self_component_class
,
180 self_component
, self_message_iterator
, module_name
);
182 PyErr_Restore(py_exc_type
, py_exc_value
, py_exc_tb
);
186 void log_exception_and_maybe_append_cause(
188 int active_log_level
,
190 bt_self_component_class
*self_component_class
,
191 bt_self_component
*self_component
,
192 bt_self_message_iterator
*self_message_iterator
,
193 const char *module_name
)
197 BT_ASSERT(PyErr_Occurred());
198 gstr
= bt_py_common_format_current_exception(active_log_level
);
200 /* bt_py_common_format_current_exception() logs errors */
204 BT_COMP_LOG_CUR_LVL(func_log_level
, active_log_level
, self_component
,
208 restore_bt_error_and_append_current_exception_chain(
209 active_log_level
, self_component_class
, self_component
,
210 self_message_iterator
, module_name
);
216 g_string_free(gstr
, TRUE
);
221 bt_logging_level
get_self_component_log_level(bt_self_component
*self_comp
)
223 return bt_component_get_logging_level(
224 bt_self_component_as_component(self_comp
));
228 bt_logging_level
get_self_message_iterator_log_level(
229 bt_self_message_iterator
*self_msg_iter
)
231 bt_self_component
*self_comp
=
232 bt_self_message_iterator_borrow_component(self_msg_iter
);
234 return get_self_component_log_level(self_comp
);
238 void loge_exception_append_cause_clear(const char *module_name
, int active_log_level
)
240 log_exception_and_maybe_append_cause(BT_LOG_ERROR
, active_log_level
,
241 true, NULL
, NULL
, NULL
, module_name
);
246 void logw_exception_clear(int active_log_level
)
248 log_exception_and_maybe_append_cause(BT_LOG_WARNING
, active_log_level
,
249 false, NULL
, NULL
, NULL
, NULL
);