"""Load saved logs for offline analysis. Accepts both raw serial captures and this tool's session logs, whose lines look like "[14:02:11.532] I (1234) TAG: msg" and whose host events look like "[14:02:11.532] --- reset issued by tool ---". """ from __future__ import annotations import re from pathlib import Path from .logline import LineKind, LogLine, parse_line, split_glued _SESSION_PREFIX = re.compile(r"^\[(\d\d):(\d\d):(\d\d)\.(\d{3})\] ") _HOST_EVENT = re.compile(r"^--- .* ---$") def read_log_file(path: Path) -> list[LogLine]: text = path.read_bytes().decode("utf-8", errors="replace") out: list[LogLine] = [] for i, raw in enumerate(text.splitlines()): host_time = i * 0.001 # raw capture: keep order, no wall clock if m := _SESSION_PREFIX.match(raw): h, mi, s, ms = (int(g) for g in m.groups()) host_time = h * 3600 + mi * 60 + s + ms / 1000 raw = raw[m.end():] if _HOST_EVENT.match(raw): out.append(LogLine(raw, LineKind.HOST, host_time)) continue out.extend(parse_line(part, host_time) for part in split_glued(raw)) return out