Files
dragonpilot/selfdrive/crash.py

90 lines
2.6 KiB
Python
Raw Normal View History

2016-11-29 18:34:21 -08:00
"""Install exception handler for process crash."""
import os
import sys
2019-03-26 01:09:18 -07:00
import threading
2019-06-28 21:11:30 +00:00
import capnp
2018-03-17 00:01:50 -07:00
from selfdrive.version import version, dirty
2016-11-29 18:34:21 -08:00
2017-10-03 00:22:32 -07:00
from selfdrive.swaglog import cloudlog
2017-09-30 03:07:27 -07:00
if os.getenv("NOLOG") or os.getenv("NOCRASH"):
2016-11-29 18:34:21 -08:00
def capture_exception(*exc_info):
pass
2016-12-12 17:47:46 -08:00
def bind_user(**kwargs):
pass
2017-05-11 12:41:17 -07:00
def bind_extra(**kwargs):
pass
2016-11-29 18:34:21 -08:00
def install():
pass
else:
from raven import Client
from raven.transport.http import HTTPTransport
with open("/data/openpilot/.git/HEAD", "r") as f:
content = f.read().split(' ')
branch = content[1]
error_tags = {'dirty': dirty, 'branch': branch}
try:
with open("/data/data/ai.comma.plus.offroad/files/persistStore/persist-auth", "r") as f:
auth = json.loads(f.read())
auth = json.loads(auth['commaUser'])
error_tags['username'] = auth['username']
error_tags['email'] = auth['email']
except:
pass
client = Client('https://980a0cba712a4c3593c33c78a12446e1:fecab286bcaf4dba8b04f7cff0188e2d@sentry.io/1488600',
install_sys_hook=False, transport=HTTPTransport, release=version, tags=error_tags)
try:
client.user_context(error_tags['username'])
except:
pass
def capture_warning(warning_string):
client.captureMessage(warning_string, level='warning')
def capture_info(info_string):
client.captureMessage(info_string, level='info')
2016-11-29 18:34:21 -08:00
2017-10-03 00:22:32 -07:00
def capture_exception(*args, **kwargs):
2019-06-28 21:11:30 +00:00
exc_info = sys.exc_info()
if not exc_info[0] is capnp.lib.capnp.KjException:
client.captureException(*args, **kwargs)
2017-10-03 00:22:32 -07:00
cloudlog.error("crash", exc_info=kwargs.get('exc_info', 1))
2016-11-29 18:34:21 -08:00
2016-12-12 17:47:46 -08:00
def bind_user(**kwargs):
client.user_context(kwargs)
2017-05-11 12:41:17 -07:00
def bind_extra(**kwargs):
client.extra_context(kwargs)
2016-11-29 18:34:21 -08:00
def install():
# installs a sys.excepthook
__excepthook__ = sys.excepthook
def handle_exception(*exc_info):
if exc_info[0] not in (KeyboardInterrupt, SystemExit):
2017-10-03 00:22:32 -07:00
capture_exception(exc_info=exc_info)
2016-11-29 18:34:21 -08:00
__excepthook__(*exc_info)
sys.excepthook = handle_exception
2019-03-26 01:09:18 -07:00
"""
Workaround for `sys.excepthook` thread bug from:
http://bugs.python.org/issue1230540
Call once from the main thread before creating any threads.
Source: https://stackoverflow.com/a/31622038
"""
init_original = threading.Thread.__init__
def init(self, *args, **kwargs):
init_original(self, *args, **kwargs)
run_original = self.run
def run_with_except_hook(*args2, **kwargs2):
try:
run_original(*args2, **kwargs2)
except Exception:
sys.excepthook(*sys.exc_info())
self.run = run_with_except_hook
threading.Thread.__init__ = init