Liga-System/wood/logger.py

77 lines
2.3 KiB
Python
Raw Normal View History

import sqlite3
import os
import inspect
from data.setup_database import DB_PATH
DB_FILE = "wood/log.db"
def log(details):
# Arbeitsdaten der aktuellen Funktion (log) holen
this_frame = inspect.currentframe()
try:
# Einen Schritt "nach oben" zum Aufrufer gehen
caller_frame = this_frame.f_back
# 3. Lesbare Infos aus dem Aufrufer-Frame extrahieren
info = inspect.getframeinfo(caller_frame)
file = f"{info.filename}"
source = f"{info.function}() L:{info.lineno}"
# 5. Datenbank-Verbindung aufbauen (DB_FILE muss vorher definiert sein)
connection = sqlite3.connect(DB_FILE)
cursor = connection.cursor()
# 6. Daten in die Datenbank schreiben
cursor.execute(
"INSERT INTO system_log (file, source, details) VALUES (?, ?, ?)",
(file, source, details)
)
connection.commit()
connection.close()
finally:
del this_frame
del caller_frame
def setup_log_db():
# --- DATENBANK CHECK ---
# ACHTUNG: Großschreibung bei DB_FILE beachten!
if not os.path.exists(DB_FILE):
print(f"WARNUNG: '{DB_FILE}' nicht gefunden!")
print("Starte Log-Datenbank-Einrichtung...")
# Wir bauen die Datei direkt HIER auf, ohne setup_database aufzurufen!
connection = sqlite3.connect(DB_FILE)
cursor = connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS system_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
file TEXT,
source TEXT,
details TEXT
)
''')
connection.commit()
connection.close()
print("Log-Datenbank aufgebaut!")
else:
print(f"OK: Log-Datenbank '{DB_FILE}' gefunden. Lade System...")
def get_full_log():
"""Holt das komplette Log und übersetzt die Spieler-IDs in Namen."""
# 1. Alle Logs aus der log.db holen
log_conn = sqlite3.connect(DB_FILE)
log_conn.row_factory = sqlite3.Row
log_cursor = log_conn.cursor()
log_cursor.execute("SELECT * FROM system_log ORDER BY timestamp DESC")
logs = [dict(row) for row in log_cursor.fetchall()]
log_conn.close()
return logs