2 * The MIT License (MIT)
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
27 #include "logging/comp-logging.h"
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
)
37 PyObject
*py_exc_cause_value
;
38 PyObject
*py_exc_type
= NULL
;
39 PyObject
*py_exc_tb
= NULL
;
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
);
52 * If the raised exception is a bt2._Error, restore the wrapped error.
54 if (PyErr_GivenExceptionMatches(py_exc_value
, py_mod_bt2_exc_error_type
)) {
55 PyObject
*py_error_swig_ptr
;
56 const bt_error
*error
;
60 * We never raise a bt2._Error with a cause: it should be the
63 BT_ASSERT(!py_exc_cause_value
);
66 * We steal the error object from the exception, to move
67 * it back as the current thread's error.
69 py_error_swig_ptr
= PyObject_GetAttrString(py_exc_value
, "_ptr");
70 BT_ASSERT(py_error_swig_ptr
);
72 ret
= PyObject_SetAttrString(py_exc_value
, "_ptr", Py_None
);
75 ret
= SWIG_ConvertPtr(py_error_swig_ptr
, (void **) &error
,
76 SWIGTYPE_p_bt_error
, 0);
79 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(error
);
81 Py_DECREF(py_error_swig_ptr
);
84 py_exc_type
= PyObject_Type(py_exc_value
);
85 py_exc_tb
= PyException_GetTraceback(py_exc_value
);
87 gstr
= bt_py_common_format_exception(py_exc_type
, py_exc_value
,
88 py_exc_tb
, active_log_level
, false);
90 /* bt_py_common_format_exception has already warned. */
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
);
104 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
105 module_name
, "%s", gstr
->str
);
110 g_string_free(gstr
, TRUE
);
113 Py_XDECREF(py_exc_cause_value
);
114 Py_XDECREF(py_exc_type
);
115 Py_XDECREF(py_exc_tb
);
119 * If you have the following code:
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
129 * We will have the following exception chain:
131 * TypeError -> ValueError -> bt2._Error
133 * Where the TypeError is the current exception (obtained from PyErr_Fetch).
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.
138 * This function gets to the bt2._Error and restores the wrapped
139 * `struct bt_error *` as the current thread's error.
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.
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
)
152 BT_ASSERT(PyErr_Occurred());
154 /* Used to access and restore the current exception. */
155 PyObject
*py_exc_type
;
156 PyObject
*py_exc_value
;
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
);
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.
171 PyException_SetTraceback(py_exc_value
, py_exc_tb
);
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
);
177 PyErr_Restore(py_exc_type
, py_exc_value
, py_exc_tb
);
181 void log_exception_and_maybe_append_cause(
183 int active_log_level
,
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
)
192 BT_ASSERT(PyErr_Occurred());
193 gstr
= bt_py_common_format_current_exception(active_log_level
);
195 /* bt_py_common_format_current_exception() logs errors */
199 BT_COMP_LOG_CUR_LVL(func_log_level
, active_log_level
, self_component
,
203 restore_bt_error_and_append_current_exception_chain(
204 active_log_level
, self_component_class
, self_component
,
205 self_message_iterator
, module_name
);
211 g_string_free(gstr
, TRUE
);
216 bt_logging_level
get_self_component_log_level(bt_self_component
*self_comp
)
218 return bt_component_get_logging_level(
219 bt_self_component_as_component(self_comp
));
223 bt_logging_level
get_self_message_iterator_log_level(
224 bt_self_message_iterator
*self_msg_iter
)
226 bt_self_component
*self_comp
=
227 bt_self_message_iterator_borrow_component(self_msg_iter
);
229 return get_self_component_log_level(self_comp
);
233 void loge_exception_append_cause_clear(const char *module_name
, int active_log_level
)
235 log_exception_and_maybe_append_cause(BT_LOG_ERROR
, active_log_level
,
236 true, NULL
, NULL
, NULL
, module_name
);
241 void logw_exception_clear(int active_log_level
)
243 log_exception_and_maybe_append_cause(BT_LOG_WARNING
, active_log_level
,
244 false, NULL
, NULL
, NULL
, NULL
);