#!/usr/bin/env python3
# Read-only UART logger: raw bytes -> .raw, host-timestamped lines -> .log
import serial, sys, time, datetime
port, base = sys.argv[1], sys.argv[2]
s = serial.Serial(port, 115200, bytesize=8, parity="N", stopbits=1, timeout=0.2)
raw = open(base + ".raw", "ab", buffering=0); log = open(base + ".log", "a", buffering=1)
log.write(f"### uartlog start {datetime.datetime.now().isoformat()} {port} 115200 8N1\n")
buf = b""
while True:
d = s.read(4096)
if not d:
continue
raw.write(d); buf += d
while b"\n" in buf:
ln, buf = buf.split(b"\n", 1)
ts = datetime.datetime.now().strftime("%H:%M:%S.%f")[:-3]
log.write(f"{ts} {ln.rstrip(b'\r').decode('utf-8', 'backslashreplace')}\n")