-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked CAPLog to prevent race condition between it and CAPConfig.
- Loading branch information
Showing
1 changed file
with
18 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |