51 lines
1.1 KiB
Python
51 lines
1.1 KiB
Python
"""Journal des événements de connexion WGSecure."""
|
|
import os
|
|
import json
|
|
from datetime import datetime
|
|
from app.utils.platform_utils import get_config_dir
|
|
|
|
_FILE = "events.json"
|
|
_MAX_ENTRIES = 200
|
|
|
|
|
|
def _path() -> str:
|
|
return os.path.join(get_config_dir(), _FILE)
|
|
|
|
|
|
def log_event(msg: str, kind: str = "info") -> None:
|
|
"""Ajoute un événement horodaté au journal."""
|
|
events = _load()
|
|
events.append({
|
|
"ts": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
"kind": kind, # info | success | error | warning
|
|
"msg": msg,
|
|
})
|
|
_save(events[-_MAX_ENTRIES:])
|
|
|
|
|
|
def get_events(n: int = 50) -> list[dict]:
|
|
return _load()[-n:]
|
|
|
|
|
|
def clear() -> None:
|
|
_save([])
|
|
|
|
|
|
def _load() -> list[dict]:
|
|
p = _path()
|
|
if not os.path.exists(p):
|
|
return []
|
|
try:
|
|
with open(p, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
except Exception:
|
|
return []
|
|
|
|
|
|
def _save(events: list[dict]) -> None:
|
|
try:
|
|
with open(_path(), "w", encoding="utf-8") as f:
|
|
json.dump(events, f, ensure_ascii=False)
|
|
except Exception:
|
|
pass
|