How can I output to STDERR with Swift?
For Swift 4.x:
import Darwin
fputs("hello from libc\n", stderr)
How to print on stderr with Swift?
From https://swift.org/blog/swift-linux-port/:
The Glibc Module: Most of the Linux C standard library is available through this module similar to the Darwin module on Apple platforms.
So this should work on all Swift platforms:
#if os(Linux)
import Glibc
#else
import Darwin
#endif
public struct StderrOutputStream: OutputStreamType {
public mutating func write(string: String) { fputs(string, stderr) }
}
public var errStream = StderrOutputStream()
debugPrint("Debug messages...", toStream: &errStream)
Update for Swift 3:
public struct StderrOutputStream: TextOutputStream {
public mutating func write(_ string: String) { fputs(string, stderr) }
}
public var errStream = StderrOutputStream()
debugPrint("Debug messages...", to: &errStream) // "Debug messages..."
print("Debug messages...", to: &errStream) // Debug messages...
swift print doesn't appear in STDOut but 3rd party c library logs do when running in docker on ECS
Had the same issue, I filed a radar, and Apple answered:
When piped to another process print is buffered, so no characters appear until the buffer is filled up. (When piped to the terminal we only buffer until we hit a newline.)
You can get the behavior you want by calling
setbuf(stdout, nil)
once at startup:
import Darwin
setbuf(stdout, nil)
Swift stderr and stout into single file
Have you tried just calling freopen
twice? Once with stderr
and once with stdout
? I think that worked fine for my purposes when I tried it.
Swift Print function formatting
Yes, it is similar to ${} in javaScript, and it is called String Interpolation:
String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal.
You can find more detailed information in here: Swift Documentation
under the section "String Interpolation".
How to print an array of characters in swift without a line terminator
In a Playground you need to print one final newline, otherwise the output
is not flushed to the output window:
for chrInStr in strTokenizeMe { print(chrInStr, terminator: " ")}
print()
A (not necessarily better) alternative would be to concatenate
the characters before printing them:
print(strTokenizeMe.map(String.init).joined(separator: " "))
The problem does not occur when running a compiled program, because
the standard output is always flushed on program exit.
How can I print some values of data? [Swift]
I'd define a type conforming to the Codable protocol and use a JSONDecoder to decode your data to something user friendly.
struct UserData: Codable {
let userId: String
let profile: String
}
Here's how to decode your data:
if let httpResponse = response as? HTTPURLResponse {
guard let data = data else { return }
let decoder = JSONDecoder()
do {
let userData = try decoder.decode(UserData.self, from: data)
print(userData.profile)
} catch {
print("Error: \(error)")
}
}
Related Topics
Cmake: Don't Set Rpath for a Single Library Used in Link
Difference Between Starting a Command Using Init.D Script and Service Start
How to Start a Process in Its Own Process Group
Ubuntu "E: Unable to Locate Package MySQL "
Simulate Microphone (Virtual Mic)
Linux/Libusb Get Usb Device Path
Ack & Negative Lookahead Giving Errors
Error Hh604: Error Running JSON-Rpc Server: Error:0308010C:Digital Envelope Routines::Unsupported
Do Here-Strings Undergo Word-Splitting
How to Get a Faster Output Pipe Than /Dev/Null
Gunicorn Does Not Start After Boot
Remote Linux Server to Remote Linux Server Large Sparse Files Copy - How To
What Happens If There Are Too Many Files Under a Single Directory in Linux
Curl Http Post File Upload Using Curl -Data in Linux Command Line