diff --git a/selfdrive/crash.py b/selfdrive/crash.py index 4676c96b8..958a23c0a 100644 --- a/selfdrive/crash.py +++ b/selfdrive/crash.py @@ -3,9 +3,13 @@ import os import sys import threading import capnp +from datetime import datetime +import traceback from selfdrive.version import version, dirty, origin, branch from common.params import Params -uniqueID = Params().get('DongleId', None) +import requests +from common.dp_common import is_online +CRASHES_DIR = '/sdcard/crash_logs/' from selfdrive.swaglog import cloudlog from common.android import ANDROID @@ -30,32 +34,52 @@ else: dongle_id = params.get("DongleId").decode('utf8') except AttributeError: dongle_id = "None" - error_tags = {'dirty': dirty, 'username': uniqueID, 'dongle_id': dongle_id, 'branch': branch, 'remote': origin} + try: + ip = requests.get('https://checkip.amazonaws.com/', timeout=3).text.strip() if is_online() else '255.255.255.255' + except: + ip = "255.255.255.255" + error_tags = {'dirty': dirty, 'username': dongle_id, 'dongle_id': dongle_id, 'branch': branch, 'remote': origin} - client = Client('https://980a0cba712a4c3593c33c78a12446e1:fecab286bcaf4dba8b04f7cff0188e2d@sentry.io/1488600', + client = Client('http://7107f046f45b4b4f9b277d0684bc9281@sentry.dragonpilot.cn:9000/2', install_sys_hook=False, transport=HTTPTransport, release=version, tags=error_tags) def capture_exception(*args, **kwargs): + save_exception(traceback.format_exc()) exc_info = sys.exc_info() if not exc_info[0] is capnp.lib.capnp.KjException: client.captureException(*args, **kwargs) cloudlog.error("crash", exc_info=kwargs.get('exc_info', 1)) + # dp - from @ShaneSmiskol, save log into local directory + def save_exception(exc_text): + if not os.path.exists(CRASHES_DIR): + os.mkdir(CRASHES_DIR) + log_file = '{}/{}'.format(CRASHES_DIR, datetime.now().strftime('%Y-%m-%d--%H-%M-%S.%f.log')[:-3]) + with open(log_file, 'w') as f: + f.write(exc_text) + print('Logged current crash to {}'.format(log_file)) + def bind_user(**kwargs): client.user_context(kwargs) def capture_warning(warning_string): - bind_user(id=dongle_id) + bind_user(id=dongle_id, ip_address=ip) client.captureMessage(warning_string, level='warning') def capture_info(info_string): - bind_user(id=dongle_id) + bind_user(id=dongle_id, ip_address=ip) client.captureMessage(info_string, level='info') def bind_extra(**kwargs): client.extra_context(kwargs) def install(): + """ + 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 + """ # installs a sys.excepthook __excepthook__ = sys.excepthook @@ -65,12 +89,6 @@ else: __excepthook__(*exc_info) sys.excepthook = handle_exception - """ - 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):