@@ -136,14 +136,23 @@ class Consoler(__consoler_class(), LeverCtl):
136
136
# 只有第一次输入时为None
137
137
__previous_message = None
138
138
139
- def __init__ (self ):
139
+ __init_status = True
140
+
141
+ def __init__ (self , * args , ** kwargs ):
140
142
# 根据环境进行不同的初始化
141
- if in_notebook ():
142
- super ().__init__ ()
143
- else :
144
- super ().__init__ (sys .stdout .buffer )
143
+ try :
144
+ if in_notebook ():
145
+ super ().__init__ ()
146
+ else :
147
+ super ().__init__ (sys .stdout .buffer )
148
+ except :
149
+ self .__init_status = False
145
150
self .original_stdout = sys .stdout # 保存原始的 sys.stdout
146
151
152
+ @property
153
+ def init_status (self ) -> bool :
154
+ return self .__init_status
155
+
147
156
def init (self , path , swanlog_level = "debug" ):
148
157
# 通过当前日期生成日志文件名
149
158
self .now = datetime .now ().strftime ("%Y-%m-%d" )
@@ -153,6 +162,10 @@ def init(self, path, swanlog_level="debug"):
153
162
os .makedirs (path )
154
163
# 日志文件路径
155
164
console_path = os .path .join (path , f"{ self .now } .log" )
165
+ # 如果日志系统初始化失败
166
+ if not self .__init_status :
167
+ with open (console_path , "w" , encoding = "utf-8" ) as f :
168
+ f .write ("Console Recoder Init Failed!" )
156
169
# 日志文件
157
170
self .console = open (console_path , "a" , encoding = "utf-8" )
158
171
@@ -238,7 +251,8 @@ def __init__(self):
238
251
239
252
def init (self , path ):
240
253
self .consoler .init (path )
241
- sys .stdout = self .consoler
254
+ if self .consoler .init_status :
255
+ sys .stdout = self .consoler
242
256
243
257
def reset (self ):
244
258
"""重置输出为原本的样子"""
0 commit comments