feat: Add logging and error handling enhancements
- Introduced new logging modules for action tracking, error logging, and performance tracking to improve observability. - Added functions for retrieving debug and error log paths in `path_config.py`. - Updated various components to utilize the new logging system, enhancing error handling and performance diagnostics. - Implemented a GUI component for viewing error logs, improving user experience in monitoring application issues. - Cleaned up unnecessary blank lines and improved code readability across multiple files.
This commit is contained in:
1 parent
bdf9312bc9
commit
efe6e2252f
48 files changed
+2165
-320
No files matched your search
@@ -0,0 +1,69 @@
|
||||
"""性能追踪模块:耗时统计与简单分析工具"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
import threading
|
||||
from typing import Callable, Dict, List, Optional
|
||||
|
||||
class PerformanceTracker:
|
||||
def __init__(self) -> None:
|
||||
self._start_times: Dict[str, float] = {}
|
||||
self._durations: Dict[str, List[float]] = {}
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def start_timer(self, name: str) -> str:
|
||||
with self._lock:
|
||||
timer_id = f"{name}-{int(time.time()*1000)}"
|
||||
self._start_times[timer_id] = time.perf_counter()
|
||||
return timer_id
|
||||
|
||||
def end_timer(self, timer_id: str) -> float:
|
||||
with self._lock:
|
||||
start = self._start_times.pop(timer_id, None)
|
||||
if start is None:
|
||||
return 0.0
|
||||
elapsed = time.perf_counter() - start
|
||||
self._durations.setdefault(timer_id.split("-")[0], []).append(elapsed)
|
||||
return elapsed
|
||||
|
||||
def get_elapsed(self, timer_id: str) -> float:
|
||||
with self._lock:
|
||||
start = self._start_times.get(timer_id)
|
||||
if start is None:
|
||||
return 0.0
|
||||
return time.perf_counter() - start
|
||||
|
||||
def track_function(self, func: Callable) -> Callable:
|
||||
def wrapper(*args, **kwargs):
|
||||
name = func.__qualname__
|
||||
timer = self.start_timer(name)
|
||||
try:
|
||||
result = func(*args, **kwargs)
|
||||
return result
|
||||
finally:
|
||||
self.end_timer(timer)
|
||||
return wrapper
|
||||
|
||||
def track_step(self, name: str) -> Callable:
|
||||
def decorator(func: Callable) -> Callable:
|
||||
def inner(*args, **kwargs):
|
||||
t = self.start_timer(name)
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
finally:
|
||||
self.end_timer(t)
|
||||
return inner
|
||||
return decorator
|
||||
|
||||
def get_statistics(self) -> Dict[str, float]:
|
||||
# 返回一个简单的平均耗时统计字典,使用最近的 durations
|
||||
with self._lock:
|
||||
stats = {}
|
||||
for key, arr in self._durations.items():
|
||||
if arr:
|
||||
stats[key] = sum(arr) / len(arr)
|
||||
return stats
|
||||
|
||||
__all__ = ["PerformanceTracker"]
|
||||
|
||||
|
||||
Reference in new issue
Block a user