Skip to content

Commit a2cdf37

Browse files
committed
feat:增加清理多余日志功能
1 parent b36bc30 commit a2cdf37

7 files changed

Lines changed: 143 additions & 8 deletions

File tree

agent/custom/action.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
validate_mfa,
2121
wait_for_freezes,
2222
check_resolution,
23+
cleanup_maafw_bak_logs,
2324
)
2425

2526

@@ -356,3 +357,34 @@ def run(
356357
except Exception as e:
357358
logger.error(f"非线性滑动执行失败: {str(e)}")
358359
return CustomAction.RunResult(success=False)
360+
361+
362+
@AgentServer.custom_action("CleanupMaafwBakLogs")
363+
class CleanupMaafwBakLogs(CustomAction):
364+
"""
365+
清理maafw.bak日志文件
366+
参数:{"save_log_count": 保留日志数量}
367+
默认留3个
368+
"""
369+
370+
def run(
371+
self,
372+
context: Context,
373+
argv: CustomAction.RunArg,
374+
) -> CustomAction.RunResult:
375+
try:
376+
keep_count = 3 # 与你的函数默认值保持一致
377+
if argv.custom_action_param:
378+
param_dict = json.loads(argv.custom_action_param)
379+
count_value = param_dict.get("save_log_count", "")
380+
# 安全解析数字
381+
if count_value and str(count_value).isdigit():
382+
keep_count = int(count_value)
383+
384+
cleanup_maafw_bak_logs(context, keep_count=keep_count)
385+
386+
return CustomAction.RunResult(success=True)
387+
388+
except Exception as e:
389+
print(f"日志清理执行异常: {e}")
390+
return CustomAction.RunResult(success=False)

agent/custom/sink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
def is_aspect_ratio_16x9(width: int, height: int) -> bool:
2222
"""
2323
检查给定的尺寸是否大约为 16:9
24-
同时处理横屏16:9和竖屏9:16方向
24+
同时处理横屏(16:9)和竖屏(9:16)方向
2525
"""
2626
if width <= 0 or height <= 0:
2727
return False

agent/custom/utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,43 @@ def nonlinear_swipe(
301301

302302
def send_notification(title: str = "系统通知", msg: str = "这是一条测试消息"):
303303
Notify(title, msg, "MaaAutoNaruto", logo.__str__()).send()
304+
305+
306+
def cleanup_maafw_bak_logs(context=None, keep_count: int = 3):
307+
import sys
308+
from pathlib import Path
309+
310+
root = Path(__file__).parent.parent.parent
311+
sys.path.insert(0, str(root))
312+
313+
try:
314+
debug_folder = root / "debug"
315+
316+
if not debug_folder.exists():
317+
print("[日志清理] debug文件夹不存在")
318+
return
319+
320+
log_files = list(debug_folder.glob("maafw.bak.*.log"))
321+
print(f"[日志清理] 找到日志总数:{len(log_files)}")
322+
323+
if not log_files:
324+
print("[日志清理] 无符合格式的日志")
325+
return
326+
327+
# 按时间从新到旧排序
328+
log_files.sort(reverse=True)
329+
to_delete = log_files[keep_count:]
330+
print(f"[日志清理] 待删除旧日志:{len(to_delete)} 个")
331+
332+
# 删除
333+
for f in to_delete:
334+
try:
335+
f.unlink()
336+
print(f"[日志清理] 已删除:{f.name}")
337+
except Exception as e:
338+
print(f"[日志清理] 删除失败 {f.name}:{e}")
339+
340+
print(f"[日志清理] 完成!保留最新 {keep_count} 个日志")
341+
342+
except Exception as e:
343+
print(f"[日志清理] 异常:{e}")

assets/interface.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@
377377
"display_touch"
378378
]
379379
},
380+
{
381+
"name": "日志清理",
382+
"entry": "cleanup_maafw_bak_logs",
383+
"option": [
384+
"保留备份日志个数"
385+
],
386+
"default_check": true,
387+
"description": "清理多余的日志,节省空间"
388+
},
380389
{
381390
"name": "完成标记",
382391
"entry": "complete",
@@ -3380,6 +3389,27 @@
33803389
}
33813390
}
33823391
]
3392+
},
3393+
"保留备份日志个数": {
3394+
"description": "清理日志时保留备份日志个数",
3395+
"type": "input",
3396+
"inputs": [
3397+
{
3398+
"name": "保留日志数",
3399+
"label": "保留日志数",
3400+
"description": "输入需要的保留日志数,最大999",
3401+
"pipeline_type": "int",
3402+
"default": "3",
3403+
"verify": "^[1-9]\\d{0,2}$"
3404+
}
3405+
],
3406+
"pipeline_override": {
3407+
"cleanup_maafw_bak_logs": {
3408+
"custom_action_param": {
3409+
"save_log_count": "{保留日志数}"
3410+
}
3411+
}
3412+
}
33833413
}
33843414
}
33853415
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"complete": {
3+
"recognition": "DirectHit",
4+
"pre_delay": 0,
5+
"post_delay": 0,
6+
"$doc": "用来标记全部任务已经完成,和auto_mas配合",
7+
"focus": "OK"
8+
},
9+
"cleanup_maafw_bak_logs": {
10+
"recognition": "DirectHit",
11+
"action": "Custom",
12+
"custom_action": "CleanupMaafwBakLogs",
13+
"custom_action_param": {
14+
"save_log_count": "3"
15+
},
16+
"pre_delay": 0,
17+
"post_delay": 0,
18+
"$doc": "清理多余的maafw_bak日志,节省空间"
19+
}
20+
}

assets/resource/base/pipeline/Share.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,5 @@
330330
"post_delay": 0,
331331
"post_wait_freezes": 200,
332332
"focus": "[color:darkgray]关闭周榜更新提示[/color]"
333-
},
334-
"complete": {
335-
"recognition": "DirectHit",
336-
"pre_delay": 0,
337-
"post_delay": 0,
338-
"focus": "OK"
339333
}
340334
}

deps/tools/custom.action.schema.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"StopTaskList",
1111
"RetryFailed",
1212
"CounterIncrement",
13-
"NonlinearSwipe"
13+
"NonlinearSwipe",
14+
"CleanupMaafwBakLogs"
1415
]
1516
},
1617
"custom_action_param": {
@@ -35,6 +36,9 @@
3536
},
3637
{
3738
"$ref": "#/$defs/NonlinearSwipe"
39+
},
40+
{
41+
"$ref": "#/$defs/CleanupMaafwBakLogsParam"
3842
}
3943
]
4044
}
@@ -123,6 +127,21 @@
123127
"description": "调用非线性滑动",
124128
"markdownDescription": "非线性滑动\n\n**功能说明:**\n- 进行非线性滑动\n\n**返回值:**\n- `success`正常时返回 true",
125129
"properties": {}
130+
},
131+
"CleanupMaafwBakLogsParam": {
132+
"type": "object",
133+
"description": "清理debug目录下的maafw.bak日志文件,保留指定数量的最新日志",
134+
"markdownDescription": "清理maafw.bak日志文件\n\n**功能说明:**\n- 扫描项目根目录下 debug 文件夹\n- 匹配格式:maafw.bak.*.log\n- 按文件名时间戳从新到旧排序\n- 删除超出指定数量的旧日志文件\n- 无依赖项目日志器,使用控制台输出\n\n**参数说明:**\n- `save_log_count`:保留的最新日志文件数量\n - 类型:整数(数字)\n - 非必填,默认值:3\n - 示例:`5` 表示保留最新5个日志\n\n**返回值:**\n- `success`:执行完成始终返回 true",
135+
"properties": {
136+
"save_log_count": {
137+
"title": "Save Log Count",
138+
"description": "需要保留的最新日志文件数量,默认值为3",
139+
"type": "integer",
140+
"minimum": 1,
141+
"default": 3,
142+
"markdownDescription": "*integer*\n\n保留的最新日志文件数量.默认值:3最小值:1"
143+
}
144+
}
126145
}
127146
}
128147
}

0 commit comments

Comments
 (0)