""" Structured logging for RFCP backend. """ import os import sys import time import threading _log_file = None def rfcp_log(tag: str, msg: str): """Log with tag prefix, timestamp, and thread name. Writes to stdout and a log file for reliability. """ global _log_file ts = time.strftime('%H:%M:%S') thr = threading.current_thread().name line = f"[{tag} {ts}] [{thr}] {msg}" print(line, flush=True) try: if _log_file is None: log_dir = os.environ.get('RFCP_DATA_PATH', './data') os.makedirs(log_dir, exist_ok=True) log_path = os.path.join(log_dir, 'rfcp-backend.log') _log_file = open(log_path, 'a') _log_file.write(line + '\n') _log_file.flush() except Exception: pass