def debug_exception(logger=None):
"""
Diagnostic helper context manager
It controls execution within its context and writes extended
diagnostic info to the Kodi log if an unhandled exception
happens within the context. The info includes the following items:
- Module path.
- Code fragment where the exception has happened.
- Global variables.
- Local variables.
After logging the diagnostic info the exception is re-raised.
Example::
with debug_exception():
# Some risky code
raise RuntimeError('Fatal error!')
:param logger: logger function which must accept a single argument
which is a log message. By default it is :func:`xbmc.log`
with ``ERROR`` level.
"""
try:
yield
except:
if logger is None:
logger = lambda msg: xbmc.log(msg, xbmc.LOGERROR)
logger('Unhandled exception detected!')
logger('*** Start diagnostic info ***')
frame_info = inspect.trace(5)[-1]
logger('File: {0}'.format(frame_info[1]))
context = ''
for i, line in enumerate(frame_info[4], frame_info[2] - frame_info[5]):
if i == frame_info[2]:
context += '{0}:>{1}'.format(str(i).rjust(5), line)
else:
context += '{0}: {1}'.format(str(i).rjust(5), line)
logger('Code context:\n' + context)
logger('Global variables:\n' + _format_vars(frame_info[0].f_globals))
logger('Local variables:\n' + _format_vars(frame_info[0].f_locals))
logger('**** End diagnostic info ****')
raise
评论列表
文章目录