Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created July 11, 2016 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwaldowski/4ed2fd48da2af3193b0cbd3ec1883c9d to your computer and use it in GitHub Desktop.
Save zwaldowski/4ed2fd48da2af3193b0cbd3ec1883c9d to your computer and use it in GitHub Desktop.
import Darwin
extension Process {
// See also: https://github.com/apple/swift/blob/9cdbec13eee72feccfc5f8b987882a8c52e8107b/stdlib/private/SwiftPrivate/IO.swift#L84
public struct Stream: OutputStreamType {
private let fd: UnsafeMutablePointer<FILE>
private init(fd: UnsafeMutablePointer<FILE>) {
self.fd = fd
}
public func write(string: String) {
flockfile(fd)
for c in string.utf8 {
putc_unlocked(numericCast(c), fd)
}
funlockfile(fd)
}
}
@_transparent
public static var stdout: Stream {
get {
return Stream(fd: Darwin.stdout)
}
set { }
}
@_transparent
public static var stderr: Stream {
get {
return Stream(fd: Darwin.stderr)
}
set { }
}
}
print("I am an error!", toStream: &Process.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment