mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
If we don't do this, the uvicorn server may log its shutdown after the Qt application has closed, and the signal this attempts to emit may not be valid. Disconnect the log signals when the application exits to prevent that. There's actually another solution that I thought would be better, but I couldn't get it to work: https://www.pyinstaller.org/en/stable/feature-notes.html#automatic-hiding-and-minimization-of-console-window-under-windows describes a way to have pyinstaller hide or minimize the console rather than disabling it entirely. I was never really fond of getting rid of the console window in the first place, but it did bother some users. If we could get the hide or minimize option working, that'd probably avoid bothering users, but also make the logs much easier to find, get us out of the trouble of maintaining our own log viewer, and fix the problem mentioned in the comment I add here (the log window only works if there's only one in memory log handler). Another option would be ditching our log window and instead just having that menu item open the log file or directory in whatever program the OS defaults to (probably notepad). It would still have the quirk of maybe needing to open more than one location, since logging is use configurable. Fixes https://github.com/dcs-liberation/dcs_liberation/issues/3278.
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import typing
|
|
from collections.abc import Iterator
|
|
|
|
LogHook = typing.Callable[[str], None]
|
|
|
|
|
|
class HookableInMemoryHandler(logging.Handler):
|
|
"""Hookable in-memory logging handler for logs window"""
|
|
|
|
_log: str
|
|
_hook: typing.Optional[typing.Callable[[str], None]]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(HookableInMemoryHandler, self).__init__(*args, **kwargs)
|
|
self._log = ""
|
|
self._hook = None
|
|
|
|
@staticmethod
|
|
def iter_registered_handlers(
|
|
logger: logging.Logger | None = None,
|
|
) -> Iterator[HookableInMemoryHandler]:
|
|
if logger is None:
|
|
logger = logging.getLogger()
|
|
for handler in logger.handlers:
|
|
if isinstance(handler, HookableInMemoryHandler):
|
|
yield handler
|
|
|
|
@property
|
|
def log(self) -> str:
|
|
return self._log
|
|
|
|
def emit(self, record):
|
|
msg = self.format(record)
|
|
self._log += msg + "\n"
|
|
if self._hook is not None:
|
|
self._hook(msg)
|
|
|
|
def write(self, m):
|
|
pass
|
|
|
|
def clearLog(self) -> None:
|
|
self._log = ""
|
|
|
|
def setHook(self, hook: typing.Callable[[str], None]) -> None:
|
|
self._hook = hook
|
|
|
|
def clearHook(self) -> None:
|
|
self._hook = None
|