This commit is contained in:
2026-01-23 02:24:19 -08:00
parent 5626bde557
commit e73cbd4ba2

View File

@@ -2,6 +2,8 @@ from __future__ import annotations
import sqlite3 import sqlite3
import json import json
import threading
from queue import Queue
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from contextlib import contextmanager from contextlib import contextmanager
@@ -158,18 +160,53 @@ class Database:
# Singleton instance # Singleton instance
db = Database() db = Database()
def get_db() -> Database: _LOG_QUEUE: Queue[tuple[str, str, str]] = Queue()
return db _LOG_THREAD_STARTED = False
_LOG_THREAD_LOCK = threading.Lock()
def log_to_db(level: str, module: str, message: str):
"""Log a message to the database.""" def _log_worker_loop() -> None:
while True:
level, module, message = _LOG_QUEUE.get()
try: try:
db.execute( db.execute(
"INSERT INTO logs (level, module, message) VALUES (?, ?, ?)", "INSERT INTO logs (level, module, message) VALUES (?, ?, ?)",
(level, module, message) (level, module, message)
) )
except Exception: except Exception:
# Avoid recursive logging errors if DB is locked pass
finally:
try:
_LOG_QUEUE.task_done()
except Exception:
pass
def _ensure_log_thread() -> None:
global _LOG_THREAD_STARTED
if _LOG_THREAD_STARTED:
return
with _LOG_THREAD_LOCK:
if _LOG_THREAD_STARTED:
return
thread = threading.Thread(
target=_log_worker_loop,
name="mediosdb-log",
daemon=True
)
thread.start()
_LOG_THREAD_STARTED = True
def get_db() -> Database:
return db
def log_to_db(level: str, module: str, message: str):
"""Log a message to the database asynchronously."""
try:
_ensure_log_thread()
_LOG_QUEUE.put((level, module, message))
except Exception:
# Avoid recursive logging errors if the queue fails
pass pass
# Initialize DB logger in the unified logger # Initialize DB logger in the unified logger