Browse Source

Now log_expt() logs a traceback

Yann Weber 5 years ago
parent
commit
ccb4234906
1 changed files with 27 additions and 1 deletions
  1. 27
    1
      src/pyutils.c

+ 27
- 1
src/pyutils.c View File

@@ -436,7 +436,9 @@ void log_expt(int priority)
436 436
 	}
437 437
 
438 438
 	PyObject *expt, *expt_bytes, *expt_cls,
439
-		*expt_val, *expt_type, *traceback;
439
+		*expt_val, *expt_type, *traceback,
440
+		*tbmod, *tbfmt, *tbfmt_args, *tbstr, **lines;
441
+	Py_ssize_t i, nlines;
440 442
 	char *msg, *type, *val;
441 443
 	int msg_sz;
442 444
 	char msg_fmt[] = "%s: %s";
@@ -462,6 +464,30 @@ void log_expt(int priority)
462 464
 	snprintf(msg, msg_sz, msg_fmt, type, val);
463 465
 
464 466
 	pyfcgi_log(priority, msg);
467
+
468
+	//Fetching & logging traceback
469
+	tbmod = PyImport_ImportModule("traceback");
470
+	tbfmt = PyObject_GetAttrString(tbmod, "format_tb");
471
+	tbfmt_args = Py_BuildValue("(O)", traceback);
472
+	tbstr = PyObject_CallObject(tbfmt, tbfmt_args);
473
+	if(!tbstr)
474
+	{
475
+		pyfcgi_log(LOG_ALERT,"FAILS TO FOMAT");
476
+		PyErr_Fetch(&expt_cls, &expt, &traceback);
477
+		pyfcgi_log(LOG_ALERT, "%s", PyUnicode_AsUTF8(PyObject_ASCII(expt)));
478
+		return;
479
+	}
480
+	nlines = PySequence_Fast_GET_SIZE(tbstr);
481
+	lines = PySequence_Fast_ITEMS(tbstr);
482
+	for(i=0; i<nlines; i++)
483
+	{
484
+		pyfcgi_log(priority, PyUnicode_AsUTF8(lines[i]));
485
+	}
486
+	Py_DECREF(tbmod);
487
+	Py_DECREF(tbfmt);
488
+	Py_DECREF(tbfmt_args);
489
+	Py_DECREF(tbstr);
490
+
465 491
 }
466 492
 
467 493
 void pyfcgi_python_version(char version[16])

Loading…
Cancel
Save