Python中的崩溃报告 [英] Crash reporting in Python

查看:200
本文介绍了Python中的崩溃报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个崩溃报告框架,该框架可用于纯Python Tkinter应用程序?理想情况下,它应该跨平台运行。

Is there a crash reporting framework that can be used for pure Python Tkinter applications? Ideally, it should work cross-platform.

实际上,由于Python解释器本身几乎不会崩溃,因此,这更多是异常报告。

Practically speaking, this is more of 'exception reporting' since the Python interpreter itself hardly crashes.

这里是一个示例崩溃报告器:

Here's a sample crash reporter:

推荐答案

而不是使用 try..except污染代码。到处都应该通过设置 sys.excepthook 来实现自己的除钩子。下面是一个示例:

Rather than polluting your code with try..except everywhere, you should just implement your own except hook by setting sys.excepthook. Here is an example:

import sys
import traceback

def install_excepthook():
    def my_excepthook(exctype, value, tb):
        s = ''.join(traceback.format_exception(exctype, value, tb))
        dialog = ErrorReportDialog(None, s)
        dialog.exec_()

    sys.excepthook = my_excepthook

调用 install_exception()在应用程序启动时。

Call install_exception() when your application starts.

ErrorReportDialog 是我做了一个Qt对话框。 traceback.format_exception()将以与Python解释器相同的方式格式化传递给except挂钩的参数。

ErrorReportDialog is a Qt dialog I've made. traceback.format_exception() will format argument passed to the except hook in the same way it does in Python's interpreter.

编辑:我忘了提一点小技巧。它不适用于线程(嗯,至少上次我没有检查过)。对于在另一个线程中运行的代码,您需要将其包装在 try..except 块中。

I forgot to mention a little gotcha with that. It doesn't work with threads (well, at least it didn't last time I checked). For code running in another thread, you will need to wrap it in a try..except block.

这篇关于Python中的崩溃报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆