次の例を考えてみましょう。
_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)
}
}
_
OSLogWrapper
の新しいインスタンスを作成し、testWrapper()
を呼び出す場合
_let logger = OSLogWrapper()
logger.testWrapper()
_
Xcodeコンソールで次の出力を取得します。
_2018-06-19 18:21:08.327979-0400 WrapperWTF[50240:548958] WTF: 0.000000
_
私は考えられるすべてをチェックしました、そして、私はここで間違っていることの頭または尾を作ることができません。ドキュメントを調べても、役に立つものは何もありません。
助けてくれてありがとう!
上記のRob Mayoffの回答へのコメントで述べたように、os_signpost()
で同じ種類の問題を経験している人のために、ここで作成したラッパークラスを示します。
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, dso: UnsafeRawPointer = #dsohandle, idObject: AnyObject? = nil) {
if #available(iOS 12.0, *) {
signpost(.begin, dso: dso, name: name, idObject: idObject)
}
}
public final func begin(name: StaticString, dso: UnsafeRawPointer = #dsohandle, idObject: AnyObject? = nil, _ format: StaticString, _ arguments: CVarArg...) {
if #available(iOS 12.0, *) {
signpost(.begin, dso: dso, name: name, idObject: idObject, format, arguments)
}
}
public final func event(name: StaticString, dso: UnsafeRawPointer = #dsohandle, idObject: AnyObject? = nil) {
if #available(iOS 12.0, *) {
signpost(.event, dso: dso, name: name, idObject: idObject)
}
}
public final func event(name: StaticString, dso: UnsafeRawPointer = #dsohandle, idObject: AnyObject? = nil, _ format: StaticString, _ arguments: CVarArg...) {
if #available(iOS 12.0, *) {
signpost(.event, dso: dso, name: name, idObject: idObject, format, arguments)
}
}
public final func end(name: StaticString, dso: UnsafeRawPointer = #dsohandle, idObject: AnyObject? = nil) {
if #available(iOS 12.0, *) {
signpost(.end, dso: dso, name: name, idObject: idObject)
}
}
public final func end(name: StaticString, dso: UnsafeRawPointer = #dsohandle, idObject: AnyObject? = nil, _ format: StaticString, _ arguments: CVarArg...) {
if #available(iOS 12.0, *) {
signpost(.end, dso: dso, name: name, idObject: idObject, format, arguments)
}
}
@available(iOS 12.0, *)
private final func signpost(_ type: OSSignpostType, dso: UnsafeRawPointer = #dsohandle, name: StaticString, idObject: AnyObject? = nil) {
guard log.signpostsEnabled else { return }
let signpostID = getSignpostId(forObject: idObject)
os_signpost(type, dso: dso, log: log, name: name, signpostID: signpostID)
}
@available(iOS 12.0, *)
private final func signpost(
_ type: OSSignpostType,
dso: UnsafeRawPointer,
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/Darwin/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
}
}