"""Append-only provenance log for playback-control events. Every path that can start/stop playback (MPRIS calls, the media-key eventFilter, the Player's transport methods) drops a one-line record here, so "the app played by itself" incidents can be attributed after the fact: which pathway fired, when, and — for D-Bus — who sent it. Written to ~/.cache/lintunes/control-events.log (survives app restarts and doesn't depend on how the app was launched); mirrored to the standard logger. """ import logging import time from pathlib import Path log = logging.getLogger("lintunes.control") _MAX_BYTES = 1_000_000 def _log_path() -> Path: return Path.home() / ".cache" / "lintunes" / "control-events.log" def log_control(source: str, action: str, detail: str = ""): """Record a playback-control event; never raises.""" line = f"{time.strftime('%F %T')} [{source}] {action}" if detail: line += f" {detail}" log.info("[%s] %s %s", source, action, detail) try: path = _log_path() path.parent.mkdir(parents=True, exist_ok=True) if path.exists() and path.stat().st_size > _MAX_BYTES: path.replace(path.with_suffix(".log.old")) with open(path, "a", encoding="utf-8") as f: f.write(line + "\n") except Exception: pass # a provenance logger must never break playback