ios – os_log wrapper crashed when using “%s”


I am trying to wrap os_log for logging in my iOS app like so:

import Foundation
import OSLog

enum LogCategory: String, CaseIterable {
    case viewCycle
    case tracking
    case api
}

struct Log {
    
    private static let logs = {
        return LogCategory.allCases
            .reduce(into: [LogCategory: OSLog]()) { dict, category in
                dict = OSLog(subsystem: Bundle.main.bundleIdentifier ?? "BIMB", category: category.rawValue)
            }
    }()

    static func debug(category: LogCategory, message: StaticString, _ args: CVarArg...) {
        logImpl(category: category, message: message, type: .debug, args)
    }

    static func info(category: LogCategory, message: StaticString, _ args: CVarArg...) {
        logImpl(category: category, message: message, type: .info, args)
    }
    
    static func notice(category: LogCategory, message: StaticString, _ args: CVarArg...) {
        logImpl(category: category, message: message, type: .default, args)
    }
    
    static func warning(category: LogCategory, message: StaticString, _ args: CVarArg...) {
        logImpl(category: category, message: message, type: .default, args)
    }
    
    static func error(category: LogCategory, message: StaticString, _ args: CVarArg...) {
        logImpl(category: category, message: message, type: .error, args)
    }
    
    static func critical(category: LogCategory, message: StaticString, _ args: CVarArg...) {
        logImpl(category: category, message: message, type: .fault, args)
    }
    
    private static func logImpl(category: LogCategory, message: StaticString, type: OSLogType, _ args: CVarArg...) {
        guard let log = logs else {
            return
        }
        os_log(message, log: log, type: type, args)
    }
}

The problem is if I did this:

Log.debug(category: .tracking, 
          message: "Device ID: %s.", 
          UIDevice.current.identifierForVendor?.uuidString ?? "unknown")

it always crashed with this error:

2023-12-13 12:33:35.173798+0700 bimb-authenticate-ios[62740:928633]
*** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[Swift.__SwiftDeferredNSArray
UTF8String]: unrecognized selector sent to instance 0x600000dcbbc0’

But if I just do it with os_log like this:

os_log("Device ID: %s.",
       log: OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "tracking"),
       type: .debug,
       UIDevice.current.identifierForVendor?.uuidString ?? "unknown")

it worked fine. Can you tell me what’s wrong here?

Thanks.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img