From 21b67c364c3704a85f9e7a07b6574414647834b0 Mon Sep 17 00:00:00 2001 From: shenjack-mac <3695888@qq.com> Date: Sun, 9 Apr 2023 03:21:11 +0800 Subject: [PATCH] add some typing write sysinfo to logs --- Difficult_Rocket/__init__.py | 4 ++-- Difficult_Rocket/client/__init__.py | 4 ++-- Difficult_Rocket/crash/__init__.py | 12 ++++++++---- Difficult_Rocket/main.py | 10 ++++++++++ configs/lang/en-us.toml | 5 +++++ configs/lang/zh-CN.toml | 13 +++++++++---- 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Difficult_Rocket/__init__.py b/Difficult_Rocket/__init__.py index 89c2539..232053c 100644 --- a/Difficult_Rocket/__init__.py +++ b/Difficult_Rocket/__init__.py @@ -11,11 +11,11 @@ github: @shenjackyuanjie gitee: @shenjackyuanjie """ -import contextlib # import ctypes import sys import warnings import traceback +import contextlib from typing import Optional from Difficult_Rocket.api.types import Options @@ -70,7 +70,7 @@ class _DR_option(Options): # tests playing: bool = False debugging: bool = False - crash_report_test: bool = False + crash_report_test: bool = True pyglet_macosx_dev_test: bool = True # window option diff --git a/Difficult_Rocket/client/__init__.py b/Difficult_Rocket/client/__init__.py index a4a451c..1ab08e0 100644 --- a/Difficult_Rocket/client/__init__.py +++ b/Difficult_Rocket/client/__init__.py @@ -92,8 +92,8 @@ class Client: self.use_time = end_time - start_time if DR_option.use_DR_rust: from libs.Difficult_Rocket_rs import read_ship_test, part_list_read_test - part_list_read_test() - read_ship_test() + # part_list_read_test() + # read_ship_test() self.logger.info(tr().client.setup.use_time().format(Decimal(self.use_time) / 1000000000)) self.logger.debug(tr().client.setup.use_time_ns().format(self.use_time)) diff --git a/Difficult_Rocket/crash/__init__.py b/Difficult_Rocket/crash/__init__.py index 3ebf999..5d6f2b8 100644 --- a/Difficult_Rocket/crash/__init__.py +++ b/Difficult_Rocket/crash/__init__.py @@ -19,7 +19,7 @@ import traceback import threading import multiprocessing from pathlib import Path -from typing import Optional, TextIO +from typing import Optional, Union, TextIO # import psutil # for more system info @@ -52,7 +52,7 @@ all_thread = [threading.main_thread()] all_process = [multiprocessing.current_process()] -def crash_info_handler(info: str = None) -> str: +def crash_info_handler(info: Optional[str] = None) -> str: if not info: info = traceback.format_exc().replace('<', '< ') format_info = f"
\n{info}
\n" @@ -60,7 +60,7 @@ def crash_info_handler(info: str = None) -> str: return format_info -def markdown_line_handler(string: Optional[str or bool or int or float], code: bool = False, level: int = 1, +def markdown_line_handler(string: Optional[Union[str, bool, int, float]], code: bool = False, level: int = 1, end: str = '\n') -> str: lvl = '- ' * level f_string = string @@ -85,7 +85,7 @@ def write_markdown_tablet(crash_file: TextIO, tablet: list) -> None: f'| `{a}`{" " * (a_len - len(a))} | `{b}`{" " * (b_len - len(b))} | `{c}`{" " * (c_len - len(c))} |\n') -def create_crash_report(info: str = None) -> None: +def create_crash_report(info: Optional[str] = None) -> None: crash_info = crash_info_handler(info) if 'crash_report' not in os.listdir('./'): os.mkdir('./crash_report') @@ -94,6 +94,7 @@ def create_crash_report(info: str = None) -> None: cache_stream = io.StringIO() try: write_cache(cache_stream, crash_info) + write_info_to_cache(cache_stream) finally: get_cache = cache_stream.getvalue() cache_stream.close() @@ -106,6 +107,9 @@ def write_cache(cache_stream, crash_info): cache_stream.write(Head_message.format(now_time=time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(time.time())))) # 崩溃信息 cache_stream.write(crash_info) + + +def write_info_to_cache(cache_stream): # 运行状态信息 from Difficult_Rocket import DR_option, DR_runtime cache_stream.write(Run_message) diff --git a/Difficult_Rocket/main.py b/Difficult_Rocket/main.py index ab26c2a..56152bc 100644 --- a/Difficult_Rocket/main.py +++ b/Difficult_Rocket/main.py @@ -18,12 +18,15 @@ import logging import logging.config import multiprocessing +from io import StringIO + if __name__ == '__main__': # been start will not run this sys.path.append('/bin/libs') sys.path.append('/bin') from Difficult_Rocket import client, server, DR_option +from Difficult_Rocket.crash import write_info_to_cache from Difficult_Rocket.utils import tools from Difficult_Rocket.utils.translate import tr @@ -53,8 +56,15 @@ class Game: self.logger.info(tr().language_set_to()) self.logger.info(tr().main.logger.created()) # version check + self.log_env() self.python_version_check() self.setup() + + def log_env(self) -> None: + cache_steam = StringIO() + write_info_to_cache(cache_steam) + text = cache_steam.getvalue() + self.logger.info(text) def setup(self) -> None: self.client = client.Client(net_mode='local') diff --git a/configs/lang/en-us.toml b/configs/lang/en-us.toml index 1057563..10e9b5e 100644 --- a/configs/lang/en-us.toml +++ b/configs/lang/en-us.toml @@ -3,6 +3,11 @@ language_set_to = "Language set to: English" language_available = "Available languages list: {}" [main] +env.version.python = "Python version: {}" +env.version.DR = "Difficult_Rocket version: {}" +env.version.DR_rs = "Difficult_Rocket_rs version: {}" +env.version.pyglet = "pyglet version: {}" +env.version.pyglet_rs = "pyglet_rs version: {}" version.now_on = "Difficult Rocket is running on Python Vision" version.need3p = "Difficult Rocket depends on Python vision 3.0+" version.best38p = "Difficult Rocket is writen in Python {write_py_v}, and now is running on Python {py_v} may cause BUG" diff --git a/configs/lang/zh-CN.toml b/configs/lang/zh-CN.toml index 744bbca..07abd4d 100644 --- a/configs/lang/zh-CN.toml +++ b/configs/lang/zh-CN.toml @@ -3,10 +3,15 @@ language_set_to = "语言已经设置为:简体中文" language_available = "可用语言列表: {}" [main] -version.now_on = "困难火箭的运行Python环境为" -version.need3p = "困难火箭需要Python3.0+的环境" -version.best38p = "困难火箭是在Python {write_py_v}的环境下编写的,目前正在运行在Python {py_v}的环境下,可能产生BUG" -lang.language = "简 体 中 文 (zh-cn)" +env.version.python = "Python 版本: {}" +env.version.DR = "Difficult_Rocket 版本: {}" +env.version.DR_rs = "Difficult_Rocket_rs 版本: {}" +env.version.pyglet = "pyglet 版本: {}" +env.version.pyglet_rs = "pyglet_rs 版本: {}" +version.now_on = "困难火箭的运行 Python 环境为" +version.need3p = "困难火箭需要 Python3.0+ 的环境" +version.best38p = "困难火箭是在 Python {write_py_v} 的环境下编写的,目前正在运行在Python {py_v}的环境下,可能产生BUG" +lang.language = "简 体 中 文 (zh-cn)" logger.language = "日志记录语言为:" logger.created = "日志处理器创建完成" logger.mkdir = "logs/ 文件夹不存在,已经新建文件夹"