-
-
Notifications
You must be signed in to change notification settings - Fork 806
美化输出 #1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
美化输出 #1075
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7a6743e
美化终端输出
f7ef90a
美化异常信息处理
a2d24a7
移除测试异常钩子代码
91243b8
更新exec_hook.py
061cfe9
日志显示小瑕疵修改
08dc130
修复小瑕疵
a9cbde8
修复类型问题
7648ccf
优化管理面板下载函数
a5d0bfd
美化Loader那边的异常信息
7bbd3b6
优化部分类型注解,错误信息显示
3ea9ed8
修复瑕疵
42137ea
优化日志显示
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
""" exception hook """ | ||
|
||
import traceback | ||
import multiprocessing | ||
import threading | ||
import inspect | ||
import sys | ||
from rich.console import Console | ||
from rich.text import Text | ||
from rich.panel import Panel | ||
|
||
console = Console() | ||
|
||
def format_stack_trace(exctype, value, tb, max_depth=15, nested=False) -> Text: | ||
tb_list = traceback.extract_tb(tb) | ||
exception_info = Text() | ||
|
||
if nested: | ||
exception_info.append(f"{exctype.__name__}: {value}\n", style="bold red") | ||
else: | ||
# 获取当前进程和线程名称 | ||
process_name = multiprocessing.current_process().name | ||
thread_name = threading.current_thread().name | ||
exception_info.append( | ||
f"Exception in process: {process_name}, thread: {thread_name}; {exctype.__name__}: {value}\n", | ||
style="bold red" | ||
) | ||
exception_info.append("Traceback (most recent call last):\n", style="bold") | ||
|
||
# 限制堆栈跟踪的深度 | ||
limited_tb_list = tb_list[:max_depth] | ||
more_frames = len(tb_list) - max_depth | ||
|
||
for i, (filename, lineno, funcname, line) in enumerate(limited_tb_list): | ||
# 获取函数所在的模块名 | ||
module_name = inspect.getmodulename(filename) | ||
exception_info.append( | ||
f" at {module_name}.{funcname} in ({filename}:{lineno})\n", | ||
style="yellow" | ||
) | ||
|
||
if more_frames > 0: | ||
exception_info.append(f" ... {more_frames} more ...\n", style="dim") | ||
|
||
# 检查是否有原因和其他信息 | ||
cause = getattr(value, "__cause__", None) | ||
context = getattr(value, "__context__", None) | ||
|
||
if cause: | ||
exception_info.append("Caused by: \n", style="bold red") | ||
exception_info.append(format_stack_trace(type(cause), cause, cause.__traceback__, nested=True)) | ||
if context and not cause: | ||
exception_info.append("Original exception: \n", style="bold red") | ||
exception_info.append(format_stack_trace(type(context), context, context.__traceback__, nested=True)) | ||
|
||
return exception_info | ||
|
||
def ExtractException(exctype, value, tb, panel: bool = True, rich_printable: bool = False) -> Text | Panel | str | None: | ||
""" | ||
- panel: 是否以Panel形式返回异常信息 | ||
- rich_printable: 是否以可打印的格式返回异常信息 (把rich转换为普通print或者 stdout | stderr等控制台输出有效果的格式) | ||
""" | ||
# 获取回溯信息并格式化为字符串 | ||
_exc_info = None | ||
if all(x is None for x in (exctype, value, tb)): | ||
return None | ||
tb_str = format_stack_trace(exctype, value, tb) | ||
# 返回异常信息 | ||
if panel: | ||
_exc_info = Panel(tb_str, title="[bold red]Exception Occurred[/bold red]", border_style="red") | ||
if rich_printable: | ||
with console.capture() as capture: | ||
console.print(_exc_info) | ||
return capture.get() | ||
return _exc_info | ||
|
||
def sys_excepthook(exctype, value, tb): | ||
# 获取异常信息并打印到控制台 | ||
exception_info = ExtractException(exctype, value, tb , panel=True) | ||
if exception_info: | ||
console.print(exception_info) | ||
|
||
def set_exechook(): | ||
""" | ||
设置全局异常处理函数 | ||
""" | ||
sys.excepthook = sys_excepthook | ||
|
||
def GetStackTrace(vokedepth: int = 1) -> Text: | ||
""" | ||
获取堆栈跟踪信息 | ||
""" | ||
# 获取当前调用栈信息的前两层 | ||
stack = traceback.extract_stack(limit=vokedepth) | ||
stack_info = Text("Stack Trace:\n", style="bold") | ||
for frame in stack[:-vokedepth+1]: | ||
filename = frame.filename | ||
line = frame.lineno | ||
funcname = frame.name | ||
stack_info.append(f" at {funcname} in ({filename}:{line})\n", style="yellow") | ||
return stack_info | ||
|
||
if __name__ == "__main__": | ||
|
||
set_exechook() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When vokedepth is 1, this slice returns an empty list, potentially omitting useful stack trace information. Consider adjusting the slicing logic to ensure at least one stack frame is captured.
Copilot uses AI. Check for mistakes.