修看门狗日志编码:*>> 在 PS5.1 默认写 UTF-16LE,污染同一个日志文件

现场(2026-10-12 注册计划任务后实测):runner-stdout.log 尾部按 UTF-8 读出来
是 "t i m e =   2 0 2 6 - 1 0 - 1 2 ...",即每个字节后面跟一个 NUL。

根因正是我自己刚写进文档的那条坑:PS 5.1 的 *>> 重定向由 Out-File 实现,而
Out-File 的默认编码是 UTF-16LE。同一个文件里 Log() 用 Add-Content -Encoding
ascii 写 ASCII 行、daemon 输出却以 UTF-16LE 追加,于是"一个文件两种编码",
排障时按 UTF-8 读就是乱码——等于把唯一的 runner 现场日志弄成不可读。

- daemon 输出改到独立文件 runner-daemon.log,显式 Out-File -Encoding utf8
  (不用 ascii:提交标题与作业输出可能含非 ASCII,ascii 会打成问号)
- 看门狗自身日志改名 runner-watchdog.log,与 daemon 输出彻底分离
- 模板与生成物均校验通过:parseErrors=0、nonASCII=0
- 旧 runner-stdout.log 保留作历史,不再写入
This commit is contained in:
2026-10-12 13:21:06 +08:00
parent 038e2cfba3
commit e6bcad86cb
+13 -3
View File
@@ -338,7 +338,8 @@ $runnerDir = '__RUNNER_DIR__'
$runnerExe = Join-Path $runnerDir 'gitea-runner.exe'
$runnerCfg = Join-Path $runnerDir 'config.yaml'
$logDir = Join-Path $runnerDir 'log'
$log = Join-Path $logDir 'runner-stdout.log'
$log = Join-Path $logDir 'runner-watchdog.log'
$daemonLog = Join-Path $logDir 'runner-daemon.log'
$probeUrl = '__GITEA_URL__/api/v1/version'
$maxWaitSec = 1800
@@ -371,7 +372,15 @@ while ($true) {
}
Log 'starting daemon'
& $runnerExe daemon --config $runnerCfg *>> $log
# MEASURED TRAP (2026-10-12, hit by this very script): in PowerShell 5.1 the
# *>> redirection is implemented with Out-File, whose DEFAULT ENCODING IS
# UTF-16LE. Appending that into a file whose other lines come from
# Add-Content -Encoding ascii leaves ONE FILE WITH TWO ENCODINGS, and the
# daemon lines read back as "t i m e = 2 0 2 6 ..." (a NUL after every byte).
# Never rely on a redirection's default encoding -- state it explicitly.
# utf8 (not ascii) because job/commit text can legitimately be non-ASCII.
& $runnerExe daemon --config $runnerCfg 2>&1 |
Out-File -FilePath $daemonLog -Append -Encoding utf8
Log ("daemon exited, exit code {0}; restarting in 10s" -f $LASTEXITCODE)
Start-Sleep -Seconds 10
}
@@ -415,7 +424,8 @@ if ($StartNow) {
Warn "Gitea is not answering at $GiteaUrl right now -- the watchdog will keep waiting"
}
Start-ScheduledTask -TaskName $TaskName
Ok "task started; watch: Get-Content '$(Join-Path $RunnerDir 'log\runner-stdout.log')' -Tail 20"
Ok "task started; daemon log: Get-Content '$(Join-Path $RunnerDir 'log\runner-daemon.log')' -Tail 20"
Ok "watchdog log: Get-Content '$(Join-Path $RunnerDir 'log\runner-watchdog.log')' -Tail 20"
Info 'verify on the web UI: repo -> Settings -> Actions -> Runners -> win-stm32-ci should turn Idle'
}