Skip to content

Commit

Permalink
Reworked CAPLog to prevent race condition between it and CAPConfig.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikeith committed Oct 16, 2020
1 parent d15ab9f commit 28f1eed
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions ios/Capacitor/Capacitor/CAPLog.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
public class CAPLog {

public static let config = CAPConfig()

public static var enableLogging: Bool = true

private static var oneTimeConfigCheck: () -> () = {
// `dispatch_once` is not available since Swift 3. but, since static properties are implicitly lazy,
// this code will only execute once which is sufficient for our needs here. but since we need to do an
// async dispatch, there is a window of time where the default value will be valid before the config
// value(s) can be loaded.
DispatchQueue.main.async {
let config = CAPConfig()
if let configFlag = (config.getValue("ios.hideLogs") as? Bool) ?? (config.getValue("hideLogs") as? Bool) {
enableLogging = !configFlag
}
}
return {}
}()

public static func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
if !self.hideLogs() {
oneTimeConfigCheck()
if enableLogging {
for (itemIndex, item) in items.enumerated() {
Swift.print(item, terminator: itemIndex == items.count - 1 ? terminator : separator)
}
}
}

public static func hideLogs() -> Bool {
if let hideLogs = (config.getValue("ios.hideLogs") as? Bool) ?? (config.getValue("hideLogs") as? Bool) {
return hideLogs
}
return false
}
}

0 comments on commit 28f1eed

Please sign in to comment.