diff --git a/alas.py b/alas.py index 8426935e1..f3f3f5082 100644 --- a/alas.py +++ b/alas.py @@ -1,5 +1,9 @@ +import os +import re +import time + from module.config.config import AzurLaneConfig -from module.logger import logger, pyw_name +from module.logger import logger, pyw_name, log_file class AzurLaneAutoScript: @@ -9,6 +13,28 @@ class AzurLaneAutoScript: ini_name = ini_name.lower() self.config = AzurLaneConfig(ini_name) + def run(self, command): + try: + self.__getattribute__(command.lower())() + except Exception as e: + logger.exception(e) + + if self.config.ENABLE_ERROR_LOG_AND_SCREENSHOT_SAVE: + folder = f'./log/error/{int(time.time() * 1000)}' + logger.info(f'Saving error: {folder}') + os.mkdir(folder) + for index, image in enumerate(logger.screenshot_deque): + image.save(f'{folder}/{index}.png') + with open(log_file, 'r') as f: + start = 0 + for index, line in enumerate(f.readlines()): + if re.search('\+-{15,}\+', line): + start = index + with open(log_file, 'r') as f: + text = f.readlines()[start - 2:] + with open(f'{folder}/log.txt', 'w') as f: + f.writelines(text) + def setting(self): for key, value in self.config.config['Setting'].items(): print(f'{key} = {value}') diff --git a/config/template.ini b/config/template.ini index 4bee4f20a..a093afd3c 100644 --- a/config/template.ini +++ b/config/template.ini @@ -78,6 +78,7 @@ urgent_ship = 155 command = emulator serial = 127.0.0.1:62001 package_name = com.bilibili.azurlane +enable_error_log_and_screenshot_save = yes enable_perspective_error_image_save = no use_adb_screenshot = yes use_adb_control = no diff --git a/module/config/argparser.py b/module/config/argparser.py index 9ed929110..cc5dfe92f 100644 --- a/module/config/argparser.py +++ b/module/config/argparser.py @@ -210,6 +210,7 @@ def main(ini_name=''): emulator.add_argument('--包名', default=default('--包名'), help='如果不是Biliibli国服, 或者使用了非官方客户端, 需修改') debug = emulator_parser.add_argument_group('调试设置', '') + debug.add_argument('--出错时保存log和截图', default=default('--出错时保存log和截图'), choices=['是', '否']) debug.add_argument('--保存透视识别出错的图像', default=default('--保存透视识别出错的图像'), choices=['是', '否']) adb = emulator_parser.add_argument_group('ADB设置', '') @@ -309,7 +310,4 @@ def main(ini_name=''): # Call AzurLaneAutoScript alas = AzurLaneAutoScript(ini_name=ini_name) - try: - alas.__getattribute__(command.lower())() - except Exception as e: - logger.exception(e) + alas.run(command=command) diff --git a/module/config/config.py b/module/config/config.py index 5d2fb8bb8..cb4453314 100644 --- a/module/config/config.py +++ b/module/config/config.py @@ -169,7 +169,10 @@ class AzurLaneConfig: """ error_log """ + PERSPECTIVE_ERROR_LOG_FOLDER = './log/perspective_error' ERROR_LOG_FOLDER = './log/error' + ENABLE_ERROR_LOG_AND_SCREENSHOT_SAVE = True + ENABLE_PERSPECTIVE_ERROR_IMAGE_SAVE = False """ module.map.fleet @@ -238,7 +241,7 @@ class AzurLaneConfig: ), MID_DIFF_RANGE ) - ENABLE_PERSPECTIVE_ERROR_IMAGE_SAVE = False + """ module.daemon """ @@ -291,7 +294,8 @@ class AzurLaneConfig: def create_folder(self): self.SCREEN_SHOT_SAVE_FOLDER = self.SCREEN_SHOT_SAVE_FOLDER_BASE + '/' + self.CAMPAIGN_NAME - for folder in [self.SCREEN_SHOT_SAVE_FOLDER_BASE, self.ASSETS_FOLDER, self.SCREEN_SHOT_SAVE_FOLDER, self.ERROR_LOG_FOLDER]: + for folder in [self.SCREEN_SHOT_SAVE_FOLDER_BASE, self.ASSETS_FOLDER, self.SCREEN_SHOT_SAVE_FOLDER, + self.PERSPECTIVE_ERROR_LOG_FOLDER, self.ERROR_LOG_FOLDER]: if not os.path.exists(folder): os.mkdir(folder) @@ -333,6 +337,7 @@ class AzurLaneConfig: option = config['Emulator'] self.SERIAL = option['serial'] self.PACKAGE_NAME = option['package_name'].strip() + self.ENABLE_ERROR_LOG_AND_SCREENSHOT_SAVE = to_bool(option['enable_error_log_and_screenshot_save']) self.ENABLE_PERSPECTIVE_ERROR_IMAGE_SAVE = to_bool(option['enable_perspective_error_image_save']) self.USE_ADB_SCREENSHOT = to_bool(option['use_adb_screenshot']) self.USE_ADB_CONTROL = to_bool(option['use_adb_control']) diff --git a/module/config/dictionary.py b/module/config/dictionary.py index 971b2e9c2..ab01bbcad 100644 --- a/module/config/dictionary.py +++ b/module/config/dictionary.py @@ -105,6 +105,7 @@ dic_chi_to_eng = { '观舰类紧急委托': 'urgent_ship', '设备': 'serial', '包名': 'package_name', + '出错时保存log和截图': 'enable_error_log_and_screenshot_save', '保存透视识别出错的图像': 'enable_perspective_error_image_save', '使用ADB截图': 'use_adb_screenshot', '使用ADB点击': 'use_adb_control', diff --git a/module/device/screenshot.py b/module/device/screenshot.py index 2b9769d17..c8900cd75 100644 --- a/module/device/screenshot.py +++ b/module/device/screenshot.py @@ -6,6 +6,7 @@ from PIL import Image from retrying import retry from module.device.connection import Connection +from module.logger import logger class Screenshot(Connection): @@ -61,6 +62,7 @@ class Screenshot(Connection): self.image = self._screenshot_uiautomator2() self.image.load() + logger.screenshot_deque.append(self.image) return self.image def save_screenshot(self, genre='items'): diff --git a/module/logger.py b/module/logger.py index 6fd9c6540..a23b03652 100644 --- a/module/logger.py +++ b/module/logger.py @@ -2,6 +2,7 @@ import logging import datetime import os import sys +from collections import deque pyw_name = os.path.splitext(os.path.basename(sys.argv[0]))[0] if f'{pyw_name}.pyw' not in os.listdir('./'): @@ -48,5 +49,6 @@ def attr(name, text): logger.hr = hr logger.attr = attr +logger.screenshot_deque = deque(maxlen=30) logger.hr('Start', level=0) diff --git a/module/map/perspective.py b/module/map/perspective.py index 67504be22..406431a10 100644 --- a/module/map/perspective.py +++ b/module/map/perspective.py @@ -406,5 +406,5 @@ class Perspective: return False file = '%s.%s' % (int(time.time() * 1000), 'png') - file = os.path.join(self.config.ERROR_LOG_FOLDER, file) + file = os.path.join(self.config.PERSPECTIVE_ERROR_LOG_FOLDER, file) self.image.save(file)