Difficult-Rocket/Difficult_Rocket/crash/__init__.py

148 lines
5.3 KiB
Python
Raw Normal View History

2021-10-30 20:19:12 +08:00
# -------------------------------
# Difficult Rocket
2023-01-20 14:08:12 +08:00
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
2021-10-30 20:19:12 +08:00
# All rights reserved
# -------------------------------
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
2021-10-31 14:58:09 +08:00
2023-02-25 09:18:41 +08:00
import io
import os
import time
import platform
import traceback
import threading
import multiprocessing
from pathlib import Path
2023-05-10 00:52:56 +08:00
from typing import Optional, Union
2021-10-31 14:58:09 +08:00
2023-02-25 09:18:41 +08:00
# import psutil
# for more system info
# where the crash report from
# this can't be crash , or the game will really crash!
import Difficult_Rocket
Head_message = """# ----- Difficult Rocket Crash Report -----
## Time: {now_time}
## Traceback
"""
Run_message = """\n## Difficult Rocket running status\n"""
DR_configs = """\n### game config\n"""
Process_message = """\n## Process info\n"""
Thread_message = """\n## Thread info\n"""
Python_message = """\n## Python info\n"""
System_message = """\n## System info\n"""
all_thread = [threading.main_thread()]
all_process = [multiprocessing.current_process()]
2023-04-09 03:21:11 +08:00
def crash_info_handler(info: Optional[str] = None) -> str:
2023-02-25 09:18:41 +08:00
if not info:
info = traceback.format_exc().replace('<', '< ')
format_info = f"<pre>\n{info}</pre>\n"
format_info.replace('<module>', '< module>')
return format_info
2023-04-09 03:21:11 +08:00
def markdown_line_handler(string: Optional[Union[str, bool, int, float]], code: bool = False, level: int = 1,
2023-02-25 09:18:41 +08:00
end: str = '\n') -> str:
lvl = '- ' * level
f_string = string
if code:
f_string = f'`{f_string}`'
return f'{lvl}{f_string}{end}'
def to_code(string: str):
return f'`{string}`'
2023-04-09 03:21:11 +08:00
def create_crash_report(info: Optional[str] = None) -> None:
2023-02-25 09:18:41 +08:00
crash_info = crash_info_handler(info)
if 'crash_report' not in os.listdir('./'):
os.mkdir('./crash_report')
2023-07-16 00:20:11 +08:00
date_time = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime())
2023-02-25 09:18:41 +08:00
filename = f'crash-{date_time}.md'
cache_stream = io.StringIO()
try:
write_cache(cache_stream, crash_info)
2023-04-09 03:21:11 +08:00
write_info_to_cache(cache_stream)
2023-02-25 09:18:41 +08:00
finally:
get_cache = cache_stream.getvalue()
cache_stream.close()
with open(f'./crash_report/{filename}', 'w+', encoding='utf-8') as crash_file:
crash_file.write(get_cache)
def write_cache(cache_stream, crash_info):
# 开头信息
2023-07-16 00:20:11 +08:00
cache_stream.write(Head_message.format(now_time=time.strftime('%Y/%m/%d %H:%M:%S', time.localtime())))
2023-02-25 09:18:41 +08:00
# 崩溃信息
cache_stream.write(crash_info)
2023-04-09 03:21:11 +08:00
def write_info_to_cache(cache_stream):
2023-02-25 09:18:41 +08:00
# 运行状态信息
from Difficult_Rocket import DR_status
from Difficult_Rocket.runtime import DR_runtime
2023-02-25 09:18:41 +08:00
cache_stream.write(Run_message)
2023-07-01 21:57:54 +08:00
cache_stream.write(markdown_line_handler(f'DR Version: {Difficult_Rocket.sdk_version}', level=1))
2023-02-25 09:18:41 +08:00
cache_stream.write(markdown_line_handler(f'DR language: {DR_runtime.language}', level=1))
cache_stream.write(markdown_line_handler(f'Running Dir: {Path(os.curdir).resolve()}', level=1))
2023-05-10 00:52:56 +08:00
cache_stream.write(f"\n{DR_runtime.as_markdown()}")
cache_stream.write(DR_configs)
2023-06-16 23:36:24 +08:00
cache_stream.write(f"\n{DR_status.as_markdown()}")
2023-05-10 00:52:56 +08:00
cache_stream.write(Process_message)
2023-02-25 09:18:41 +08:00
for process in all_process:
process: multiprocessing.Process
cache_stream.write(markdown_line_handler(f'{process.name}', code=True))
cache_stream.write(markdown_line_handler(f'Ident: {process.ident}', level=2))
cache_stream.write(markdown_line_handler(f'Running: {process.is_alive()}', level=2))
# 运行线程信息
cache_stream.write(Thread_message)
for thread in all_thread:
thread: threading.Thread
cache_stream.write(markdown_line_handler(f'{thread.name}', code=True))
cache_stream.write(markdown_line_handler(f'order: {all_thread.index(thread)}', level=2))
cache_stream.write(markdown_line_handler(f'Ident: {thread.ident}', level=2))
cache_stream.write(markdown_line_handler(f'Daemon: {thread.daemon}', level=2))
cache_stream.write(markdown_line_handler(f'Running: {thread.is_alive()}', level=2))
# Python 信息
cache_stream.write(Python_message)
cache_stream.write(markdown_line_handler(f'Version: {to_code(platform.python_version())}', level=1))
cache_stream.write(markdown_line_handler(f'Branch: {to_code(platform.python_branch())}', level=1))
cache_stream.write(markdown_line_handler(f'Implementation: {to_code(platform.python_implementation())}', level=1))
cache_stream.write(markdown_line_handler(f'Compiler: {to_code(platform.python_compiler())}', level=1))
# 电脑系统信息
cache_stream.write(System_message)
cache_stream.write(markdown_line_handler(f'System: {to_code(platform.platform())}', level=1))
cache_stream.write(markdown_line_handler(f'Computer name: {to_code(platform.node())}', level=1))
cache_stream.write(markdown_line_handler(f'machine: {to_code(platform.machine())}', level=1))
cache_stream.write(markdown_line_handler(f'processor: {to_code(platform.processor())}', level=1))
cache_stream.write(markdown_line_handler(f'release: {to_code(platform.release())}', level=1))
cache_stream.write(markdown_line_handler(f'version: {to_code(platform.version())}', level=1))
if __name__ == '__main__':
os.chdir('../../')
try:
raise FileNotFoundError('abc')
except FileNotFoundError:
create_crash_report()