Difficult-Rocket/Difficult_Rocket/crash/crash.py

153 lines
6.0 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
2022-06-27 16:51:14 +08:00
# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com
2021-09-08 23:38:34 +08:00
# All rights reserved
# -------------------------------
2021-09-02 22:47:10 +08:00
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
2022-11-20 17:46:02 +08:00
import io
2021-09-02 22:47:10 +08:00
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
2022-11-22 11:43:46 +08:00
from pathlib import Path
2022-08-21 14:59:35 +08:00
from typing import Optional, TextIO
2021-09-02 22:47:10 +08:00
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 写完它
2022-08-03 20:22:36 +08:00
import Difficult_Rocket
2021-09-02 22:47:10 +08:00
Head_message = """# ----- Difficult Rocket Crash Report -----
2022-08-12 21:07:36 +08:00
2022-08-03 20:22:36 +08:00
## Time: {now_time}
2022-08-12 21:07:36 +08:00
2021-09-02 22:47:10 +08:00
## Traceback
"""
2022-08-12 21:07:36 +08:00
Run_message = """\n## Difficult Rocket running status\n"""
2022-08-12 21:07:36 +08:00
DR_configs = """\n### game config\n"""
2021-09-02 22:47:10 +08:00
2022-08-12 21:07:36 +08:00
Process_message = """\n## Process info\n"""
2021-09-16 19:18:06 +08:00
2022-08-12 21:07:36 +08:00
Thread_message = """\n## Thread info\n"""
2022-08-03 20:22:36 +08:00
2022-08-12 21:07:36 +08:00
Python_message = """\n## Python info\n"""
2022-08-03 20:22:36 +08:00
2022-08-12 21:07:36 +08:00
System_message = """\n## System info\n"""
2021-09-02 22:47:10 +08:00
2021-09-09 23:54:03 +08:00
all_thread = [threading.main_thread()]
all_process = [multiprocessing.current_process()]
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:
2022-08-03 20:22:36 +08:00
info = traceback.format_exc().replace('<', '< ')
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
2022-08-03 20:22:36 +08:00
def to_code(string: str):
2022-08-03 20:29:13 +08:00
return f'`{string}`'
2022-08-03 20:22:36 +08:00
2022-08-21 14:59:35 +08:00
def write_markdown_tablet(crash_file: TextIO, tablet: list) -> None:
2022-11-22 11:43:46 +08:00
a_len = max(tablet[1], 6)
b_len = max(tablet[2], 5)
c_len = max(tablet[3], 10)
2022-11-26 21:48:55 +08:00
crash_file.write(f'\n| Option{" " * (a_len - 4)} | Value{" " * (b_len - 3)} | Value Type{" " * (c_len - 8)} |\n')
2022-11-22 11:43:46 +08:00
crash_file.write(f'|:{"-" * (a_len + 3)}|:{"-" * (b_len + 3)}|:{"-" * (c_len + 3)}|\n')
2022-11-26 21:48:55 +08:00
for a, b, c in tablet[0]:
b, c = str(b), str(c)
crash_file.write(f'| `{a}`{" " * (a_len - len(a))} | `{b}`{" " * (b_len - len(b))} | `{c}`{" " * (c_len - len(c))} |\n')
2022-08-21 14:59:35 +08:00
2022-09-04 13:53:09 +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)
2022-11-20 17:46:02 +08:00
cache_stream = io.StringIO()
try:
2021-09-16 19:18:06 +08:00
# 开头信息
2022-11-20 17:46:02 +08:00
cache_stream.write(Head_message.format(now_time=time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(time.time()))))
2021-09-16 19:18:06 +08:00
# 崩溃信息
2022-11-20 17:46:02 +08:00
cache_stream.write(crash_info)
2022-08-03 20:22:36 +08:00
# 运行状态信息
2022-08-12 21:07:36 +08:00
from Difficult_Rocket import DR_option, DR_runtime
2022-11-20 17:46:02 +08:00
cache_stream.write(Run_message)
cache_stream.write(markdown_line_handler(f'DR Version: {Difficult_Rocket.game_version}', level=1))
cache_stream.write(markdown_line_handler(f'DR language: {DR_runtime.language}', level=1))
2022-11-22 11:43:46 +08:00
cache_stream.write(markdown_line_handler(f'Running Dir: {Path(os.curdir).resolve()}', level=1))
2022-08-21 14:59:35 +08:00
option_with_len = DR_runtime.option_with_len()
2022-11-26 21:48:55 +08:00
write_markdown_tablet(crash_file=cache_stream, tablet=option_with_len)
2022-08-03 20:22:36 +08:00
# # DR 的游戏设置
2022-11-20 17:46:02 +08:00
cache_stream.write(DR_configs)
2022-08-21 14:59:35 +08:00
option_with_len = DR_option.option_with_len()
2022-11-26 21:48:55 +08:00
write_markdown_tablet(crash_file=cache_stream, tablet=option_with_len)
# 多进程信息
2022-11-20 17:46:02 +08:00
cache_stream.write(Process_message)
for process in all_process:
process: multiprocessing.Process
2022-11-20 17:46:02 +08:00
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))
2021-09-16 19:18:06 +08:00
# 运行线程信息
2022-11-20 17:46:02 +08:00
cache_stream.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
2022-11-20 17:46:02 +08:00
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))
2021-09-16 19:18:06 +08:00
# Python 信息
2022-11-20 17:46:02 +08:00
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))
2021-09-16 19:18:06 +08:00
# 电脑系统信息
2022-11-20 17:46:02 +08:00
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))
finally:
get_cache = cache_stream.getvalue()
2022-11-26 21:48:55 +08:00
cache_stream.close()
2022-11-20 17:46:02 +08:00
with open('./crash_report/{}'.format(filename), 'w+', encoding='utf-8') as crash_file:
crash_file.write(get_cache)
2021-09-02 22:47:10 +08:00
if __name__ == '__main__':
2022-08-03 20:22:36 +08:00
os.chdir('../../')
2021-09-02 22:47:10 +08:00
try:
raise FileNotFoundError('abc')
2022-08-03 20:22:36 +08:00
except FileNotFoundError:
2021-09-02 22:47:10 +08:00
create_crash_report()