Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

Launch logs collapse repeated lines and stop at 256 MB

Gauthier Piarrette0c28aa4280fd
diff --git a/Sources/HighballKit/LogFilter.swift b/Sources/HighballKit/LogFilter.swift
new file mode 100644
index 0000000..03ece3e
--- /dev/null
+++ b/Sources/HighballKit/LogFilter.swift
@@ -0,0 +1,57 @@
+import Foundation
+
+/// Keeps a launch log readable and bounded. A line repeated back to back is written once, then
+/// summarised ("the line above repeated N more times"), with a running count every
+/// `progressEvery` repeats so a log cut short still says what filled it. Past `capBytes` the
+/// log stops growing and says so once.
+///
+/// highball#224: msync printed "node memory pool exhausted" millions of times and a single
+/// My Summer Car session left a 1.4 GB log (a second one 2.4 GB), the cause buried in one line.
+public struct LogFilter {
+ public var progressEvery = 10_000
+ public var capBytes = 256 * 1024 * 1024
+ private var last: String?
+ private var repeats = 0
+ private var written = 0
+ private var capped = false
+
+ public init(progressEvery: Int = 10_000, capBytes: Int = 256 * 1024 * 1024) {
+ self.progressEvery = progressEvery
+ self.capBytes = capBytes
+ }
+
+ /// The lines to write for one line of output (none while it only repeats the previous one).
+ public mutating func feed(_ line: String) -> [String] {
+ if line == last {
+ repeats += 1
+ return repeats % progressEvery == 0 ? admit(["# the line above repeated \(repeats) more times so far"]) : []
+ }
+ var out: [String] = []
+ if repeats > 0 { out.append("# the line above repeated \(repeats) more times") }
+ out.append(line)
+ last = line
+ repeats = 0
+ return admit(out)
+ }
+
+ /// Anything still owed at the end of the output.
+ public mutating func finish() -> [String] {
+ defer { repeats = 0 }
+ return repeats > 0 ? admit(["# the line above repeated \(repeats) more times"]) : []
+ }
+
+ private mutating func admit(_ lines: [String]) -> [String] {
+ if capped { return [] }
+ var out: [String] = []
+ for l in lines {
+ written += l.utf8.count + 1
+ if written > capBytes {
+ capped = true
+ out.append("# log capped at \(capBytes / (1024 * 1024)) MB, the rest of this session's output was not written")
+ break
+ }
+ out.append(l)
+ }
+ return out
+ }
+}
diff --git a/Sources/HighballKit/WineRunner.swift b/Sources/HighballKit/WineRunner.swift
index a78f6e9..a103a5a 100644
--- a/Sources/HighballKit/WineRunner.swift
+++ b/Sources/HighballKit/WineRunner.swift
@@ -194,19 +194,29 @@ public struct WineRunner: Sendable {
// write(contentsOf:) can't raise; a failed log write is dropped, never fatal.
DispatchQueue.global(qos: .utility).async {
var buffer = Data()
+ // Repeated lines are collapsed and the log is capped (LogFilter, highball#224),
+ // for the file and for the live log alike.
+ var filter = LogFilter()
while true {
let chunk = reader.availableData
if chunk.isEmpty { break }
- try? logHandle.write(contentsOf: chunk)
buffer.append(chunk)
+ var out = ""
while let nl = buffer.firstIndex(of: 0x0A) {
let line = String(decoding: buffer[..<nl], as: UTF8.self)
buffer.removeSubrange(...nl)
if line.contains("import_dll") { importFailures.append(line) }
- onOutput?(line)
+ for kept in filter.feed(line) {
+ out += kept + "\n"
+ onOutput?(kept)
+ }
}
+ if !out.isEmpty { try? logHandle.write(contentsOf: Data(out.utf8)) }
}
- if !buffer.isEmpty { onOutput?(String(decoding: buffer, as: UTF8.self)) }
+ var rest = filter.finish()
+ if !buffer.isEmpty { rest += filter.feed(String(decoding: buffer, as: UTF8.self)) + filter.finish() }
+ for kept in rest { onOutput?(kept) }
+ if !rest.isEmpty { try? logHandle.write(contentsOf: Data((rest.joined(separator: "\n") + "\n").utf8)) }
try? logHandle.close()
}
}
diff --git a/Tests/HighballKitTests/LogFilterTests.swift b/Tests/HighballKitTests/LogFilterTests.swift
new file mode 100644
index 0000000..d3b82e7
--- /dev/null
+++ b/Tests/HighballKitTests/LogFilterTests.swift
@@ -0,0 +1,43 @@
+import XCTest
+@testable import HighballKit
+
+/// highball#224: one msync warning repeated millions of times made a 1.4 GB log. Repeats are
+/// collapsed into a count, and a runaway log stops at a cap and says so.
+final class LogFilterTests: XCTestCase {
+ func testRepeatedLinesCollapseIntoACount() {
+ var f = LogFilter(progressEvery: 1000)
+ var out: [String] = []
+ out += f.feed("start")
+ for _ in 0..<5 { out += f.feed("msync: warn: node memory pool exhausted") }
+ out += f.feed("err: something else")
+ out += f.finish()
+ XCTAssertEqual(out, ["start", "msync: warn: node memory pool exhausted",
+ "# the line above repeated 4 more times", "err: something else"])
+ }
+
+ func testALongRunOfRepeatsReportsProgressAndTheRemainderAtTheEnd() {
+ var f = LogFilter(progressEvery: 3)
+ var out: [String] = []
+ for _ in 0..<8 { out += f.feed("x") }
+ out += f.finish()
+ XCTAssertEqual(out, ["x", "# the line above repeated 3 more times so far",
+ "# the line above repeated 6 more times so far",
+ "# the line above repeated 7 more times"])
+ }
+
+ func testTheLogStopsAtTheCapAndSaysSoOnce() {
+ var f = LogFilter(capBytes: 20)
+ var out: [String] = []
+ for i in 0..<10 { out += f.feed("line \(i)") }
+ XCTAssertEqual(out.first, "line 0")
+ XCTAssertTrue(out.last?.hasPrefix("# log capped at") ?? false)
+ XCTAssertEqual(out.filter { $0.hasPrefix("# log capped") }.count, 1)
+ XCTAssertTrue(f.feed("more").isEmpty)
+ }
+
+ func testDistinctLinesPassThroughUnchanged() {
+ var f = LogFilter()
+ let lines = ["# gin header", "a", "b", "a", "b"]
+ XCTAssertEqual(lines.flatMap { f.feed($0) } + f.finish(), lines)
+ }
+}