-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrash_watcher.py
More file actions
92 lines (69 loc) · 2.47 KB
/
crash_watcher.py
File metadata and controls
92 lines (69 loc) · 2.47 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
89
90
91
92
"""CrashWatcher module recording recent events and emitting crash reports."""
from __future__ import annotations
import atexit
import threading
import time
from collections import deque
from typing import Deque
try:
import tkinter as tk
from tkinter import messagebox
except Exception: # pragma: no cover - tkinter may be unavailable in some envs
tk = None # type: ignore
messagebox = None # type: ignore
class CrashWatcher(threading.Thread):
"""Background thread storing recent events in a circular buffer."""
def __init__(self, max_events: int = 100):
super().__init__(daemon=True)
self._events: Deque[str] = deque(maxlen=max_events)
self._lock = threading.Lock()
def record_event(self, msg: str) -> None:
ts = time.strftime("%Y-%m-%d %H:%M:%S")
thread = threading.current_thread().name
with self._lock:
self._events.append(f"{ts} [{thread}] {msg}")
def dump_events(self) -> str:
with self._lock:
return "\n".join(self._events)
def run(self) -> None: # pragma: no cover - thread performs no active work
while True:
time.sleep(1)
_watcher: CrashWatcher | None = None
clean_shutdown = False
def start(max_events: int = 100) -> None:
"""Start the global crash watcher thread and register exit handler."""
global _watcher
if _watcher is None:
_watcher = CrashWatcher(max_events=max_events)
_watcher.start()
atexit.register(_handle_exit)
def record_event(msg: str) -> None:
"""Record an event in the crash buffer."""
if _watcher is not None:
_watcher.record_event(msg)
def dump_events() -> str:
"""Return concatenated recent events."""
if _watcher is None:
return ""
return _watcher.dump_events()
def mark_clean_shutdown() -> None:
"""Set the global clean shutdown flag."""
global clean_shutdown
clean_shutdown = True
def _handle_exit() -> None:
if clean_shutdown or _watcher is None:
return
report_path = "crash_report.txt"
data = _watcher.dump_events()
with open(report_path, "w", encoding="utf-8") as fh:
fh.write(data)
if tk and messagebox:
try:
root = tk.Tk()
root.withdraw()
messagebox.showerror("Crash Report", data)
root.destroy()
except Exception:
pass
else: # pragma: no cover - in headless environments
print(f"Crash report written to {report_path}")