Skip to content

Commit 51e7961

Browse files
committed
Append a suffix at then end of a chunk logged by splitLongMultilineMessage
`os_log` truncates trailing whitespace in a log message. If a chunk containing the file contents ends with a newline, this means that the newline will not get logged and thus the file contents read from the log aren't correct. Append an end marker to each chunk to prevent this trimming.
1 parent a9242f8 commit 51e7961

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Sources/LSPLogging/SplitLogMessage.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
/// Splits `message` on newline characters such that each chunk is at most `maxChunkSize` bytes long.
1414
///
15-
/// The intended use case for this is to split compiler arguments into multiple chunks so that each chunk doesn't exceed
16-
/// the maximum message length of `os_log` and thus won't get truncated.
15+
/// The intended use case for this is to split compiler arguments and a file's contents into multiple chunks so
16+
/// that each chunk doesn't exceed the maximum message length of `os_log` and thus won't get truncated.
1717
///
1818
/// - Note: This will only split along newline boundary. If a single line is longer than `maxChunkSize`, it won't be
1919
/// split. This is fine for compiler argument splitting since a single argument is rarely longer than 800 characters.
@@ -23,6 +23,12 @@ public func splitLongMultilineMessage(message: String, maxChunkSize: Int = 800)
2323
if let lastChunk = chunks.last, lastChunk.utf8.count + line.utf8.count < maxChunkSize {
2424
chunks[chunks.count - 1] += "\n" + line
2525
} else {
26+
if !chunks.isEmpty {
27+
// Append an end marker to the last chunk so that os_log doesn't truncate trailing whitespace,
28+
// which would modify the source contents.
29+
// Empty newlines are important so the offset of the request is correct.
30+
chunks[chunks.count - 1] += "\n--- End Chunk"
31+
}
2632
chunks.append(String(line))
2733
}
2834
}

0 commit comments

Comments
 (0)