Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flush api to default stdout/stderr logger #87

Merged
merged 2 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 21 additions & 5 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,19 +531,35 @@ public struct MultiplexLogHandler: LogHandler {
/// cross-thread interleaving of output.
internal struct StdioOutputStream: TextOutputStream {
internal let file: UnsafeMutablePointer<FILE>
internal let flushMode: FlushMode

internal func write(_ string: String) {
string.withCString { ptr in
flockfile(file)
flockfile(self.file)
defer {
funlockfile(file)
funlockfile(self.file)
}
_ = fputs(ptr, self.file)
if case .always = self.flushMode {
self.flush()
}
_ = fputs(ptr, file)
}
}

internal static let stderr = StdioOutputStream(file: systemStderr)
internal static let stdout = StdioOutputStream(file: systemStdout)
/// Flush the underlying stream.
/// This has no effect when using the `.always` flush mode, which is the default
internal func flush() {
_ = fflush(self.file)
}

internal static let stderr = StdioOutputStream(file: systemStderr, flushMode: .always)
internal static let stdout = StdioOutputStream(file: systemStdout, flushMode: .always)

/// Defines the flushing strategy for the underlying stream.
internal enum FlushMode {
case undefined
case always
}
}

// Prevent name clashes
Expand Down
1 change: 1 addition & 0 deletions Tests/LoggingTests/LoggingTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extension LoggingTest {
("testLogLevelCases", testLogLevelCases),
("testLogLevelOrdering", testLogLevelOrdering),
("testStreamLogHandlerWritesToAStream", testStreamLogHandlerWritesToAStream),
("testStdioOutputStreamFlush", testStdioOutputStreamFlush),
]
}
}
68 changes: 68 additions & 0 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
@testable import Logging
import XCTest

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
#else
import Glibc
#endif

class LoggingTest: XCTestCase {
func testAutoclosure() throws {
// bootstrap with our test logging impl
Expand Down Expand Up @@ -444,4 +450,66 @@ class LoggingTest: XCTestCase {
XCTAssertTrue(messageSucceeded ?? false)
XCTAssertEqual(interceptStream.strings.count, 1)
}

func testStdioOutputStreamFlush() {
// flush on every statement
self.testStdioOutputStreamFlush { writeFD, readFD, readBuffer in
let logStream = StdioOutputStream(file: writeFD, flushMode: .always)
LoggingSystem.bootstrapInternal { StreamLogHandler(label: $0, stream: logStream) }
Logger(label: "test").critical("test")

let size = read(readFD, readBuffer, 256)
XCTAssertGreaterThan(size, -1, "expected flush")

logStream.flush()
let size2 = read(readFD, readBuffer, 256)
XCTAssertEqual(size2, -1, "expected no flush")
}
// default flushing
self.testStdioOutputStreamFlush { writeFD, readFD, readBuffer in
let logStream = StdioOutputStream(file: writeFD, flushMode: .undefined)
LoggingSystem.bootstrapInternal { StreamLogHandler(label: $0, stream: logStream) }
Logger(label: "test").critical("test")

let size = read(readFD, readBuffer, 256)
XCTAssertEqual(size, -1, "expected no flush")

logStream.flush()
let size2 = read(readFD, readBuffer, 256)
XCTAssertGreaterThan(size2, -1, "expected flush")
}
}

func testStdioOutputStreamFlush(_ body: (UnsafeMutablePointer<FILE>, CInt, UnsafeMutablePointer<Int8>) -> Void) {
tomerd marked this conversation as resolved.
Show resolved Hide resolved
var fds: [Int32] = [-1, -1]
fds.withUnsafeMutableBufferPointer { ptr in
let err = pipe(ptr.baseAddress!)
assert(err == 0, "pipe: \(err)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it's in a test case, you can use XCTAssertEqual(0, err)

}

let writeFD = fdopen(fds[1], "w")
let writeBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: 256)
defer {
writeBuffer.deinitialize(count: 256)
writeBuffer.deallocate()
}

var err = setvbuf(writeFD, writeBuffer, _IOFBF, 256)
assert(err == 0)
tomerd marked this conversation as resolved.
Show resolved Hide resolved

let readFD = fds[0]
err = fcntl(readFD, F_SETFL, fcntl(readFD, F_GETFL) | O_NONBLOCK)
assert(err == 0)
tomerd marked this conversation as resolved.
Show resolved Hide resolved

let readBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: 256)
defer {
readBuffer.deinitialize(count: 256)
readBuffer.deallocate()
}

// the actual test
body(writeFD!, readFD, readBuffer)

fds.forEach { close($0) }
}
}