Difficult-Rocket/Difficult_Rocket/crash.py

116 lines
4.2 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2021 by shenjackyuanjie
# All rights reserved
# -------------------------------
2021-09-02 22:47:10 +08:00
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
import os
import time
2021-09-16 19:18:06 +08:00
import platform
2021-09-02 22:47:10 +08:00
import traceback
2021-09-16 19:18:06 +08:00
import threading
import multiprocessing
2021-09-02 22:47:10 +08:00
from typing import Optional
2021-09-16 19:18:06 +08:00
# import psutil
# for more system info
2021-09-02 22:47:10 +08:00
# where the crash report from
# this can't be crash , or the game will really crash!
# TODO 写完它
Head_message = """# ----- Difficult Rocket Crash Report -----
## Traceback
"""
Process_message = """## Process info
"""
2021-09-02 22:47:10 +08:00
Thread_message = """## Thread info
"""
2021-09-16 19:18:06 +08:00
Python_message = """## Python info
"""
2021-09-02 22:47:10 +08:00
System_message = """## System info
"""
2021-09-09 23:54:03 +08:00
all_thread = [threading.main_thread()]
all_process = [multiprocessing.current_process()]
2021-09-08 23:38:34 +08:00
record_thread = True
2021-09-05 00:50:05 +08:00
2021-09-02 22:47:10 +08:00
def crash_info_handler(info: str = None) -> str:
if not info:
info = traceback.format_exc()
2021-09-16 19:18:06 +08:00
format_info = f"<pre>\n{info}</pre>\n"
2021-09-02 22:47:10 +08:00
return format_info
2021-09-16 19:18:06 +08:00
def markdown_line_handler(string: Optional[str or bool or int or float], code: bool = False, level: int = 1, end: str = '\n') -> str:
2021-09-02 22:47:10 +08:00
lvl = '- ' * level
f_string = string
if code:
f_string = '`{}`'.format(f_string)
2021-09-16 19:18:06 +08:00
return '{}{}{}'.format(lvl, f_string, end)
2021-09-02 22:47:10 +08:00
def create_crash_report(info: str = None) -> None:
2021-09-16 19:18:06 +08:00
crash_info = crash_info_handler(info)
2021-09-02 22:47:10 +08:00
if 'crash_report' not in os.listdir('./'):
os.mkdir('./crash_report')
date_time = time.strftime('%Y-%m-%d %H-%M-%S', time.gmtime(time.time()))
filename = 'crash-{}.md'.format(date_time)
with open('./crash_report/{}'.format(filename), 'w+', encoding='utf-8') as crash_file:
2021-09-16 19:18:06 +08:00
# 开头信息
crash_file.write(Head_message)
# 崩溃信息
2021-09-02 22:47:10 +08:00
crash_file.write(crash_info)
# 多进程信息
crash_file.write(Process_message)
for process in all_process:
process: multiprocessing.Process
crash_file.write(markdown_line_handler(f'{process.name}', code=True))
crash_file.write(markdown_line_handler(f'Ident: {process.ident}', level=2))
crash_file.write(markdown_line_handler(f'Running: {process.is_alive()}', level=2))
2021-09-16 19:18:06 +08:00
# 运行线程信息
2021-09-02 22:47:10 +08:00
crash_file.write(Thread_message)
2021-09-08 23:38:34 +08:00
for thread in all_thread:
2021-09-09 23:54:03 +08:00
thread: threading.Thread
crash_file.write(markdown_line_handler(f'{thread.name}', code=True))
crash_file.write(markdown_line_handler(f'order: {all_thread.index(thread)}', level=2))
2021-09-02 22:47:10 +08:00
crash_file.write(markdown_line_handler(f'Ident: {thread.ident}', level=2))
crash_file.write(markdown_line_handler(f'Daemon: {thread.isDaemon()}', level=2))
2021-09-09 23:54:03 +08:00
crash_file.write(markdown_line_handler(f'Running: {thread.is_alive()}', level=2))
2021-09-16 19:18:06 +08:00
# Python 信息
crash_file.write(Python_message)
crash_file.write(markdown_line_handler(f'Version: {platform.python_version()}', code=True, level=1))
# crash_file.write(markdown_line_handler(f'Version tuple: {platform.python_version_tuple()}', code=True, level=1))
# crash_file.write(markdown_line_handler(f'Build: {platform.python_build()}', code=True, level=1))
crash_file.write(markdown_line_handler(f'Implementation: {platform.python_implementation()}', code=True, level=1))
crash_file.write(markdown_line_handler(f'Compiler: {platform.python_compiler()}', code=True, level=1))
# 电脑系统信息
2021-09-02 22:47:10 +08:00
crash_file.write(System_message)
2021-09-16 19:18:06 +08:00
crash_file.write(markdown_line_handler(f'System: {platform.platform()}', code=True, level=1))
crash_file.write(markdown_line_handler(f'Computer name: {platform.node()}', code=True, level=1))
crash_file.write(markdown_line_handler(f'machine: {platform.machine()}', code=True, level=1))
crash_file.write(markdown_line_handler(f'processor: {platform.processor()}', code=True, level=1))
crash_file.write(markdown_line_handler(f'release: {platform.release()}', code=True, level=1))
crash_file.write(markdown_line_handler(f'version: {platform.version()}', code=True, level=1))
2021-09-02 22:47:10 +08:00
if __name__ == '__main__':
os.chdir('../')
try:
raise FileNotFoundError('abc')
except:
create_crash_report()