启用高级日志功能

This commit is contained in:
星外之神 2022-07-24 12:46:59 +08:00
parent 8d6526bd66
commit efb5db0c69

21
main.py
View File

@ -1,16 +1,28 @@
#!/usr/bin/env python
import pygame as pg
import sys
import logging
import traceback
import os
from logging.handlers import RotatingFileHandler
from source import tool
from source import constants as c
from source.state import mainmenu, screen, level
if __name__=='__main__':
# 控制状态机运行
# 日志设置
if not os.path.exists(os.path.dirname(c.USERLOG_PATH)):
os.makedirs(os.path.dirname(c.USERLOG_PATH))
sys.stderr = open(c.USERLOG_PATH, "w")
logger = logging.getLogger()
formatter = logging.Formatter('%(asctime)s: %(message)s')
file_handler = RotatingFileHandler(c.USERLOG_PATH, "a", 1024*1024, 0, "utf-8")
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
try:
# 控制状态机运行
game = tool.Control()
state_dict = { c.MAIN_MENU: mainmenu.Menu(),
c.GAME_VICTORY: screen.GameVictoryScreen(),
@ -19,4 +31,5 @@ if __name__=='__main__':
}
game.setup_states(state_dict, c.MAIN_MENU)
game.run()
except:
logger.error(f'\n{traceback.format_exc()}')