Skip to content

Instantly share code, notes, and snippets.

@Samasaur1
Last active January 19, 2025 19:03
Show Gist options
  • Save Samasaur1/fc0ac732bde806f5b7d84d6b71d7e20f to your computer and use it in GitHub Desktop.
Save Samasaur1/fc0ac732bde806f5b7d84d6b71d7e20f to your computer and use it in GitHub Desktop.
print to standard error in Swift
internal struct FileHandleOutputStream: TextOutputStream {
private let fileHandle: FileHandle
let encoding: String.Encoding
init(_ fileHandle: FileHandle, encoding: String.Encoding = .utf8) {
self.fileHandle = fileHandle
self.encoding = encoding
}
mutating func write(_ string: String) {
if let data = string.data(using: encoding) {
fileHandle.write(data)
}
}
}
internal var STDERR = FileHandleOutputStream(.standardError)
internal var STDOUT = FileHandleOutputStream(.standardOutput)
print("Hello, world!, to: &STDERR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment