From 81c20a0b5c5b35606905aee5e988485508cf96e2 Mon Sep 17 00:00:00 2001 From: 18870 Date: Wed, 8 Feb 2023 16:37:11 +0800 Subject: [PATCH] Opt: Remove log output to stdout when using electron Try to avoid #2051 --- gui.py | 14 +++++++ module/logger.pyi | 68 ++++++++++++++++----------------- module/webui/app.py | 5 --- module/webui/process_manager.py | 13 +++++++ 4 files changed, 60 insertions(+), 40 deletions(-) diff --git a/gui.py b/gui.py index f3206b71c..97b5f3cf4 100644 --- a/gui.py +++ b/gui.py @@ -1,6 +1,7 @@ import threading from multiprocessing import Event, Process +from module.logger import logger from module.webui.setting import State @@ -49,6 +50,19 @@ def func(ev: threading.Event): host = args.host or State.deploy_config.WebuiHost or "0.0.0.0" port = args.port or int(State.deploy_config.WebuiPort) or 22267 + State.electron = args.electron + + logger.hr("Launcher config") + logger.attr("Host", host) + logger.attr("Port", port) + logger.attr("Electron", args.electron) + logger.attr("Reload", ev is not None) + + if State.electron: + # https://github.com/LmeSzinc/AzurLaneAutoScript/issues/2051 + logger.info("Electron detected, remove log output to stdout") + from module.logger import console_hdlr + logger.removeHandler(console_hdlr) uvicorn.run("module.webui.app:app", host=host, port=port, factory=True) diff --git a/module/logger.pyi b/module/logger.pyi index 36ce22ffd..cfa47f3fc 100644 --- a/module/logger.pyi +++ b/module/logger.pyi @@ -3,15 +3,12 @@ from typing import Any, Callable from rich.console import Console, ConsoleRenderable from rich.highlighter import RegexHighlighter +from rich.logging import RichHandler from rich.theme import Theme - class HTMLConsole(Console): ... - - class Highlighter(RegexHighlighter): ... - WEB_THEME: Theme logger_debug: bool @@ -22,53 +19,54 @@ console_formatter: logging.Formatter web_formatter: logging.Formatter stdout_console: Console +console_hdlr: RichHandler +def set_file_logger( + name: str = pyw_name, +) -> None: ... +def set_func_logger( + func: Callable[[ConsoleRenderable], None], +) -> None: ... class __logger(logging.Logger): def rule( - self, - title: str = "", - *, - characters: str = "-", - style: str = "rule.line", - end: str = "\n", - align: str = "center", + self, + title: str = "", + *, + characters: str = "-", + style: str = "rule.line", + end: str = "\n", + align: str = "center", ) -> None: ... - def hr( - self, - title, - level: int = 3, + self, + title, + level: int = 3, ) -> None: ... - def attr( - self, - name, - text, + self, + name, + text, ) -> None: ... - def attr_align( - self, - name, - text, - front = "", - align: int = 22, + self, + name, + text, + front="", + align: int = 22, ) -> None: ... - def set_file_logger( - self, - name: str = pyw_name, + self, + name: str = pyw_name, ) -> None: ... - def set_func_logger( - func: Callable[[Any], Any], + self, + func: Callable[[ConsoleRenderable], None], ) -> None: ... - def print( - self, - *objects: ConsoleRenderable, - **kwargs, + self, + *objects: ConsoleRenderable, + **kwargs, ) -> None: ... - logger: __logger diff --git a/module/webui/app.py b/module/webui/app.py index cb7bcd517..7ddf2a2d5 100644 --- a/module/webui/app.py +++ b/module/webui/app.py @@ -1207,9 +1207,6 @@ def app(): action="store_true", help="Use jsdelivr cdn for pywebio static files (css, js). Self host cdn by default.", ) - parser.add_argument( - "--electron", action="store_true", help="Runs by electron client." - ) parser.add_argument( "--run", nargs="+", @@ -1223,7 +1220,6 @@ def app(): lang.LANG = State.deploy_config.Language key = args.key or State.deploy_config.Password cdn = args.cdn if args.cdn else State.deploy_config.CDN - State.electron = args.electron runs = None if args.run: runs = args.run @@ -1238,7 +1234,6 @@ def app(): logger.attr("Language", lang.LANG) logger.attr("Password", True if key else False) logger.attr("CDN", cdn) - logger.attr("Electron", args.electron) def index(): if key is not None and not login(key): diff --git a/module/webui/process_manager.py b/module/webui/process_manager.py index 0c9f4ac69..b113a013c 100644 --- a/module/webui/process_manager.py +++ b/module/webui/process_manager.py @@ -1,3 +1,4 @@ +import argparse import os import queue import threading @@ -119,8 +120,20 @@ class ProcessManager: def run_process( config_name, func: str, q: queue.Queue, e: threading.Event = None ) -> None: + parser = argparse.ArgumentParser() + parser.add_argument( + "--electron", action="store_true", help="Runs by electron client." + ) + args, _ = parser.parse_known_args() + State.electron = args.electron + # Setup logger set_file_logger(name=config_name) + if State.electron: + # https://github.com/LmeSzinc/AzurLaneAutoScript/issues/2051 + logger.info("Electron detected, remove log output to stdout") + from module.logger import console_hdlr + logger.removeHandler(console_hdlr) set_func_logger(func=q.put) from module.config.config import AzurLaneConfig