Chore: [ALAS] Abstract save_error_log for reusing

This commit is contained in:
LmeSzinc 2024-05-20 19:11:27 +08:00
parent 191f2f177f
commit 4445859671
3 changed files with 48 additions and 33 deletions

View File

@ -1,5 +1,3 @@
import os
import re
import threading import threading
import time import time
from datetime import datetime, timedelta 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.config import AzurLaneConfig, TaskEnd
from module.config.utils import deep_get, deep_set from module.config.utils import deep_get, deep_set
from module.exception import * from module.exception import *
from module.logger import logger from module.logger import logger, save_error_log
from module.notify import handle_notify from module.notify import handle_notify
@ -149,33 +147,7 @@ class AzurLaneAutoScript:
Save last 60 screenshots in ./log/error/<timestamp> Save last 60 screenshots in ./log/error/<timestamp>
Save logs to ./log/error/<timestamp>/log.txt Save logs to ./log/error/<timestamp>/log.txt
""" """
from module.base.utils import save_image save_error_log(config=self.config, device=self.device)
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)
def wait_until(self, future): def wait_until(self, future):
""" """

View File

@ -1,3 +1,2 @@
from .logger import logger from .error import save_error_log
from .logger import set_file_logger, set_func_logger from .logger import HTMLConsole, Highlighter, WEB_THEME, logger, set_file_logger, set_func_logger
from .logger import WEB_THEME, Highlighter, HTMLConsole

44
module/logger/error.py Normal file
View File

@ -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/<timestamp>
Save logs to ./log/error/<timestamp>/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)