2026-03-10 15:34:25 +01:00
|
|
|
import sqlite3
|
|
|
|
|
import os
|
2026-03-27 09:38:29 +01:00
|
|
|
import inspect
|
2026-03-10 15:34:25 +01:00
|
|
|
|
|
|
|
|
from data.setup_database import DB_PATH
|
|
|
|
|
|
|
|
|
|
DB_FILE = "wood/log.db"
|
|
|
|
|
|
2026-03-27 09:38:29 +01:00
|
|
|
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}"
|
2026-03-10 15:34:25 +01:00
|
|
|
|
2026-03-27 09:38:29 +01:00
|
|
|
# 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
|
2026-03-10 15:34:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2026-03-27 09:38:29 +01:00
|
|
|
file TEXT,
|
|
|
|
|
source TEXT,
|
2026-03-10 15:34:25 +01:00
|
|
|
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
|