MCPcopy Create free account
hub / github.com/domoticz/domoticz / LogPythonException

Function LogPythonException

main/EventsPythonModule.cpp:301–359  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

299 }
300
301 void LogPythonException()
302 {
303 PyNewRef pTraceback;
304 PyNewRef pExcept;
305 PyNewRef pValue;
306
307 PyErr_Fetch(&pExcept, &pValue, &pTraceback);
308 PyErr_NormalizeException(&pExcept, &pValue, &pTraceback);
309
310 if (!pExcept && !pValue && !pTraceback)
311 {
312 _log.Log(LOG_ERROR, "Unable to decode exception.");
313 }
314 else
315 {
316 std::string sTypeText("Unknown");
317
318 /* See if we can get a full traceback */
319 PyNewRef pModule = PyImport_ImportModule("traceback");
320 if (pModule)
321 {
322 PyNewRef pFunc = PyObject_GetAttrString(pModule, "format_exception");
323 if (pFunc && PyCallable_Check(pFunc)) {
324 PyNewRef pList = PyObject_CallFunctionObjArgs(pFunc, (PyObject*)pExcept, (PyObject*)pValue, (PyObject*)pTraceback, NULL);
325 if (pList)
326 {
327 for (Py_ssize_t i = 0; i < PyList_Size(pList); i++)
328 {
329 PyBorrowedRef pPyStr = PyList_GetItem(pList, i);
330 std::string pStr(pPyStr);
331 size_t pos = 0;
332 std::string token;
333 while ((pos = pStr.find('\n')) != std::string::npos) {
334 token = pStr.substr(0, pos);
335 _log.Log(LOG_ERROR, "%s", token.c_str());
336 pStr.erase(0, pos + 1);
337 }
338 }
339 }
340 else
341 {
342 if (pExcept) sTypeText = pExcept.Attribute("__name__");
343 _log.Log(LOG_ERROR, "Exception: '%s'. No traceback available.", sTypeText.c_str());
344 }
345 }
346 else
347 {
348 if (pExcept) sTypeText = pExcept.Attribute("__name__");
349 _log.Log(LOG_ERROR, "'format_exception' lookup failed, exception: '%s'. No traceback available.", sTypeText.c_str());
350 }
351 }
352 else
353 {
354 if (pExcept) sTypeText = pExcept.Attribute("__name__");
355 _log.Log(LOG_ERROR, "'Traceback' module import failed, exception: '%s'. No traceback available.", sTypeText.c_str());
356 }
357 }
358 PyErr_Clear();

Callers 7

LogPythonExceptionMethod · 0.85
InitialiseMethod · 0.85
ConnectionReadMethod · 0.85
ReleaseThreadMethod · 0.85
CallbackMethod · 0.85
StopMethod · 0.85

Calls 4

c_strMethod · 0.80
LogMethod · 0.45
findMethod · 0.45
AttributeMethod · 0.45

Tested by

no test coverage detected