Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: upper limit of errors and logs #3729

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions alas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import shutil
import threading
import time
from datetime import datetime, timedelta
Expand Down Expand Up @@ -131,20 +132,41 @@ def run(self, command):
)
exit(1)

def keep_last_errlog(self, folder_path, n: int = 30):
"""

Keep last n folders in folder_path, delete others.
If n is negative or 0, do nothing.(Keep all errlog folders)

Args:
folder_path (str): Path to folder.\n
n (int): Number of folders to keep.
"""
if n <= 0:
return
folders = [
os.path.join(folder_path, f)
for f in os.listdir(folder_path)
if os.path.isdir(os.path.join(folder_path, f))
]
for folder in folders[:-n]:
shutil.rmtree(folder)

def save_error_log(self):
"""
Save last 60 screenshots in ./log/error/<timestamp>
Save logs to ./log/error/<timestamp>/log.txt
Save last 60 screenshots in ./log/error/<config-name>/<timestamp>
Save logs to ./log/error/<config-name>/<timestamp>/log.txt
"""
import pathlib
from module.base.utils import save_image
from module.handler.sensitive_info import (handle_sensitive_image,
handle_sensitive_logs)
if self.config.Error_SaveError:
if not os.path.exists('./log/error'):
os.mkdir('./log/error')
folder = f'./log/error/{int(time.time() * 1000)}'
config_folder = pathlib.Path(f"./log/error/{self.config_name}")
folder = config_folder.joinpath(str(int(time.time() * 1000)))
folder.mkdir(parents=True, exist_ok=True)
logger.warning(f'Saving error: {folder}')
os.mkdir(folder)

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'])
Expand All @@ -160,6 +182,7 @@ def save_error_log(self):
lines = handle_sensitive_logs(lines)
with open(f'{folder}/log.txt', 'w', encoding='utf-8') as f:
f.writelines(lines)
self.keep_last_errlog(config_folder, self.config.Error_SaveErrorCount)

def restart(self):
from module.handler.login import LoginHandler
Expand Down
6 changes: 6 additions & 0 deletions config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Error": {
"HandleError": true,
"SaveError": true,
"SaveErrorCount": 30,
"OnePushConfig": "provider: null",
"ScreenshotLength": 1
},
Expand All @@ -42,6 +43,11 @@
}
},
"General": {
"Log": {
"LogKeepCount": 7,
"LogBackUpMethod": "delete",
"ZipMethod": "bz2"
},
"Retirement": {
"RetireMode": "one_click_retire"
},
Expand Down
2 changes: 1 addition & 1 deletion gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def func(ev: threading.Event):
should_exit = False
while not should_exit:
event = Event()
process = Process(target=func, args=(event,))
process = Process(target=func, args=(event,), name="gui")
process.start()
while not should_exit:
try:
Expand Down
29 changes: 29 additions & 0 deletions module/config/argument/args.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@
"type": "checkbox",
"value": true
},
"SaveErrorCount": {
"type": "input",
"value": 30
},
"OnePushConfig": {
"type": "textarea",
"value": "provider: null",
Expand Down Expand Up @@ -296,6 +300,31 @@
}
},
"General": {
"Log": {
"LogKeepCount": {
"type": "input",
"value": 7
},
"LogBackUpMethod": {
"type": "select",
"value": "delete",
"option": [
"delete",
"zip",
"copy"
]
},
"ZipMethod": {
"type": "select",
"value": "bz2",
"option": [
"bz2",
"gzip",
"xz",
"zip"
]
}
},
"Retirement": {
"RetireMode": {
"type": "select",
Expand Down
9 changes: 9 additions & 0 deletions module/config/argument/argument.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ EmulatorInfo:
Error:
HandleError: true
SaveError: true
SaveErrorCount: 30
OnePushConfig:
type: textarea
mode: yaml
Expand Down Expand Up @@ -119,6 +120,14 @@ DropRecord:
MeowfficerTalent:
value: do_not
option: [ do_not, save, upload, save_and_upload ]
Log:
LogKeepCount: 7
LogBackUpMethod:
value: delete
option: [ delete, zip, copy ]
ZipMethod:
value: bz2
option: [ bz2, gzip, xz, zip ]
Retirement:
RetireMode:
value: one_click_retire
Expand Down
1 change: 1 addition & 0 deletions module/config/argument/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Alas:
- Optimization
- DropRecord
General:
- Log
- Retirement
- OneClickRetire
- Enhance
Expand Down
6 changes: 6 additions & 0 deletions module/config/config_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class GeneratedConfig:
# Group `Error`
Error_HandleError = True
Error_SaveError = True
Error_SaveErrorCount = 30
Error_OnePushConfig = 'provider: null'
Error_ScreenshotLength = 1

Expand All @@ -54,6 +55,11 @@ class GeneratedConfig:
DropRecord_MeowfficerBuy = 'do_not' # do_not, save
DropRecord_MeowfficerTalent = 'do_not' # do_not, save, upload, save_and_upload

# Group `Log`
Log_LogKeepCount = 7
Log_LogBackUpMethod = 'delete' # delete, zip, copy
Log_ZipMethod = 'bz2' # bz2, gzip, xz, zip

# Group `Retirement`
Retirement_RetireMode = 'one_click_retire' # one_click_retire, enhance, old_retire

Expand Down
29 changes: 29 additions & 0 deletions module/config/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@
"name": "Record Exception",
"help": "Records exception and log into directory for review or sharing"
},
"SaveErrorCount": {
"name": "Error storage limit",
"help": "Error logs that exceed will be deleted, unlimited if it is negative"
},
"OnePushConfig": {
"name": "Error notify config",
"help": "When Alas cannot handle exception, send a message through Onepush. Configuration document: \nhttps://github.com/LmeSzinc/AzurLaneAutoScript/wiki/Onepush-configuration-%5BEN%5D"
Expand Down Expand Up @@ -573,6 +577,31 @@
"save_and_upload": "Save and upload"
}
},
"Log": {
"_info": {
"name": "Log",
"help": "General settings, effective after the scheduler is restarted"
},
"LogKeepCount": {
"name": "Count of log rotation",
"help": "Number of days to rotate logs"
},
"LogBackUpMethod": {
"name": "Log backup method",
"help": "Back up logs or delete expired logs directly",
"delete": "delete",
"zip": "create zip in ./log/bak",
"copy": "copy to ./log/bak"
},
"ZipMethod": {
"name": "Zip Method",
"help": "Log.ZipMethod.help",
"bz2": "bz2",
"gzip": "gzip",
"xz": "xz",
"zip": "zip"
}
},
"Retirement": {
"_info": {
"name": "Retirement Settings",
Expand Down
29 changes: 29 additions & 0 deletions module/config/i18n/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@
"name": "Error.SaveError.name",
"help": "Error.SaveError.help"
},
"SaveErrorCount": {
"name": "Error.SaveErrorCount.name",
"help": "Error.SaveErrorCount.help"
},
"OnePushConfig": {
"name": "Error.OnePushConfig.name",
"help": "Error.OnePushConfig.help"
Expand Down Expand Up @@ -573,6 +577,31 @@
"save_and_upload": "save_and_upload"
}
},
"Log": {
"_info": {
"name": "Log._info.name",
"help": "Log._info.help"
},
"LogKeepCount": {
"name": "Log.LogKeepCount.name",
"help": "Log.LogKeepCount.help"
},
"LogBackUpMethod": {
"name": "Log.LogBackUpMethod.name",
"help": "Log.LogBackUpMethod.help",
"delete": "delete",
"zip": "zip",
"copy": "copy"
},
"ZipMethod": {
"name": "Log.ZipMethod.name",
"help": "Log.ZipMethod.help",
"bz2": "bz2",
"gzip": "gzip",
"xz": "xz",
"zip": "zip"
}
},
"Retirement": {
"_info": {
"name": "Retirement._info.name",
Expand Down
29 changes: 29 additions & 0 deletions module/config/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@
"name": "出错时,保存 Log 和截图",
"help": ""
},
"SaveErrorCount": {
"name": "Error Log 保存上限",
"help": "超出上限的错误日志将被删除,若为0或负值则没有限制"
},
"OnePushConfig": {
"name": "错误推送设置",
"help": "发生无法处理的异常后,使用 Onepush 推送一条错误信息。配置方法见文档:https://github.com/LmeSzinc/AzurLaneAutoScript/wiki/Onepush-configuration-%5BCN%5D"
Expand Down Expand Up @@ -573,6 +577,31 @@
"save_and_upload": "保存并上传"
}
},
"Log": {
"_info": {
"name": "日志",
"help": "调度器重启后生效"
},
"LogKeepCount": {
"name": "Log保留数量",
"help": "Log过期时间(天)"
},
"LogBackUpMethod": {
"name": "过期Log处理方式",
"help": "备份目录为./log/bak",
"delete": "删除",
"zip": "压缩备份",
"copy": "拷贝备份"
},
"ZipMethod": {
"name": "压缩格式",
"help": "",
"bz2": "bz2",
"gzip": "gzip",
"xz": "xz",
"zip": "zip"
}
},
"Retirement": {
"_info": {
"name": "退役设置",
Expand Down
29 changes: 29 additions & 0 deletions module/config/i18n/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@
"name": "出錯時,保存 Log 和截圖",
"help": ""
},
"SaveErrorCount": {
"name": "Error Log 保存上限",
"help": "超出上限的錯誤日志將被刪除, 若爲0或負數則沒有限制"
},
"OnePushConfig": {
"name": "錯誤推送設定",
"help": "發生無法處理的異常後,使用 Onepush 推送错误消息。設定參考文檔:https://github.com/LmeSzinc/AzurLaneAutoScript/wiki/Onepush-configuration-%5BCN%5D"
Expand Down Expand Up @@ -573,6 +577,31 @@
"save_and_upload": "保存並上傳"
}
},
"Log": {
"_info": {
"name": "Log",
"help": "調度器重啟後生效"
},
"LogKeepCount": {
"name": "Log保留數量",
"help": "Log過期時間(天)"
},
"LogBackUpMethod": {
"name": "Log處理方式",
"help": "備份目錄為./log/bak",
"delete": "刪除",
"zip": "壓縮備份",
"copy": "copy"
},
"ZipMethod": {
"name": "壓縮格式",
"help": "",
"bz2": "bz2",
"gzip": "gzip",
"xz": "xz",
"zip": "zip"
}
},
"Retirement": {
"_info": {
"name": "退役設定",
Expand Down
Loading