"""Print launch diagnostics when /health did not answer. Usage: diagnose_start.py """ from __future__ import annotations import sys from pathlib import Path def tail(path: Path, n: int = 30) -> str: try: text = path.read_text(encoding="utf-8", errors="replace") except Exception as exc: return f"(cannot read {path}: {exc})" lines = text.splitlines() return "\n".join(lines[-n:]) def main() -> int: port = sys.argv[1] if len(sys.argv) > 1 else "?" log = Path(sys.argv[2]) if len(sys.argv) > 2 else Path("wvt.log") pidfile = Path(sys.argv[3]) if len(sys.argv) > 3 else Path("wvt.pid") crash = log.parent / "wvt-crash.log" print(f"port={port}") if pidfile.is_file(): pid = pidfile.read_text(encoding="utf-8", errors="replace").strip() print(f"pidfile={pidfile} pid={pid or '(empty)'}") if pid.isdigit(): import subprocess out = subprocess.run( ["tasklist", "/FI", f"PID eq {pid}"], capture_output=True, ) print(out.stdout.decode("utf-8", errors="replace").strip() or "(tasklist empty)") else: print(f"pidfile={pidfile} (missing)") print(f"--- tail {log} ---") print(tail(log)) if crash.is_file(): print(f"--- {crash} ---") print(tail(crash, 40)) else: print(f"--- {crash} (missing) ---") return 0 if __name__ == "__main__": raise SystemExit(main())