My iOS app has a function that logs a whole load of statistics about a particular object. Since this is only for dev purposes I don't want any of the actual statistics gathering to be run if debug logs are not enabled so I am checking OSLog.default.isEnabled(type: .debug) before doing anything. Here is the basic shell of my code:
When "Show Debug Info" is enabled in Console, this is what I get (as expected):
So this has the effect that I go through all the expense of calculating the statistics when I shouldn't.
(BTW - I added the NSLog calls to help me see what was really happening. I don't intend to keep them in there)
I don't think I am doing anything wrong in my code.
I'm running macOS 10.15.7 alongside two iPads, one with iOS 14.3 and one with 14.4. Both iPads are connected via USB
Code Block swift func dumpStatistics(for item: Any) { func callSomeVerySlowFunc(_ object: Any) -> String { /* Something much more complicated than this */ return "Hello" } guard OSLog.default.isEnabled(type: .debug) else { NSLog("Not dumping Statistics, debug logging is disabled") return } NSLog("(NSLog) dumping Statistics, debug logging is supposedly enabled") os_log(.debug, "(os_log)dumping Statistics, debug logging is supposedly enabled") let stats: String = callSomeVerySlowFunc(item) NSLog("(NSLog) dumping Statistics: %@", stats) os_log(.debug, "(os_log) dumping Statistics: %{public}@", stats) }
When "Show Debug Info" is enabled in Console, this is what I get (as expected):
(NSLog) dumping Statistics, debug logging is supposedly enabled
(os_log)dumping Statistics, debug logging is supposedly enabled
(NSLog) dumping Statistics: Hello
When "Show Debug Info" is disabled, this is what I expect to get:(os_log) dumping Statistics: Hello
However, this is what I actually do get:Not dumping Statistics, debug logging is disabled
(NSLog) dumping Statistics, debug logging is supposedly enabled
This implies that OSLog.default.isEnabled(type: .debug) is always returning true, but the actual logging code itself knows it's not really enabled.(NSLog) dumping Statistics: Hello
So this has the effect that I go through all the expense of calculating the statistics when I shouldn't.
(BTW - I added the NSLog calls to help me see what was really happening. I don't intend to keep them in there)
I don't think I am doing anything wrong in my code.
I'm running macOS 10.15.7 alongside two iPads, one with iOS 14.3 and one with 14.4. Both iPads are connected via USB