-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (72 loc) · 2.81 KB
/
Copy pathmain.py
File metadata and controls
88 lines (72 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# main.py
import queue
import threading
import tkinter as tk
import sys
import traceback
from pathlib import Path
from tkinter import messagebox
from config import LOG_FILE_PATHS, KEYWORDS
from file_watcher import MultiFileWatcher as FileWatcher
from filter_module import analyze_line
from gui_module import LogWatcherGUI
from popup_module import show_critical_alert
class LogWatcherApp:
def __init__(self):
self.root = tk.Tk()
self.log_queue = queue.Queue()
# Watcher initialisieren
self.watcher = FileWatcher(
LOG_FILE_PATHS,
self.on_new_line,
self.on_watcher_status,
)
# GUI initialisieren
self.gui = LogWatcherGUI(self.root, self.stop_app)
# Worker-Thread für das Einlesen der Datei starten
self.watcher_thread = threading.Thread(target=self.watcher.start, daemon=True)
self.watcher_thread.start()
# Regelmäßiges Prüfen der Queue starten (Polling im Tkinter-Hauptthread)
self.root.after(100, self.process_queue)
for path in LOG_FILE_PATHS:
self.gui.append_log(f"Überwachung gestartet: {path}")
def on_new_line(self, line):
"""Callback vom FileWatcher, wenn eine Zeile reinkommt."""
is_match, level, keyword = analyze_line(line, KEYWORDS)
self.log_queue.put((line, level, keyword if is_match else None))
def process_queue(self):
"""Verarbeitet die Queue im Hauptthread (Thread-sicher für Tkinter)."""
while not self.log_queue.empty():
line, level, keyword = self.log_queue.get_nowait()
self.gui.append_log(line, level)
# Optionales Pop-up bei kritischen Fehlern
if keyword in ["ERROR", "CRITICAL", "Exception"]:
show_critical_alert(keyword, line)
# Nach 100ms erneut aufrufen
self.root.after(100, self.process_queue)
def on_watcher_status(self, status):
self.root.after(0, self.gui.set_status, status)
def stop_app(self):
self.watcher.stop()
def run(self):
self.root.mainloop()
def _write_startup_error(error):
if getattr(sys, "frozen", False):
error_path = Path(sys.executable).resolve().with_name("log_watcher_error.log")
else:
error_path = Path(__file__).resolve().with_name("log_watcher_error.log")
error_path.write_text(traceback.format_exc(), encoding="utf-8")
return error_path
if __name__ == "__main__":
try:
app = LogWatcherApp()
app.run()
except Exception as error:
error_path = _write_startup_error(error)
try:
messagebox.showerror(
"Log Watcher konnte nicht gestartet werden",
f"{error}\n\nDetails: {error_path}",
)
except tk.TclError:
pass