-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_thing.py
More file actions
86 lines (71 loc) · 2.51 KB
/
import_thing.py
File metadata and controls
86 lines (71 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
import datetime
import logging
import os
import pygame
import sys
import traceback
import pymunk
from pygame import *
from pygame.locals import *
from pygame import Vector2 as Ve2
import math as math2
sys.path.append(os.path.dirname(__file__))
import DEBUG
DEBUG_Enable: bool = True
DEBUG_Error: bool = True
GAME_TURTLE = "测试"
SCREEN_SIZE: tuple = (1600, 1200)
REAL_SIZE: tuple = (800, 600)
FPS: int = 20
BACKGROUND: Color | pygame.Surface = Color(0, 0, 0)
ICON: pygame.Surface | None = None
GRAVITY: tuple = (0, 900)
# 配置日志记录
logger = logging.getLogger('game_logger')
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler('log.log')
file_handler.setLevel(logging.INFO)
file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(file_formatter)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(logging.INFO)
stream_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
stream_handler.setFormatter(stream_formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
if DEBUG_Enable:
root, tkinter_handler, frame, frame2 = DEBUG.setup_wx_logger()
logger.addHandler(tkinter_handler)
def new_print(*args):
a = ' '.join(map(str, args))
logger.info(a)
DEBUG.print = new_print
with open('log.log', 'a') as f:
f.write(f'\n{datetime.datetime.now()}:\n\n')
def custom_format_stack(tb: list[traceback.FrameSummary]) -> str:
formatted_stack: list[str] = []
for fra in tb:
filename: str = fra.filename
lineno: int | None = fra.lineno
name: str = f.name
line: str | None = fra.line
formatted_stack.append(f"File: {filename}, Line: {lineno}, Function: {name}, Code: {line}")
return "\n".join(formatted_stack)
def try_except(finally_func: 'function' = lambda: None):
def decorator(func: 'function'):
def wrapper(*args, **kwargs):
try:
a = func(*args, **kwargs)
return a
except Exception as e:
tb: list[traceback.FrameSummary] = traceback.extract_tb(sys.exc_info()[2])
custom_stack: str = custom_format_stack(tb)
error_message: str = f"error: \n{type(e).__name__}: {e}\n{custom_stack}"
logger.error(error_message)
if DEBUG_Enable and DEBUG_Error:
DEBUG.continue_sign = False
finally:
finally_func()
return wrapper
return decorator