Skip to content

Commit ee41a94

Browse files
committed
Prevent console logging from freezing UI
1 parent 114bc56 commit ee41a94

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

Viewer/Document.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,7 @@ class Document: NSDocument, EvaluationDelegate {
477477
DispatchQueue.main.async {
478478
for viewController in self.sceneViewControllers {
479479
viewController.showConsole = true
480-
viewController.appendLog(
481-
NSAttributedString(string: line + "\n", attributes: [
482-
.foregroundColor: NSColor.textColor,
483-
.font: NSFont.systemFont(ofSize: 13),
484-
])
485-
)
480+
viewController.appendLog(line + "\n")
486481
}
487482
}
488483
}

Viewer/SceneViewController.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,40 @@ class SceneViewController: NSViewController {
4545
}
4646
}
4747

48+
private var logLength: Int = 0
49+
4850
func clearLog() {
51+
logLength = 0
4952
consoleTextView.textStorage?.setAttributedString(NSAttributedString(string: ""))
5053
}
5154

52-
func appendLog(_ text: NSAttributedString) {
53-
if text.string.isEmpty {
55+
func appendLog(_ text: String) {
56+
if text.isEmpty {
5457
return
5558
}
56-
consoleTextView.textStorage?.append(text)
59+
let logLimit = 20000
60+
let charCount = text.count
61+
logLength += charCount
62+
if logLength > logLimit {
63+
if logLength - charCount > logLimit {
64+
return
65+
}
66+
consoleTextView.textStorage?.append(NSAttributedString(
67+
string: "Console limit exceeded. No further logs will be printed.",
68+
attributes: [
69+
.foregroundColor: NSColor.red,
70+
.font: NSFont.systemFont(ofSize: 13),
71+
]
72+
))
73+
} else {
74+
consoleTextView.textStorage?.append(NSAttributedString(
75+
string: text,
76+
attributes: [
77+
.foregroundColor: NSColor.textColor,
78+
.font: NSFont.systemFont(ofSize: 13),
79+
]
80+
))
81+
}
5782
DispatchQueue.main.async {
5883
self.consoleTextView.scrollToEndOfDocument(self)
5984
}

0 commit comments

Comments
 (0)