mirror of
https://github.com/LorenEteval/Furious.git
synced 2026-09-27 17:37:58 +03:00
85 lines
2.3 KiB
Python
85 lines
2.3 KiB
Python
from Furious.Widget.Application import Application
|
|
from Furious.Utility.Constants import APP, PLATFORM, CRASH_LOG_DIR
|
|
|
|
import os
|
|
import sys
|
|
import logging
|
|
import datetime
|
|
import operator
|
|
import traceback
|
|
import functools
|
|
import multiprocessing
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
if PLATFORM == 'Windows':
|
|
ProcessContext = multiprocessing
|
|
else:
|
|
ProcessContext = multiprocessing.get_context('spawn')
|
|
|
|
|
|
class Process(ProcessContext.Process):
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
|
|
self.startUpTime = str(datetime.datetime.now()).replace(':', '')
|
|
self.logFileName = f'{self.startUpTime}.log'
|
|
self.fileWritten = multiprocessing.Manager().Value('b', False)
|
|
self.application = None
|
|
|
|
def exceptHook(self, exceptionType, exceptionValue, tb):
|
|
if logger.level < logging.CRITICAL:
|
|
traceback.print_exception(exceptionType, exceptionValue, tb)
|
|
|
|
if isinstance(exceptionValue, AssertionError):
|
|
error = Application.ErrorCode.AssertionError
|
|
else:
|
|
error = Application.ErrorCode.UnknownException
|
|
|
|
logger.error(f'stopped with exitcode {error}')
|
|
|
|
self.saveCrashLog(exceptionType, exceptionValue, tb)
|
|
|
|
if APP() is not None:
|
|
APP().exit(error)
|
|
else:
|
|
sys.exit(error)
|
|
|
|
def saveCrashLog(self, exceptionType, exceptionValue, tb):
|
|
try:
|
|
os.mkdir(CRASH_LOG_DIR)
|
|
except FileExistsError:
|
|
# Directory already exists
|
|
pass
|
|
except Exception:
|
|
# Any non-exit exceptions
|
|
|
|
return
|
|
|
|
try:
|
|
stackLog = functools.reduce(
|
|
operator.add,
|
|
traceback.format_exception(exceptionType, exceptionValue, tb),
|
|
)
|
|
|
|
if APP() is None:
|
|
crashLog = f'{stackLog}'
|
|
else:
|
|
crashLog = f'{APP().log()}\n{stackLog}'
|
|
|
|
with open(CRASH_LOG_DIR / self.logFileName, 'w', encoding='utf-8') as file:
|
|
file.write(crashLog)
|
|
|
|
self.fileWritten.value = True
|
|
except Exception:
|
|
# Any non-exit exceptions
|
|
|
|
pass
|
|
|
|
def run(self):
|
|
sys.excepthook = self.exceptHook
|
|
|
|
self.application = Application(sys.argv)
|
|
|
|
sys.exit(self.application.run())
|