I want to write my own logging function using os_log
(available for iOS >= 10.0) and NSLog
.
I already wrote this code:
static func LogDebug(log: StaticString) {
if #available(iOS 10.0, *) {
os_log(log, type: .debug)
} else {
NSLog(log)
}
}
But now I get the exception that StaticString can not be converted to normal String. Do you know how to solve that?
The first argument of both os_log()
and NSLog()
is a format string, and contains
format specifiers (starting with %
) which are expanded by the following variable argument list.
To log an arbitrary string, use the %@
format, followed by the
string. Otherwise it can crash or produce wrong output if the string contains %
characters:
func LogDebug(log: String) {
if #available(iOS 10.0, macOS 10.12, *) {
os_log("%@", type: .debug, log)
} else {
NSLog("%@", log)
}
}