Files
gti_radiostudio/event_logger.py
T

72 lines
2.0 KiB
Python
Raw Normal View History

2026-07-17 20:44:15 +10:00
# event_logger.py
import json
import os
import threading
import time
from datetime import datetime
LOG_DIR = os.path.expanduser("~/.gti_radiostudio/sessions")
class SessionLogger:
def __init__(self):
self._lock = threading.Lock()
self._log_path = None
self._start_new_session()
def _start_new_session(self):
os.makedirs(LOG_DIR, exist_ok=True)
ts = datetime.now().strftime("%Y-%m-%d_%H%M%S")
self._log_path = os.path.join(LOG_DIR, f"{ts}.jsonl")
self._write_raw({
"event": "session_start",
"timestamp": time.time(),
"iso": datetime.now().isoformat(),
})
def _write_raw(self, entry: dict):
with self._lock:
try:
with open(self._log_path, 'a') as f:
f.write(json.dumps(entry) + "\n")
except Exception:
pass
def log_cart_trigger(self, track_name: str):
self._write_raw({
"event": "cart_trigger",
"timestamp": time.time(),
"iso": datetime.now().isoformat(),
"track": track_name,
})
def log_mic_on(self, channel: int):
self._write_raw({
"event": "mic_on",
"timestamp": time.time(),
"iso": datetime.now().isoformat(),
"channel": channel,
})
def log_mic_off(self, channel: int, duration_sec: float):
self._write_raw({
"event": "mic_off",
"timestamp": time.time(),
"iso": datetime.now().isoformat(),
"channel": channel,
"duration_sec": round(duration_sec, 3),
})
def log_deck_play(self, deck_key: str, track_name: str):
self._write_raw({
"event": "deck_play",
"timestamp": time.time(),
"iso": datetime.now().isoformat(),
"deck": deck_key,
"track": track_name,
})
@property
def log_path(self):
return self._log_path