Skip to content

Instantly share code, notes, and snippets.

@JosephDuffy
Last active June 4, 2020 15:48
Show Gist options
  • Save JosephDuffy/5347b10f6c2a585d41c32f78a02375b4 to your computer and use it in GitHub Desktop.
Save JosephDuffy/5347b10f6c2a585d41c32f78a02375b4 to your computer and use it in GitHub Desktop.
OSLog Variadic Arguments
// A version of main.swift that is closer to https://stackoverflow.com/questions/50937765/why-does-wrapping-os-log-cause-doubles-to-not-be-logged-correctly/50942917#50942917, a linked by https://twitter.com/caseyliss/status/1268555666412765184
import Foundation
import os.log
import os
class OSLogWrapper {
func logDefaultariadicArg(_ message: StaticString, _ args: CVarArg... ) {
os_log(message, type: .default, args)
}
func logDefaultMultiArg(_ message: StaticString, _ arg1: CVarArg) {
os_log(message, type: .default, arg1)
}
func logDefaultMultiArg(_ message: StaticString, _ arg1: CVarArg, _ arg2: CVarArg) {
os_log(message, type: .default, arg1, arg2)
}
func logDefaultMultiArg(_ message: StaticString, _ arg1: CVarArg, _ arg2: CVarArg, _ arg3: CVarArg) {
os_log(message, type: .default, arg1, arg2, arg3)
}
func testWrapper() {
logDefaultMultiArg("WTF: %f", 1.2345)
logDefaultMultiArg("WTF: %f, %@, %f", 1.2345, "testing", 6.789)
logDefaultMultiArg("WTF: %f, %@", 1.2345, 1.2345, "testing")
logDefaultariadicArg("WTF: %f", 1.2345)
}
}
OSLogWrapper().testWrapper()
import Foundation
import os.log
extension OSLog {
public private(set) static var tests = OSLog(category: "Tests")
private convenience init(category: String) {
self.init(subsystem: "uk.josephduffy.os_log_testing", category: category)
}
public func signpost(type: OSSignpostType, name: StaticString, signpostID: OSSignpostID) {
os_signpost(type, log: self, name: name, signpostID: signpostID)
}
public func signpost(type: OSSignpostType, name: StaticString, signpostID: OSSignpostID, format: StaticString, _ args: CVarArg...) {
signpost(type: type, name: name, signpostID: signpostID, message: format, args: args)
}
public func `default`(_ message: StaticString, _ args: CVarArg...) {
log(message, type: .default, args: args)
}
public func info(_ message: StaticString, _ args: CVarArg...) {
log(message, type: .info, args: args)
}
public func debug(_ message: StaticString, _ args: CVarArg...) {
log(message, type: .debug, args: args)
}
public func error(_ message: StaticString, _ args: CVarArg...) {
log(message, type: .error, args: args)
}
public func fault(_ message: StaticString, _ args: CVarArg...) {
log(message, type: .fault, args: args)
}
private func log(_ message: StaticString, type: OSLogType, args: [CVarArg]) {
switch args.count {
case 0:
os_log(message, log: self, type: type)
case 1:
os_log(message, log: self, type: type, args[0])
case 2:
os_log(message, log: self, type: type, args[0], args[1])
case 3:
os_log(message, log: self, type: type, args[0], args[1], args[2])
default:
assertionFailure("Too many arguments passed to log. Update this to support this many arguments.")
os_log(message, log: self, type: type, args[0], args[1], args[2])
}
}
private func signpost(type: OSSignpostType, name: StaticString, signpostID: OSSignpostID, message: StaticString, args: [CVarArg]) {
switch args.count {
case 0:
os_signpost(type, log: self, name: name, signpostID: signpostID)
case 1:
os_signpost(type, log: self, name: name, signpostID: signpostID, message, args[0])
case 2:
os_signpost(type, log: self, name: name, signpostID: signpostID, message, args[0] ,args[1])
case 3:
os_signpost(type, log: self, name: name, signpostID: signpostID, message, args[0], args[1], args[2])
default:
assertionFailure("Too many arguments passed to signpost. Update this to support this many arguments.")
os_signpost(type, log: self, name: name, signpostID: signpostID, message, args[0], args[1], args[2])
}
}
}
let signpostID = OSSignpostID(log: OSLog.tests)
OSLog.tests.signpost(type: .begin, name: "Test Event", signpostID: signpostID, format: "%f, %@, %f", 1.2345, "testing start", 6.789)
Thread.sleep(forTimeInterval: 5)
OSLog.tests.signpost(type: .end, name: "Test Event", signpostID: signpostID, format: "%f, %@, %f", 1.2345, "testing end", 6.789)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment