This commit is contained in:
zaiun xu
2026-04-13 13:49:55 +08:00
parent 62bff77fa0
commit 5a0d7ba11b
4 changed files with 85 additions and 107 deletions

View File

@@ -1,4 +1,4 @@
"""子进程运行并把 stdout/stderr 流式写入 loguru便于查看 FishMeasure / FishAction 中间输出"""
"""子进程运行并把 stdout/stderr 合并;可选将逐行输出写入 loguru测量/行为推理可关闭以减少日志)"""
from __future__ import annotations
@@ -15,10 +15,12 @@ def run_subprocess_with_log(
cwd: str,
env: Optional[Dict[str, str]] = None,
log_name: str,
stream_to_logger: bool = True,
) -> subprocess.CompletedProcess[str]:
"""运行子进程,合并 stderr 到 stdout按行输出到 loguru。
"""运行子进程,合并 stderr 到 stdout可选按行输出到 loguru。
返回 CompletedProcessstdout 为完整输出,便于失败时拼进异常信息。
``stream_to_logger=False`` 时不把子进程逐行写入日志(仍完整收集 stdout
"""
proc = subprocess.Popen(
cmd,
@@ -33,9 +35,10 @@ def run_subprocess_with_log(
if proc.stdout is not None:
for line in proc.stdout:
lines.append(line)
s = line.rstrip()
if s:
logger.info("[{}] {}", log_name, s)
if stream_to_logger:
s = line.rstrip()
if s:
logger.info("[{}] {}", log_name, s)
rc = proc.wait()
out = "".join(lines)
return subprocess.CompletedProcess(cmd, rc, out, "")