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:
Your Name committed 2025-12-29 20:48:09 +08:00
1 parent bdf9312bc9
commit efe6e2252f
48 files changed
+2165 -320

No files matched your search

+49 -87
View File
@@ -223,30 +223,38 @@ class underlying_operations:
# 用于存储OCR区域的坐标信息(key为picname,value为(left, top, right, bottom))
self._ocr_coords_cache: dict[str, tuple[int, int, int, int]] = {}
# 检测GPU是否可用(自动检测)
self._gpu_available = self._detect_gpu_available()
# 默认使用CPU模式(删除GPU检测)
self._gpu_available = False
if not str(os.environ.get("SKIP_OCR_INIT", "")).strip():
# 优先使用EasyOCR(更快),如果失败则回退到PaddleOCR
# 首先尝试EasyOCR
devnull = None
try:
devnull = open(os.devnull, "w")
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(
devnull
):
import easyocr # type: ignore[import-untyped] # pyright: ignore[reportMissingImports]
# 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题
# 改为直接静默导入和初始化
import easyocr # type: ignore[import-untyped] # pyright: ignore[reportMissingImports]
# 只加载中文和英文,加快初始化速度
# 自动检测GPU可用性,如果可用则启用GPU加速
# 只加载中文和英文,加快初始化速度
# 自动检测GPU可用性,如果可用则启用GPU加速
# 临时重定向输出以减少初始化时的噪音
import io
from contextlib import redirect_stdout, redirect_stderr
fake_stdout = io.StringIO()
fake_stderr = io.StringIO()
with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr):
self.ocr = easyocr.Reader(
["ch_sim", "en"], gpu=self._gpu_available, verbose=False
["ch_sim", "en"], gpu=False, verbose=False
)
self.ocr_type = "easyocr"
gpu_status = "GPU加速" if self._gpu_available else "CPU模式"
print(f"[OCR] 使用 EasyOCR({gpu_status})")
except Exception:
self.ocr_type = "easyocr"
gpu_status = "CPU模式"
print(f"[OCR] 使用 EasyOCR({gpu_status})")
except Exception as e:
# EasyOCR初始化失败,回退到PaddleOCR
print(f"[OCR] EasyOCR初始化失败: {e},回退到PaddleOCR")
try:
if devnull is not None:
try:
@@ -271,7 +279,7 @@ class underlying_operations:
paddle.device.is_compiled_with_cuda()
and paddle.device.get_device()
):
use_gpu = self._gpu_available
use_gpu = False
except Exception:
pass
@@ -327,75 +335,37 @@ class underlying_operations:
adb = adb[1:-1]
return adb
def _detect_gpu_available(self) -> bool:
"""检测GPU是否可用
默认返回False(CPU模式),除非通过环境变量明确启用GPU
返回:
True: GPU可用且已启用
False: 使用CPU模式(默认)
"""
# 默认使用CPU模式,除非环境变量明确要求使用GPU
# 检查环境变量是否强制启用GPU
if str(os.environ.get("FORCE_GPU_OCR", "")).strip().lower() in (
"1",
"true",
"yes",
):
# 强制启用时,验证GPU是否真的可用
try:
import torch # type: ignore[import-untyped]
if torch.cuda.is_available():
gpu_name = torch.cuda.get_device_name(0)
print(f"[OCR] 环境变量强制启用GPU: {gpu_name},将使用GPU加速")
return True
else:
print("[OCR] 环境变量要求启用GPU,但GPU不可用,使用CPU模式")
return False
except ImportError:
print("[OCR] 环境变量要求启用GPU,但PyTorch未安装,使用CPU模式")
return False
except Exception as e:
print(f"[OCR] GPU检测失败: {e},使用CPU模式")
return False
# 默认使用CPU模式
return False
def _ensure_ocr(self):
if self.ocr is not None:
return
# 重新检测GPU(延迟初始化时)
self._gpu_available = self._detect_gpu_available()
# 默认使用CPU模式(延迟初始化时)
# 优先尝试EasyOCR
devnull = None
try:
devnull = open(os.devnull, "w")
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(
devnull
):
import easyocr # type: ignore[import-untyped]
# 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题
import easyocr # type: ignore[import-untyped]
# 临时重定向输出以减少初始化时的噪音
import io
from contextlib import redirect_stdout, redirect_stderr
fake_stdout = io.StringIO()
fake_stderr = io.StringIO()
with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr):
self.ocr = easyocr.Reader(
["ch_sim", "en"], gpu=self._gpu_available, verbose=False
["ch_sim", "en"], gpu=False, verbose=False
)
self.ocr_type = "easyocr"
gpu_status = "GPU加速" if self._gpu_available else "CPU模式"
print(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})")
except Exception:
self.ocr_type = "easyocr"
gpu_status = "GPU加速" if False else "CPU模式"
print(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})")
except Exception as e:
# EasyOCR失败,回退到PaddleOCR
try:
if devnull is not None:
try:
devnull.close()
except Exception:
pass
except Exception:
pass
print(f"[OCR] 延迟初始化EasyOCR失败: {e},回退到PaddleOCR")
devnull = None
try:
devnull = open(os.devnull, "w")
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(
@@ -412,7 +382,7 @@ class underlying_operations:
paddle.device.is_compiled_with_cuda()
and paddle.device.get_device()
):
use_gpu = self._gpu_available
use_gpu = False
except Exception:
pass
@@ -429,22 +399,14 @@ class underlying_operations:
gpu_status = "GPU加速" if use_gpu else "CPU模式"
print(f"[OCR] 延迟初始化:使用 PaddleOCR({gpu_status})")
except Exception:
try:
if devnull is not None:
try:
devnull.close()
except Exception:
pass
except Exception:
pass
self.ocr = None
self.ocr_type = None
finally:
try:
if devnull is not None:
devnull.close()
except Exception:
pass
finally:
try:
if devnull is not None:
devnull.close()
except Exception:
pass
def select_device(self):
"""选择需要连接的设备"""