From a62ae8f147aedf9ec930df5ae57a482472924f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Wed, 13 Sep 2023 16:45:14 +0200 Subject: [PATCH] [cli] Use exception logger from stdlib --- ltchiptool/__main__.py | 4 ++-- ltchiptool/gui/__main__.py | 6 +++--- ltchiptool/gui/main.py | 8 ++++---- ltchiptool/gui/work/base.py | 6 ++---- ltchiptool/util/logging.py | 13 +++++++++---- ltchiptool/util/lpm.py | 6 ++---- ltctplugin/base/base.py | 4 ++-- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/ltchiptool/__main__.py b/ltchiptool/__main__.py index 5e40066..3b2e21f 100644 --- a/ltchiptool/__main__.py +++ b/ltchiptool/__main__.py @@ -1,7 +1,7 @@ # Copyright (c) Kuba Szczodrzyński 2022-07-29. import os -from logging import DEBUG, INFO +from logging import DEBUG, INFO, exception import click from click import Context @@ -121,7 +121,7 @@ def cli(): try: cli_entrypoint() except Exception as e: - LoggingHandler.get().emit_exception(e) + exception(None, exc_info=e) exit(1) diff --git a/ltchiptool/gui/__main__.py b/ltchiptool/gui/__main__.py index 439a70a..2de1c7b 100644 --- a/ltchiptool/gui/__main__.py +++ b/ltchiptool/gui/__main__.py @@ -1,7 +1,7 @@ # Copyright (c) Kuba Szczodrzyński 2023-1-2. import sys -from logging import INFO, NOTSET, error +from logging import INFO, NOTSET, error, exception import click @@ -33,7 +33,7 @@ def gui_entrypoint(*args, **kwargs): app.MainLoop() except Exception as e: LoggingHandler.get().exception_hook = None - LoggingHandler.get().emit_exception(e) + exception(None, exc_info=e) wx.MessageBox( message=f"Exception during app initialization\n\n{type(e).__name__}: {e}", caption="Error", @@ -49,7 +49,7 @@ def cli(*args, **kwargs): try: gui_entrypoint(*args, **kwargs) except Exception as e: - LoggingHandler.get().emit_exception(e) + exception(None, exc_info=e) exit(1) diff --git a/ltchiptool/gui/main.py b/ltchiptool/gui/main.py index 39373b0..677dce8 100644 --- a/ltchiptool/gui/main.py +++ b/ltchiptool/gui/main.py @@ -2,7 +2,7 @@ import sys import threading -from logging import debug, info, warning +from logging import debug, exception, info, warning from os import rename, unlink from os.path import dirname, isfile, join @@ -127,7 +127,7 @@ def __init__(self, *args, **kw): self.loaded = True except Exception as e: - LoggingHandler.get().emit_exception(e, msg=f"Couldn't build {name}") + exception(f"Couldn't build {name}", exc_info=e) if not self.loaded: self.OnClose() @@ -231,9 +231,9 @@ def UpdateMenus(self) -> None: @staticmethod def OnException(*args): if isinstance(args[0], type): - LoggingHandler.get().emit_exception(args[1]) + exception(None, exc_info=args[1]) else: - LoggingHandler.get().emit_exception(args[0].exc_value) + exception(None, exc_info=args[0].exc_value) @staticmethod def ShowExceptionMessage(e, msg): diff --git a/ltchiptool/gui/work/base.py b/ltchiptool/gui/work/base.py index d08815f..6c90a21 100644 --- a/ltchiptool/gui/work/base.py +++ b/ltchiptool/gui/work/base.py @@ -1,11 +1,9 @@ # Copyright (c) Kuba Szczodrzyński 2023-1-9. -from logging import debug +from logging import debug, exception from threading import Event, Thread from typing import Callable -from ltchiptool.util.logging import LoggingHandler - class BaseThread(Thread): _stop_flag: Event @@ -27,7 +25,7 @@ def run(self): except Exception as e: if self.should_run(): # show exceptions only if not cancelled - LoggingHandler.get().emit_exception(e) + exception(None, exc_info=e) self.stop() if self.on_stop: diff --git a/ltchiptool/util/logging.py b/ltchiptool/util/logging.py index 66b6730..467f8ba 100644 --- a/ltchiptool/util/logging.py +++ b/ltchiptool/util/logging.py @@ -86,10 +86,15 @@ def clear_emitters(self): self.emitters.clear() def emit(self, record: LogRecord) -> None: - message = record.getMessage() - if not message: - return - self.emit_string(record.levelname[:1], message) + message = record.msg + if message: + if record.args: + message = message % record.args + self.emit_string(record.levelname[:1], message) + if record.exc_info: + _, e, _ = record.exc_info + if e: + self.emit_exception(e=e) def emit_string(self, log_prefix: str, message: str, color: str = None): now = time() diff --git a/ltchiptool/util/lpm.py b/ltchiptool/util/lpm.py index d564f11..a3e2945 100644 --- a/ltchiptool/util/lpm.py +++ b/ltchiptool/util/lpm.py @@ -5,7 +5,7 @@ import sys from dataclasses import dataclass from importlib import import_module -from logging import debug, error, info, warning +from logging import debug, error, exception, info, warning from os.path import join from pkgutil import iter_modules from typing import List, Optional, Set, Tuple @@ -15,7 +15,6 @@ import ltctplugin from ltchiptool.util.cli import run_subprocess from ltchiptool.util.fileio import readjson, writejson -from ltchiptool.util.logging import LoggingHandler from ltctplugin.base import PluginBase PYPI_URL = "https://pypi.org/search/" @@ -96,8 +95,7 @@ def rescan(self) -> Tuple[Set[str], Set[str]]: f"ltchiptool {plugin.ltchiptool_version}" ) except Exception as e: - error(f"Couldn't load plugin '{namespace}', disabling!") - LoggingHandler.get().emit_exception(e) + exception(f"Couldn't load plugin '{namespace}', disabling!", exc_info=e) self.disable(namespace, rescan=False) curr = self.disabled.union(p.namespace for p in self.plugins) return prev, curr diff --git a/ltctplugin/base/base.py b/ltctplugin/base/base.py index 992ea99..7d7723c 100644 --- a/ltctplugin/base/base.py +++ b/ltctplugin/base/base.py @@ -6,6 +6,7 @@ from abc import ABC from functools import lru_cache from glob import glob +from logging import exception from os.path import basename, isdir, isfile, join from pathlib import Path from typing import Any, Dict, Optional @@ -14,7 +15,6 @@ from semantic_version.base import BaseSpec, Version from ltchiptool import get_version -from ltchiptool.util.logging import LoggingHandler class PluginBase(ABC): @@ -101,7 +101,7 @@ def plugin_meta(self) -> dict: try: meta = self._distribution_meta except Exception as e: - LoggingHandler.get().emit_exception(e) + exception(None, exc_info=e) meta = dict() description: str = meta.get("summary", None) if description: