Skip to content

Commit

Permalink
[cli] Use exception logger from stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Sep 13, 2023
1 parent 544da87 commit a62ae8f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions ltchiptool/__main__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)


Expand Down
6 changes: 3 additions & 3 deletions ltchiptool/gui/__main__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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",
Expand All @@ -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)


Expand Down
8 changes: 4 additions & 4 deletions ltchiptool/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand Down
6 changes: 2 additions & 4 deletions ltchiptool/gui/work/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
13 changes: 9 additions & 4 deletions ltchiptool/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions ltchiptool/util/lpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ltctplugin/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a62ae8f

Please sign in to comment.