From 44458596716e06930a91604a8a934e9dde6e816f Mon Sep 17 00:00:00 2001 From: LmeSzinc <37934724+LmeSzinc@users.noreply.github.com> Date: Mon, 20 May 2024 19:11:27 +0800 Subject: [PATCH] Chore: [ALAS] Abstract save_error_log for reusing --- module/alas.py | 32 ++-------------------------- module/logger/__init__.py | 5 ++--- module/logger/error.py | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 module/logger/error.py diff --git a/module/alas.py b/module/alas.py index 34d1d7b53..7663fc119 100644 --- a/module/alas.py +++ b/module/alas.py @@ -1,5 +1,3 @@ -import os -import re import threading import time from datetime import datetime, timedelta @@ -11,7 +9,7 @@ from module.base.decorator import del_cached_property from module.config.config import AzurLaneConfig, TaskEnd from module.config.utils import deep_get, deep_set from module.exception import * -from module.logger import logger +from module.logger import logger, save_error_log from module.notify import handle_notify @@ -149,33 +147,7 @@ class AzurLaneAutoScript: Save last 60 screenshots in ./log/error/ Save logs to ./log/error//log.txt """ - from module.base.utils import save_image - from module.handler.sensitive_info import (handle_sensitive_image, handle_sensitive_logs) - if self.config.Error_SaveError: - folder = f'./log/error/{int(time.time() * 1000)}' - logger.warning(f'Saving error: {folder}') - os.makedirs(folder, exist_ok=True) - for data in self.device.screenshot_deque: - image_time = datetime.strftime(data['time'], '%Y-%m-%d_%H-%M-%S-%f') - image = handle_sensitive_image(data['image']) - save_image(image, f'{folder}/{image_time}.png') - if self.device.screenshot_tracking: - os.makedirs(f'{folder}/tracking', exist_ok=True) - for data in self.device.screenshot_tracking: - image_time = datetime.strftime(data['time'], '%Y-%m-%d_%H-%M-%S-%f') - with open(f'{folder}/tracking/{image_time}.png', 'wb') as f: - f.write(data['image'].getvalue()) - with open(logger.log_file, 'r', encoding='utf-8') as f: - lines = f.readlines() - start = 0 - for index, line in enumerate(lines): - line = line.strip(' \r\t\n') - if re.match('^═{15,}$', line): - start = index - lines = lines[start - 2:] - lines = handle_sensitive_logs(lines) - with open(f'{folder}/log.txt', 'w', encoding='utf-8') as f: - f.writelines(lines) + save_error_log(config=self.config, device=self.device) def wait_until(self, future): """ diff --git a/module/logger/__init__.py b/module/logger/__init__.py index b584b3c1f..a4d045ddc 100644 --- a/module/logger/__init__.py +++ b/module/logger/__init__.py @@ -1,3 +1,2 @@ -from .logger import logger -from .logger import set_file_logger, set_func_logger -from .logger import WEB_THEME, Highlighter, HTMLConsole +from .error import save_error_log +from .logger import HTMLConsole, Highlighter, WEB_THEME, logger, set_file_logger, set_func_logger diff --git a/module/logger/error.py b/module/logger/error.py new file mode 100644 index 000000000..b2236d5ad --- /dev/null +++ b/module/logger/error.py @@ -0,0 +1,44 @@ +import os +import re +import time +from datetime import datetime + +from module.logger.logger import logger + + +def save_error_log(config, device): + """ + Save last 60 screenshots in ./log/error/ + Save logs to ./log/error//log.txt + + Args: + config: AzurLaneConfig object + device: Device object + """ + from module.base.utils import save_image + from module.handler.sensitive_info import (handle_sensitive_image, handle_sensitive_logs) + if config.Error_SaveError: + folder = f'./log/error/{int(time.time() * 1000)}' + logger.warning(f'Saving error: {folder}') + os.makedirs(folder, exist_ok=True) + for data in device.screenshot_deque: + image_time = datetime.strftime(data['time'], '%Y-%m-%d_%H-%M-%S-%f') + image = handle_sensitive_image(data['image']) + save_image(image, f'{folder}/{image_time}.png') + if device.screenshot_tracking: + os.makedirs(f'{folder}/tracking', exist_ok=True) + for data in device.screenshot_tracking: + image_time = datetime.strftime(data['time'], '%Y-%m-%d_%H-%M-%S-%f') + with open(f'{folder}/tracking/{image_time}.png', 'wb') as f: + f.write(data['image'].getvalue()) + with open(logger.log_file, 'r', encoding='utf-8') as f: + lines = f.readlines() + start = 0 + for index, line in enumerate(lines): + line = line.strip(' \r\t\n') + if re.match('^═{15,}$', line): + start = index + lines = lines[start - 2:] + lines = handle_sensitive_logs(lines) + with open(f'{folder}/log.txt', 'w', encoding='utf-8') as f: + f.writelines(lines)