|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +from datetime import UTC, datetime |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from gi.repository import GLib |
| 9 | + |
| 10 | +STARTUP_TRACE_ENV = "MINI_EQ_STARTUP_TRACE" |
| 11 | +STARTUP_TRACE_DIR_NAME = "mini-eq" |
| 12 | +STARTUP_TRACE_FILE_NAME = "startup-trace.log" |
| 13 | + |
| 14 | + |
| 15 | +def startup_trace_enabled() -> bool: |
| 16 | + value = os.environ.get(STARTUP_TRACE_ENV, "") |
| 17 | + return value.strip().lower() not in {"", "0", "false", "no", "off"} |
| 18 | + |
| 19 | + |
| 20 | +def startup_trace_path() -> Path: |
| 21 | + return Path(GLib.get_user_state_dir()) / STARTUP_TRACE_DIR_NAME / STARTUP_TRACE_FILE_NAME |
| 22 | + |
| 23 | + |
| 24 | +def describe_output_preset_target(target: object | None) -> dict[str, object] | None: |
| 25 | + if target is None: |
| 26 | + return None |
| 27 | + |
| 28 | + route = getattr(target, "route", None) |
| 29 | + route_info = None |
| 30 | + if route is not None: |
| 31 | + route_info = { |
| 32 | + "description": _json_safe(getattr(route, "description", None)), |
| 33 | + "device_name": _json_safe(getattr(route, "device_name", None)), |
| 34 | + "name": _json_safe(getattr(route, "name", None)), |
| 35 | + "output_preset_key": _json_safe(getattr(route, "output_preset_key", None)), |
| 36 | + "route_device": _json_safe(getattr(route, "route_device", None)), |
| 37 | + } |
| 38 | + |
| 39 | + return { |
| 40 | + "device_name": _json_safe(getattr(target, "device_name", None)), |
| 41 | + "has_route_key": bool(getattr(target, "has_route_key", False)), |
| 42 | + "keys": _json_safe(tuple(getattr(target, "keys", ()) or ())), |
| 43 | + "link_key": _json_safe(getattr(target, "link_key", "")), |
| 44 | + "output_key": _json_safe(getattr(target, "output_key", None)), |
| 45 | + "route": route_info, |
| 46 | + "route_device": _json_safe(getattr(target, "route_device", None)), |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +def describe_output_preset_snapshot(snapshot: object | None) -> dict[str, object] | None: |
| 51 | + if snapshot is None: |
| 52 | + return None |
| 53 | + |
| 54 | + return { |
| 55 | + "identity": _json_safe(getattr(snapshot, "identity", None)), |
| 56 | + "sink_name": _json_safe(getattr(snapshot, "sink_name", None)), |
| 57 | + "target": describe_output_preset_target(getattr(snapshot, "target", None)), |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | +def describe_output_preset_transition(transition: object | None) -> dict[str, object] | None: |
| 62 | + if transition is None: |
| 63 | + return None |
| 64 | + |
| 65 | + return { |
| 66 | + "changed": bool(getattr(transition, "changed", False)), |
| 67 | + "current": describe_output_preset_snapshot(getattr(transition, "current", None)), |
| 68 | + "previous": describe_output_preset_snapshot(getattr(transition, "previous", None)), |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +def trace_startup_event(event: str, **fields: object) -> None: |
| 73 | + if not startup_trace_enabled(): |
| 74 | + return |
| 75 | + |
| 76 | + path = startup_trace_path() |
| 77 | + record = { |
| 78 | + "event": event, |
| 79 | + "timestamp": datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z"), |
| 80 | + } |
| 81 | + record.update({key: _json_safe(value) for key, value in fields.items()}) |
| 82 | + |
| 83 | + try: |
| 84 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 85 | + with path.open("a", encoding="utf-8") as trace_file: |
| 86 | + trace_file.write(json.dumps(record, sort_keys=True, separators=(",", ":"))) |
| 87 | + trace_file.write("\n") |
| 88 | + except Exception: |
| 89 | + return |
| 90 | + |
| 91 | + |
| 92 | +def _json_safe(value: object) -> object: |
| 93 | + if value is None or isinstance(value, str | int | float | bool): |
| 94 | + return value |
| 95 | + |
| 96 | + if isinstance(value, tuple | list | set): |
| 97 | + return [_json_safe(item) for item in value] |
| 98 | + |
| 99 | + if isinstance(value, dict): |
| 100 | + return {str(key): _json_safe(item) for key, item in value.items()} |
| 101 | + |
| 102 | + return str(value) |
0 commit comments