From 9cb24280fabf3926b92af2f7c6704f4a623eb374 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Wed, 17 Nov 2021 22:46:32 +0100 Subject: [PATCH] Clients: log exception to logfile --- CommonClient.py | 4 ++-- FactorioClient.py | 2 +- SNIClient.py | 2 +- Utils.py | 18 +++++++++++++++++- kvui.py | 20 +------------------- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/CommonClient.py b/CommonClient.py index bde3adb5a05f..e8a742bba781 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -11,7 +11,7 @@ import Utils if __name__ == "__main__": - Utils.init_logging("TextClient") + Utils.init_logging("TextClient", exception_logger="Client") from MultiServer import CommandProcessor from NetUtils import Endpoint, decode, NetworkItem, encode, JSONtoTextParser, ClientStatus, Permission @@ -271,7 +271,7 @@ def on_deathlink(self, data: dict): logger.info(f"DeathLink: Received from {data['source']}") async def send_death(self, death_text: str = ""): - logger.info("Sending death to your friends...") + logger.info("DeathLink: Sending death to your friends...") self.last_death_link = time.time() await self.send_msgs([{ "cmd": "Bounce", "tags": ["DeathLink"], diff --git a/FactorioClient.py b/FactorioClient.py index 5eb39035a986..1375f73749a2 100644 --- a/FactorioClient.py +++ b/FactorioClient.py @@ -15,7 +15,7 @@ import Utils if __name__ == "__main__": - Utils.init_logging("FactorioClient") + Utils.init_logging("FactorioClient", exception_logger="Client") from CommonClient import CommonContext, server_loop, console_loop, ClientCommandProcessor, logger, gui_enabled, \ get_base_parser diff --git a/SNIClient.py b/SNIClient.py index 6044f11a5116..1a2512621fb1 100644 --- a/SNIClient.py +++ b/SNIClient.py @@ -15,7 +15,7 @@ from Utils import get_item_name_from_id, init_logging if __name__ == "__main__": - init_logging("SNIClient") + init_logging("SNIClient", exception_logger="Client") import colorama diff --git a/Utils.py b/Utils.py index b666eed00a36..84e4378f2469 100644 --- a/Utils.py +++ b/Utils.py @@ -427,7 +427,7 @@ def get_text_between(text: str, start: str, end: str) -> str: def init_logging(name: str, loglevel: typing.Union[str, int] = logging.INFO, write_mode: str = "w", - log_format: str = "[%(name)s]: %(message)s"): + log_format: str = "[%(name)s]: %(message)s", exception_logger: str = ""): loglevel: int = loglevel_mapping.get(loglevel, loglevel) log_folder = local_path("logs") os.makedirs(log_folder, exist_ok=True) @@ -446,3 +446,19 @@ def init_logging(name: str, loglevel: typing.Union[str, int] = logging.INFO, wri root_logger.addHandler( logging.StreamHandler(sys.stdout) ) + + # Relay unhandled exceptions to logger. + if not getattr(sys.excepthook, "_wrapped", False): # skip if already modified + orig_hook = sys.excepthook + + def handle_exception(exc_type, exc_value, exc_traceback): + if issubclass(exc_type, KeyboardInterrupt): + sys.__excepthook__(exc_type, exc_value, exc_traceback) + return + logging.getLogger(exception_logger).exception("Uncaught exception", + exc_info=(exc_type, exc_value, exc_traceback)) + return orig_hook(exc_type, exc_value, exc_traceback) + + handle_exception._wrapped = True + + sys.excepthook = handle_exception diff --git a/kvui.py b/kvui.py index 1a0071c94200..da2923bbc559 100644 --- a/kvui.py +++ b/kvui.py @@ -208,26 +208,8 @@ def build(self): self.commandprocessor("/help") Clock.schedule_interval(self.update_texts, 1 / 30) self.container.add_widget(self.grid) - self.catch_unhandled_exceptions() return self.container - def catch_unhandled_exceptions(self): - """Relay unhandled exceptions to UI logger.""" - if not getattr(sys.excepthook, "_wrapped", False): # skip if already modified - orig_hook = sys.excepthook - - def handle_exception(exc_type, exc_value, exc_traceback): - if issubclass(exc_type, KeyboardInterrupt): - sys.__excepthook__(exc_type, exc_value, exc_traceback) - return - logging.getLogger("Client").exception("Uncaught exception", - exc_info=(exc_type, exc_value, exc_traceback)) - return orig_hook(exc_type, exc_value, exc_traceback) - - handle_exception._wrapped = True - - sys.excepthook = handle_exception - def update_texts(self, dt): if self.ctx.server: self.title = self.base_title + " " + Utils.__version__ + \ @@ -304,7 +286,7 @@ class TextManager(GameManager): class LogtoUI(logging.Handler): def __init__(self, on_log): - super(LogtoUI, self).__init__(logging.DEBUG) + super(LogtoUI, self).__init__(logging.INFO) self.on_log = on_log def handle(self, record: logging.LogRecord) -> None: