Skip to content

Commit

Permalink
Addresses review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenNneji committed Oct 2, 2024
1 parent 47b1d1f commit 8167404
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
19 changes: 17 additions & 2 deletions rascal2/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import logging
import pathlib
import platform
import sys
from os import PathLike

from rascal2.core import Settings
from rascal2.core import Settings, get_global_settings

SOURCE_PATH = pathlib.Path(__file__).parent
STATIC_PATH = SOURCE_PATH / "static"
Expand Down Expand Up @@ -72,12 +73,26 @@ def setup_logging(log_path: str | PathLike, level: int = logging.INFO) -> loggin
"""
path = pathlib.Path(log_path)
logger = logging.getLogger(path.stem)
logger = logging.getLogger("rascal_log")
logger.setLevel(level)
logger.handlers.clear()

# TODO add console print handler when console is added
# https://github.com/RascalSoftware/RasCAL-2/issues/5
log_filehandler = logging.FileHandler(path)
logger.addHandler(log_filehandler)

return logger


def log_uncaught_exceptions(exc_type, exc_value, exc_traceback):
"""Qt slots swallows exceptions but this ensures exceptions are logged"""
logger = logging.getLogger("rascal_log")
if not logger.handlers:
# Backup in case the crash happens before the local logger setup
path = pathlib.Path(get_global_settings().fileName()).parent
path.mkdir(parents=True, exist_ok=True)
logger.addHandler(logging.FileHandler(path / "crash.log"))
logger.critical("An unhandled exception occurred!", exc_info=(exc_type, exc_value, exc_traceback))
logging.shutdown()
sys.exit(1)
4 changes: 2 additions & 2 deletions rascal2/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from rascal2.core.settings import Settings
from rascal2.core.settings import Settings, get_global_settings

__all__ = ["Settings"]
__all__ = ["Settings", "get_global_settings"]
10 changes: 1 addition & 9 deletions rascal2/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logging
import multiprocessing
import sys

from PyQt6 import QtGui, QtWidgets

from rascal2.config import handle_scaling, path_for
from rascal2.config import handle_scaling, log_uncaught_exceptions, path_for
from rascal2.ui.view import MainWindowView


Expand All @@ -27,13 +26,6 @@ def ui_execute():
return app.exec()


def log_uncaught_exceptions(exc_type, exc_value, exc_traceback):
"""Qt slots swallows exceptions but this ensures exceptions are logged"""
logging.critical("An unhandled exception occurred!", exc_info=(exc_type, exc_value, exc_traceback))
logging.shutdown()
sys.exit(1)


def main():
multiprocessing.freeze_support()
sys.excepthook = log_uncaught_exceptions
Expand Down

0 comments on commit 8167404

Please sign in to comment.