feat: 添加工具以查找重复的异常处理,增强代码质量检查;新增验证 GUI 回退方式的自检脚本
This commit is contained in:
1 parent
982357927e
commit
43e39f4db6
16 files changed
+472
-180
No files matched your search
+14
-15
@@ -57,6 +57,7 @@ def list_adb_devices(adb_exe: str) -> list[str]:
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
check=False,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
@@ -118,6 +119,12 @@ class ProcRunner:
|
||||
"updated_ts": None,
|
||||
}
|
||||
|
||||
# 延迟属性:预先声明,避免静态检查报“在 __init__ 外定义属性”
|
||||
self._log_permission_warned: bool = False
|
||||
self._log_write_error_warned: bool = False
|
||||
self._current_trace_id: str = ""
|
||||
self._mdc_context: dict[str, str] = {}
|
||||
|
||||
# 事件流(用于趋势/分布/Top 统计),保留最近 N 条
|
||||
self._events: list[dict] = []
|
||||
self._events_cap: int = 5000
|
||||
@@ -166,13 +173,13 @@ class ProcRunner:
|
||||
f.flush() # 确保立即写入磁盘,避免缓冲区延迟导致日志丢失
|
||||
except PermissionError as e:
|
||||
# 权限错误,只输出一次警告避免刷屏
|
||||
if not hasattr(self, "_log_permission_warned"):
|
||||
if not self._log_permission_warned:
|
||||
print(f"[WEB_GUI] 警告:日志文件写入权限错误: {self._log_file_path}")
|
||||
print(f"[WEB_GUI] 错误详情: {e}")
|
||||
self._log_permission_warned = True
|
||||
except Exception as e:
|
||||
# 其他文件写失败不应影响主流程,但记录第一次错误以便调试
|
||||
if not hasattr(self, "_log_write_error_warned"):
|
||||
if not self._log_write_error_warned:
|
||||
print(f"[WEB_GUI] 警告:日志文件写入失败: {self._log_file_path}")
|
||||
print(f"[WEB_GUI] 错误详情: {e}")
|
||||
self._log_write_error_warned = True
|
||||
@@ -277,7 +284,7 @@ class ProcRunner:
|
||||
if trace_id_match:
|
||||
trace_id = trace_id_match.group(1)
|
||||
# 存储当前traceId(用于统计和显示)
|
||||
if not hasattr(self, "_current_trace_id"):
|
||||
if not self._current_trace_id:
|
||||
self._current_trace_id = trace_id
|
||||
elif trace_id != "N/A":
|
||||
self._current_trace_id = trace_id
|
||||
@@ -290,8 +297,6 @@ class ProcRunner:
|
||||
mdc_pairs = re.findall(r"(\w+)=([^,\s]+)", mdc_str)
|
||||
for key, value in mdc_pairs:
|
||||
# 存储MDC信息(可用于统计)
|
||||
if not hasattr(self, "_mdc_context"):
|
||||
self._mdc_context = {}
|
||||
self._mdc_context[key] = value
|
||||
|
||||
# --- 福袋:抽取"最近一次"识别到的信息 ---
|
||||
@@ -1424,7 +1429,7 @@ class ProcRunner:
|
||||
self._append_log(f"[网页] 读取线程启动,监视 PID={p.pid}")
|
||||
out = p.stdout
|
||||
if out is None:
|
||||
self._append_log(f"[网页] 读取线程:stdout 为 None,无法读取子进程输出")
|
||||
self._append_log("[网页] 读取线程:stdout 为 None,无法读取子进程输出")
|
||||
try:
|
||||
rc = p.wait()
|
||||
except Exception:
|
||||
@@ -1448,7 +1453,7 @@ class ProcRunner:
|
||||
pass
|
||||
except BrokenPipeError:
|
||||
# 进程已关闭stdout
|
||||
self._append_log(f"[网页] 读取线程:子进程stdout已关闭")
|
||||
self._append_log("[网页] 读取线程:子进程stdout已关闭")
|
||||
except Exception as e:
|
||||
self._append_log(f"[网页] 读取线程异常:{e}")
|
||||
|
||||
@@ -1456,7 +1461,7 @@ class ProcRunner:
|
||||
rc = p.wait(timeout=1)
|
||||
except subprocess.TimeoutExpired:
|
||||
rc = None
|
||||
self._append_log(f"[网页] 等待进程退出超时,强制终止")
|
||||
self._append_log("[网页] 等待进程退出超时,强制终止")
|
||||
try:
|
||||
p.kill()
|
||||
rc = p.wait(timeout=5)
|
||||
@@ -1691,7 +1696,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if not safe.startswith("pic/"):
|
||||
self.send_error(HTTPStatus.FORBIDDEN)
|
||||
return
|
||||
full = os.path.join(path_config.PROJECT_ROOT, safe)
|
||||
full = os.path.join(str(path_config.PROJECT_ROOT), safe)
|
||||
if not os.path.isfile(full):
|
||||
self.send_error(HTTPStatus.NOT_FOUND)
|
||||
return
|
||||
@@ -1893,12 +1898,6 @@ class Handler(BaseHTTPRequestHandler):
|
||||
|
||||
self.send_error(HTTPStatus.NOT_FOUND)
|
||||
|
||||
def log_message(self, format, *args): # type: ignore[override]
|
||||
# 覆盖基类方法以抑制日志输出
|
||||
# 参数名从 fmt 改为 format 以匹配基类签名
|
||||
return
|
||||
|
||||
|
||||
def main() -> int:
|
||||
host = "127.0.0.1"
|
||||
# 支持 PORT / WEB_GUI_PORT 两种环境变量,默认 8000
|
||||
|
||||
Reference in new issue
Block a user