Prenons l'exemple suivant:
import Foundation
import os.log
class OSLogWrapper {
func logDefault(_ message: StaticString, _ args: CVarArg...) {
os_log(message, type: .default, args)
}
func testWrapper() {
logDefault("WTF: %f", 1.2345)
}
}
Si je crée une nouvelle instance de OSLogWrapper
et appelle testWrapper()
let logger = OSLogWrapper()
logger.testWrapper()
Je reçois la sortie suivante dans la console Xcode:
2018-06-19 18:21:08.327979-0400 WrapperWTF[50240:548958] WTF: 0.000000
J'ai vérifié tout ce à quoi je pouvais penser et je ne pouvais pas comprendre ce qui n'allait pas ici. Regarder à travers la documentation ne donne rien d'utile.
Merci pour l'aide!
Comme mentionné dans mon commentaire à la réponse de Rob Mayoff ci-dessus, pour toute personne confrontée au même type de problème avec os_signpost()
, voici un cours d'encapsulation que j'ai créé autour de ce problème:
import Foundation
import os
import _SwiftOSOverlayShims
public final class Signpost {
private final let log: OSLog
public init(log: OSLog) {
self.log = log
}
public final func begin(name: StaticString, idObject: AnyObject? = nil) {
if #available(iOS 12.0, *) {
signpost(.begin, name: name, idObject: idObject)
}
}
public final func begin(name: StaticString, idObject: AnyObject? = nil, _ format: StaticString, _ arguments: CVarArg...) {
if #available(iOS 12.0, *) {
signpost(.begin, name: name, idObject: idObject, format, arguments)
}
}
public final func event(name: StaticString, idObject: AnyObject? = nil) {
if #available(iOS 12.0, *) {
signpost(.event, name: name, idObject: idObject)
}
}
public final func event(name: StaticString, idObject: AnyObject? = nil, _ format: StaticString, _ arguments: CVarArg...) {
if #available(iOS 12.0, *) {
signpost(.event, name: name, idObject: idObject, format, arguments)
}
}
public final func end(name: StaticString, idObject: AnyObject? = nil) {
if #available(iOS 12.0, *) {
signpost(.end, name: name)
}
}
public final func end(name: StaticString, idObject: AnyObject? = nil, _ format: StaticString, _ arguments: CVarArg...) {
if #available(iOS 12.0, *) {
signpost(.end, name: name, idObject: idObject, format, arguments)
}
}
@available(iOS 12.0, *)
private final func signpost(_ type: OSSignpostType, name: StaticString, idObject: AnyObject? = nil) {
guard log.signpostsEnabled else { return }
let signpostID = getSignpostId(forObject: idObject)
os_signpost(type, log: log, name: name, signpostID: signpostID)
}
@available(iOS 12.0, *)
private final func signpost(
_ type: OSSignpostType,
dso: UnsafeRawPointer = #dsohandle,
name: StaticString,
idObject: AnyObject? = nil,
_ format: StaticString,
_ arguments: [CVarArg])
{
// This crazy mess is because [CVarArg] gets treated as a single CVarArg and repassing a CVarArg... actually passes a [CVarArg]
// This was copied from the publicly available Swift source code at https://github.com/Apple/Swift/blob/master/stdlib/public/SDK/os/os_signpost.Swift#L40
// THIS IS A HACK
guard log.signpostsEnabled else { return }
let signpostID = getSignpostId(forObject: idObject)
guard signpostID != .invalid && signpostID != .null else { return }
let ra = _Swift_os_log_return_address()
name.withUTF8Buffer { (nameBuf: UnsafeBufferPointer<UInt8>) in
// Since dladdr is in libc, it is safe to unsafeBitCast
// the cstring argument type.
nameBuf.baseAddress!.withMemoryRebound(to: CChar.self, capacity: nameBuf.count) { nameStr in
format.withUTF8Buffer { (formatBuf: UnsafeBufferPointer<UInt8>) in
// Since dladdr is in libc, it is safe to unsafeBitCast
// the cstring argument type.
formatBuf.baseAddress!.withMemoryRebound(to: CChar.self, capacity: formatBuf.count) { formatStr in
withVaList(arguments) { valist in
_Swift_os_signpost_with_format(dso, ra, log, type, nameStr, signpostID.rawValue, formatStr, valist)
}
}
}
}
}
}
@available(iOS 12.0, *)
private final func getSignpostId(forObject idObject: AnyObject?) -> OSSignpostID {
if let idObject = idObject {
return OSSignpostID(log: log, object: idObject)
}
return .exclusive
}
}