Skip to content

Commit 5fa412d

Browse files
Fixbug/swan consoler (#423)
* fix: consoler init * fix: failed log * Update console.py --------- Co-authored-by: KAAANG <[email protected]>
1 parent 4a6c365 commit 5fa412d

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

swanlab/log/console.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,23 @@ class Consoler(__consoler_class(), LeverCtl):
136136
# 只有第一次输入时为None
137137
__previous_message = None
138138

139-
def __init__(self):
139+
__init_status = True
140+
141+
def __init__(self, *args, **kwargs):
140142
# 根据环境进行不同的初始化
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
145150
self.original_stdout = sys.stdout # 保存原始的 sys.stdout
146151

152+
@property
153+
def init_status(self) -> bool:
154+
return self.__init_status
155+
147156
def init(self, path, swanlog_level="debug"):
148157
# 通过当前日期生成日志文件名
149158
self.now = datetime.now().strftime("%Y-%m-%d")
@@ -153,6 +162,10 @@ def init(self, path, swanlog_level="debug"):
153162
os.makedirs(path)
154163
# 日志文件路径
155164
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!")
156169
# 日志文件
157170
self.console = open(console_path, "a", encoding="utf-8")
158171

@@ -238,7 +251,8 @@ def __init__(self):
238251

239252
def init(self, path):
240253
self.consoler.init(path)
241-
sys.stdout = self.consoler
254+
if self.consoler.init_status:
255+
sys.stdout = self.consoler
242256

243257
def reset(self):
244258
"""重置输出为原本的样子"""

vue/src/views/experiment/pages/log/LogPage.vue

+4-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const indexWidth = computed(() => {
122122
if (l) {
123123
index = lines.value[l - 1].index === '' ? lines.value[l - 2].index : lines.value[l - 1].index
124124
}
125+
if (!index) return '0px'
125126
return 8 * index.length + 'px'
126127
})
127128
@@ -222,7 +223,9 @@ const computeRange = (e) => {
222223
* 一般行高是 16px,基本不会出问题,所以目前只在初始化时计算,而没做另外的事件监听
223224
*/
224225
const computeLineHeight = () => {
225-
const line_styles = window.getComputedStyle(document.querySelector('.log-line'))
226+
const e = document.querySelector('.log-line')
227+
if (!e) return 0
228+
const line_styles = window.getComputedStyle(e)
226229
lineHeight.value = parseFloat(line_styles.getPropertyValue('line-height'))
227230
}
228231

0 commit comments

Comments
 (0)