Files
auto-pickup-v2/log.py
T

46 lines
914 B
Python

"""统一日志控制 — 编译前修改 LEVEL 控制输出量。
等级:
0 = 用户级:只显示处理结果和错误
1 = 调试级:显示所有步骤细节
2 = 追踪级:显示 API 请求/响应内容
"""
import sys
from datetime import datetime
LEVEL = -1 # 编译时修改此值
_gui_callback = None # GUI 注册的回调: (msg: str) -> None
def set_gui_callback(cb):
"""注册 GUI 日志回调,所有日志同步输出到 GUI。"""
global _gui_callback
_gui_callback = cb
def _fmt(msg: str) -> str:
return f"[{datetime.now().strftime('%H:%M:%S.%f')[:-3]}] {msg}"
def _emit(msg: str):
print(_fmt(msg), file=sys.stderr)
if _gui_callback:
_gui_callback(msg)
def user(msg: str):
if LEVEL >= 0:
_emit(msg)
def debug(msg: str):
if LEVEL >= 1:
_emit(msg)
def trace(msg: str):
if LEVEL >= 2:
_emit(f"[trace] {msg}")