From ada27b92efe9ef3b9589fe08037adf1e258aa767 Mon Sep 17 00:00:00 2001 From: Snowdash Date: Sun, 8 Sep 2024 10:13:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E7=BA=BF=E7=A8=8B=E4=BB=A5?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=97=A7=E6=88=AA=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免检查截图时间影响截图 --- arknights_mower/utils/log.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/arknights_mower/utils/log.py b/arknights_mower/utils/log.py index d59dfa1f..c3f49a2d 100644 --- a/arknights_mower/utils/log.py +++ b/arknights_mower/utils/log.py @@ -3,6 +3,7 @@ import sys import time import traceback +import threading from logging.handlers import RotatingFileHandler from pathlib import Path @@ -31,7 +32,6 @@ def filter(self, record: logging.LogRecord) -> bool: filter = PackagePathFilter() - logger = logging.getLogger(__name__) logger.setLevel("DEBUG") @@ -72,7 +72,18 @@ def save_screenshot(img: bytes) -> None: folder = get_path("@app/screenshot") folder.mkdir(exist_ok=True, parents=True) time_ns = time.time_ns() - start_time_ns = time_ns - config.conf.screenshot * 3600 * 10**9 + start_time_ns = time_ns - config.conf.screenshot * 3600 * 10 ** 9 + checking_thread = threading.Thread(target=check_old_screenshot, + kwargs={"start_time_ns": start_time_ns, + "folder": folder}) + checking_thread.start() + filename = f"{time_ns}.jpg" + with folder.joinpath(filename).open("wb") as f: + f.write(img) + logger.debug(f"save screenshot: {filename}") + + +def check_old_screenshot(start_time_ns, folder): for i in folder.iterdir(): if i.is_dir(): shutil.rmtree(i) @@ -80,7 +91,3 @@ def save_screenshot(img: bytes) -> None: i.unlink() elif int(i.stem) < start_time_ns: i.unlink() - filename = f"{time_ns}.jpg" - with folder.joinpath(filename).open("wb") as f: - f.write(img) - logger.debug(f"save screenshot: {filename}")