- if (BT_LOG_ON_WARN && Py_IsInitialized() && PyErr_Occurred()) {
- BT_LOGW_STR("Exception occured: traceback: ");
- PyErr_Print();
+ GString *exc = NULL;
+
+ if (Py_IsInitialized() && PyErr_Occurred()) {
+ exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
+ if (!exc) {
+ BT_LOGE_STR("Failed to format Python exception.");
+ goto end;
+ }
+
+ (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
+ "Babeltrace library", "%s", exc->str);
+ }
+
+end:
+ if (exc) {
+ g_string_free(exc, TRUE);
+ }
+}
+
+static
+void log_python_traceback(int log_level)
+{
+ GString *exc = NULL;
+
+ if (Py_IsInitialized() && PyErr_Occurred()) {
+ exc = bt_py_common_format_current_exception(BT_LOG_OUTPUT_LEVEL);
+ if (!exc) {
+ BT_LOGE_STR("Failed to format Python exception.");
+ goto end;
+ }
+
+ BT_LOG_WRITE(log_level, BT_LOG_TAG,
+ "Exception occured: Python traceback:\n%s", exc->str);
+ }
+
+end:
+ if (exc) {
+ g_string_free(exc, TRUE);