|
| 1 | +import logging |
| 2 | +import platform |
| 3 | +import sys |
| 4 | +import threading |
| 5 | +from logging.handlers import RotatingFileHandler |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from PySide6.QtCore import ( |
| 9 | + QMessageLogContext, |
| 10 | + QStandardPaths, |
| 11 | + QtMsgType, |
| 12 | + qInstallMessageHandler, |
| 13 | +) |
| 14 | +from PySide6.QtWidgets import QApplication |
| 15 | + |
| 16 | +from . import __version__ |
| 17 | + |
| 18 | +_configured = False |
| 19 | +_log_file_path: Path | None = None |
| 20 | + |
| 21 | + |
| 22 | +def setup_logging(_app: QApplication) -> Path: |
| 23 | + global _configured, _log_file_path |
| 24 | + if _configured: |
| 25 | + assert _log_file_path is not None |
| 26 | + return _log_file_path |
| 27 | + |
| 28 | + base = Path( |
| 29 | + QStandardPaths.writableLocation( |
| 30 | + QStandardPaths.StandardLocation.AppLocalDataLocation |
| 31 | + ) |
| 32 | + ) |
| 33 | + log_dir = base / "logs" |
| 34 | + log_dir.mkdir(parents=True, exist_ok=True) |
| 35 | + log_path = log_dir / "clinical-dbs-annotator.log" |
| 36 | + |
| 37 | + fmt = logging.Formatter("%(asctime)s ¦ %(levelname)s ¦ %(name)s ¦ %(message)s") |
| 38 | + root = logging.getLogger() |
| 39 | + root.setLevel(logging.DEBUG) |
| 40 | + for h in root.handlers[:]: |
| 41 | + root.removeHandler(h) |
| 42 | + |
| 43 | + fh = RotatingFileHandler( |
| 44 | + log_path, maxBytes=5_000_000, backupCount=3, encoding="utf-8" |
| 45 | + ) |
| 46 | + fh.setLevel(logging.INFO) |
| 47 | + fh.setFormatter(fmt) |
| 48 | + root.addHandler(fh) |
| 49 | + |
| 50 | + if not getattr(sys, "frozen", False): |
| 51 | + sh = logging.StreamHandler(sys.stderr) |
| 52 | + sh.setLevel(logging.INFO) |
| 53 | + sh.setFormatter(fmt) |
| 54 | + root.addHandler(sh) |
| 55 | + |
| 56 | + def exc_hook(exc_type, exc, tb): |
| 57 | + logging.getLogger("uncaught").critical( |
| 58 | + "Uncaught exception", |
| 59 | + exc_info=(exc_type, exc, tb), |
| 60 | + ) |
| 61 | + |
| 62 | + sys.excepthook = exc_hook |
| 63 | + |
| 64 | + def thread_exc_hook(args: threading.ExceptHookArgs) -> None: |
| 65 | + logging.getLogger("uncaught").critical( |
| 66 | + "Uncaught thread exception", |
| 67 | + exc_info=(args.exc_type, args.exc_value, args.exc_traceback), |
| 68 | + ) |
| 69 | + |
| 70 | + threading.excepthook = thread_exc_hook |
| 71 | + |
| 72 | + def qt_handler(mode: QtMsgType, context: QMessageLogContext, message: str) -> None: |
| 73 | + if mode == QtMsgType.QtDebugMsg: |
| 74 | + level = logging.DEBUG |
| 75 | + elif mode == QtMsgType.QtInfoMsg: |
| 76 | + level = logging.INFO |
| 77 | + elif mode == QtMsgType.QtWarningMsg: |
| 78 | + level = logging.WARNING |
| 79 | + elif mode == QtMsgType.QtCriticalMsg: |
| 80 | + level = logging.ERROR |
| 81 | + else: |
| 82 | + level = logging.CRITICAL |
| 83 | + suffix = "" |
| 84 | + if context.file: |
| 85 | + suffix = f" ({context.file}:{context.line})" |
| 86 | + logging.getLogger("qt").log(level, "%s%s", message, suffix) |
| 87 | + |
| 88 | + qInstallMessageHandler(qt_handler) |
| 89 | + |
| 90 | + logging.getLogger("clinical_dbs_annotator").info( |
| 91 | + "Started v%s Python %s | %s | log=%s", |
| 92 | + __version__, |
| 93 | + platform.python_version(), |
| 94 | + platform.platform(), |
| 95 | + log_path.resolve(), |
| 96 | + ) |
| 97 | + |
| 98 | + resolved = log_path.resolve() |
| 99 | + _configured = True |
| 100 | + _log_file_path = resolved |
| 101 | + return resolved |
0 commit comments