Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions Sources/ContainerCommands/Container/ContainerLogs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,7 @@ extension Application {
follow: Bool
) async throws {
if let n {
var buffer = Data()
let size = try fh.seekToEnd()
var offset = size
var lines: [String] = []

while offset > 0, lines.count < n {
let readSize = min(1024, offset)
offset -= readSize
try fh.seek(toOffset: offset)

let data = fh.readData(ofLength: Int(readSize))
buffer.insert(contentsOf: data, at: 0)

if let chunk = String(data: buffer, encoding: .utf8) {
lines = chunk.components(separatedBy: .newlines)
lines = lines.filter { !$0.isEmpty }
}
}

lines = Array(lines.suffix(n))
for line in lines {
for line in try Self.lastLines(fh: fh, n: n) {
print(line)
}
} else {
Expand All @@ -109,6 +89,39 @@ extension Application {
}
}

/// Returns the last `n` complete lines from `fh`, reading backwards in
/// 1024-byte chunks. Exposed (internal) for testing.
///
/// The loop keeps reading until it has accumulated *more* than `n`
/// non-empty lines, or it reaches the start of the file. Reading one
/// line past `n` guarantees the oldest returned line is complete: the
/// first line in the buffer may be a fragment truncated at a chunk
/// boundary, and `suffix(n)` drops it. Stopping at exactly `n`
/// (`lines.count < n`) could return that fragment as the oldest line
/// whenever the last `n` lines span more than one 1024-byte chunk.
static func lastLines(fh: FileHandle, n: Int) throws -> [String] {
var buffer = Data()
let size = try fh.seekToEnd()
var offset = size
var lines: [String] = []

while offset > 0, lines.count <= n {
let readSize = min(1024, offset)
offset -= readSize
try fh.seek(toOffset: offset)

let data = fh.readData(ofLength: Int(readSize))
buffer.insert(contentsOf: data, at: 0)

if let chunk = String(data: buffer, encoding: .utf8) {
lines = chunk.components(separatedBy: .newlines)
lines = lines.filter { !$0.isEmpty }
}
}

return Array(lines.suffix(n))
}

private static func followFile(fh: FileHandle) async throws {
_ = try fh.seekToEnd()
let stream = AsyncStream<String> { cont in
Expand Down
75 changes: 75 additions & 0 deletions Tests/ContainerCommandsTests/ContainerLogsTailTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the container project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

import Foundation
import Testing

@testable import ContainerCommands

struct ContainerLogsTailTests {
private func withTempLog(_ contents: String, _ body: (FileHandle) throws -> Void) throws {
let url = FileManager.default.temporaryDirectory
.appendingPathComponent("container-logs-tail-\(UUID().uuidString).log")
try Data(contents.utf8).write(to: url)
let fh = try FileHandle(forReadingFrom: url)
defer {
try? fh.close()
try? FileManager.default.removeItem(at: url)
}
try body(fh)
}

@Test("returns the last n complete lines for short input")
func lastNShortLines() throws {
let contents = (1...10).map { "line-\($0)" }.joined(separator: "\n") + "\n"
try withTempLog(contents) { fh in
let lines = try Application.ContainerLogs.lastLines(fh: fh, n: 3)
#expect(lines == ["line-8", "line-9", "line-10"])
}
}

@Test("n larger than the line count returns every line")
func nLargerThanLineCount() throws {
try withTempLog("a\nb\n") { fh in
let lines = try Application.ContainerLogs.lastLines(fh: fh, n: 10)
#expect(lines == ["a", "b"])
}
}

@Test("a single line longer than the 1024-byte chunk is not truncated")
func longSingleLineNotTruncated() throws {
// Regression: a log line larger than the 1024-byte backward-read chunk
// was returned with its beginning cut off by `container logs -n 1`.
let long = "START" + String(repeating: "X", count: 3000) + "END"
try withTempLog(long + "\n") { fh in
let lines = try Application.ContainerLogs.lastLines(fh: fh, n: 1)
#expect(lines == [long])
}
}

@Test("multiple lines spanning several chunks keep the oldest line intact")
func longLinesSpanningChunks() throws {
// Each line is 800 bytes; the last two together exceed one 1024-byte
// chunk, which previously truncated the older of the two.
let a = String(repeating: "A", count: 800)
let b = String(repeating: "B", count: 800)
let c = String(repeating: "C", count: 800)
try withTempLog([a, b, c].joined(separator: "\n") + "\n") { fh in
let lines = try Application.ContainerLogs.lastLines(fh: fh, n: 2)
#expect(lines == [b, c])
}
}
}