Logging: Use ringlogger for logging from the app

This commit is contained in:
Roopesh Chander 2018-12-13 17:30:02 +05:30
parent 8eb6fa67ef
commit 6ee3bd1ad2
3 changed files with 23 additions and 22 deletions

View File

@ -29,7 +29,7 @@ extension FileManager {
do { do {
try FileManager.default.removeItem(at: url) try FileManager.default.removeItem(at: url)
} catch let error { } catch let error {
os_log("Failed to delete file '%{public}@': %{public}@", log: OSLog.default, type: .debug, url.absoluteString, error.localizedDescription) wg_log(.info, message: "Failed to delete file '\(url.path)': \(error)")
return false return false
} }
return true return true

View File

@ -82,7 +82,7 @@ class TunnelsManager {
#else #else
NETunnelProviderManager.loadAllFromPreferences { managers, error in NETunnelProviderManager.loadAllFromPreferences { managers, error in
if let error = error { if let error = error {
os_log("Failed to load tunnel provider managers: %{public}@", log: OSLog.default, type: .debug, "\(error)") wg_log(.error, message: "Failed to load tunnel provider managers: \(error)")
completionHandler(.failure(TunnelsManagerError.systemErrorOnListingTunnels)) completionHandler(.failure(TunnelsManagerError.systemErrorOnListingTunnels))
return return
} }
@ -114,7 +114,7 @@ class TunnelsManager {
tunnelProviderManager.saveToPreferences { [weak self] error in tunnelProviderManager.saveToPreferences { [weak self] error in
guard error == nil else { guard error == nil else {
os_log("Add: Saving configuration failed: %{public}@", log: OSLog.default, type: .error, "\(error!)") wg_log(.error, message: "Add: Saving configuration failed: \(error!)")
completionHandler(.failure(TunnelsManagerError.systemErrorOnAddTunnel)) completionHandler(.failure(TunnelsManagerError.systemErrorOnAddTunnel))
return return
} }
@ -171,7 +171,7 @@ class TunnelsManager {
tunnelProviderManager.saveToPreferences { [weak self] error in tunnelProviderManager.saveToPreferences { [weak self] error in
guard error == nil else { guard error == nil else {
os_log("Modify: Saving configuration failed: %{public}@", log: OSLog.default, type: .error, "\(error!)") wg_log(.error, message: "Modify: Saving configuration failed: \(error!)")
completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel) completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel)
return return
} }
@ -196,7 +196,7 @@ class TunnelsManager {
tunnelProviderManager.loadFromPreferences { error in tunnelProviderManager.loadFromPreferences { error in
tunnel.isActivateOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled tunnel.isActivateOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled
guard error == nil else { guard error == nil else {
os_log("Modify: Re-loading after saving configuration failed: %{public}@", log: OSLog.default, type: .error, "\(error!)") wg_log(.error, message: "Modify: Re-loading after saving configuration failed: \(error!)")
completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel) completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel)
return return
} }
@ -214,7 +214,7 @@ class TunnelsManager {
tunnelProviderManager.removeFromPreferences { [weak self] error in tunnelProviderManager.removeFromPreferences { [weak self] error in
guard error == nil else { guard error == nil else {
os_log("Remove: Saving configuration failed: %{public}@", log: OSLog.default, type: .error, "\(error!)") wg_log(.error, message: "Remove: Saving configuration failed: \(error!)")
completionHandler(TunnelsManagerError.systemErrorOnRemoveTunnel) completionHandler(TunnelsManagerError.systemErrorOnRemoveTunnel)
return return
} }
@ -275,8 +275,7 @@ class TunnelsManager {
guard let tunnelProvider = session.manager as? NETunnelProviderManager else { return } guard let tunnelProvider = session.manager as? NETunnelProviderManager else { return }
guard let tunnel = self.tunnels.first(where: { $0.tunnelProvider == tunnelProvider }) else { return } guard let tunnel = self.tunnels.first(where: { $0.tunnelProvider == tunnelProvider }) else { return }
os_log("Tunnel '%{public}@' connection status changed to '%{public}@'", wg_log(.debug, message: "Tunnel '\(tunnel.name)' connection status changed to '\(tunnel.tunnelProvider.connection.status)'")
log: OSLog.default, type: .debug, tunnel.name, "\(tunnel.tunnelProvider.connection.status)")
// In case our attempt to start the tunnel, didn't succeed // In case our attempt to start the tunnel, didn't succeed
if tunnel == self.tunnelBeingActivated { if tunnel == self.tunnelBeingActivated {
@ -361,26 +360,26 @@ class TunnelContainer: NSObject {
tunnelConfiguration: TunnelConfiguration, tunnelConfiguration: TunnelConfiguration,
completionHandler: @escaping (TunnelsManagerError?) -> Void) { completionHandler: @escaping (TunnelsManagerError?) -> Void) {
if recursionCount >= 8 { if recursionCount >= 8 {
os_log("startActivation: Failed after 8 attempts. Giving up with %{public}@", log: OSLog.default, type: .error, "\(lastError!)") wg_log(.error, message: "startActivation: Failed after 8 attempts. Giving up with \(lastError!)")
completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed) completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed)
return return
} }
os_log("startActivation: Entering (tunnel: %{public}@)", log: OSLog.default, type: .debug, self.name) wg_log(.debug, message: "startActivation: Entering (tunnel: \(self.name))")
guard tunnelProvider.isEnabled else { guard tunnelProvider.isEnabled else {
// In case the tunnel had gotten disabled, re-enable and save it, // In case the tunnel had gotten disabled, re-enable and save it,
// then call this function again. // then call this function again.
os_log("startActivation: Tunnel is disabled. Re-enabling and saving", log: OSLog.default, type: .info) wg_log(.debug, staticMessage: "startActivation: Tunnel is disabled. Re-enabling and saving")
tunnelProvider.isEnabled = true tunnelProvider.isEnabled = true
tunnelProvider.saveToPreferences { [weak self] error in tunnelProvider.saveToPreferences { [weak self] error in
if error != nil { if error != nil {
os_log("Error saving tunnel after re-enabling: %{public}@", log: OSLog.default, type: .error, "\(error!)") wg_log(.error, message: "Error saving tunnel after re-enabling: \(error!)")
completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed) completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed)
return return
} }
os_log("startActivation: Tunnel saved after re-enabling", log: OSLog.default, type: .info) wg_log(.debug, staticMessage: "startActivation: Tunnel saved after re-enabling")
os_log("startActivation: Invoking startActivation", log: OSLog.default, type: .debug) wg_log(.debug, staticMessage: "startActivation: Invoking startActivation")
self?.startActivation(recursionCount: recursionCount + 1, lastError: NEVPNError(NEVPNError.configurationUnknown), self?.startActivation(recursionCount: recursionCount + 1, lastError: NEVPNError(NEVPNError.configurationUnknown),
tunnelConfiguration: tunnelConfiguration, completionHandler: completionHandler) tunnelConfiguration: tunnelConfiguration, completionHandler: completionHandler)
} }
@ -389,33 +388,33 @@ class TunnelContainer: NSObject {
// Start the tunnel // Start the tunnel
do { do {
os_log("startActivation: Starting tunnel", log: OSLog.default, type: .debug) wg_log(.debug, staticMessage: "startActivation: Starting tunnel")
try (tunnelProvider.connection as? NETunnelProviderSession)?.startTunnel() try (tunnelProvider.connection as? NETunnelProviderSession)?.startTunnel()
os_log("startActivation: Success", log: OSLog.default, type: .debug) wg_log(.debug, staticMessage: "startActivation: Success")
completionHandler(nil) completionHandler(nil)
} catch let error { } catch let error {
guard let systemError = error as? NEVPNError else { guard let systemError = error as? NEVPNError else {
os_log("Failed to activate tunnel: Error: %{public}@", log: OSLog.default, type: .debug, "\(error)") wg_log(.error, message: "Failed to activate tunnel: Error: \(error)")
status = .inactive status = .inactive
completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed) completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed)
return return
} }
guard systemError.code == NEVPNError.configurationInvalid || systemError.code == NEVPNError.configurationStale else { guard systemError.code == NEVPNError.configurationInvalid || systemError.code == NEVPNError.configurationStale else {
os_log("Failed to activate tunnel: VPN Error: %{public}@", log: OSLog.default, type: .debug, "\(error)") wg_log(.error, message: "Failed to activate tunnel: VPN Error: \(error)")
status = .inactive status = .inactive
completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed) completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed)
return return
} }
os_log("startActivation: Will reload tunnel and then try to start it. ", log: OSLog.default, type: .info) wg_log(.debug, staticMessage: "startActivation: Will reload tunnel and then try to start it.")
tunnelProvider.loadFromPreferences { [weak self] error in tunnelProvider.loadFromPreferences { [weak self] error in
if error != nil { if error != nil {
os_log("startActivation: Error reloading tunnel: %{public}@", log: OSLog.default, type: .debug, "\(error!)") wg_log(.error, message: "startActivation: Error reloading tunnel: \(error!)")
self?.status = .inactive self?.status = .inactive
completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed) completionHandler(TunnelsManagerError.tunnelActivationAttemptFailed)
return return
} }
os_log("startActivation: Tunnel reloaded", log: OSLog.default, type: .info) wg_log(.debug, staticMessage: "startActivation: Tunnel reloaded")
os_log("startActivation: Invoking startActivation", log: OSLog.default, type: .debug) wg_log(.debug, staticMessage: "startActivation: Invoking startActivation")
self?.startActivation(recursionCount: recursionCount + 1, lastError: systemError, tunnelConfiguration: tunnelConfiguration, completionHandler: completionHandler) self?.startActivation(recursionCount: recursionCount + 1, lastError: systemError, tunnelConfiguration: tunnelConfiguration, completionHandler: completionHandler)
} }
} }

View File

@ -21,6 +21,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
os_log("Can't obtain log file URL. Log is not saved to file.", log: OSLog.default, type: .error) os_log("Can't obtain log file URL. Log is not saved to file.", log: OSLog.default, type: .error)
} }
wg_log(.info, message: "Launching app")
let window = UIWindow(frame: UIScreen.main.bounds) let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = UIColor.white window.backgroundColor = UIColor.white
self.window = window self.window = window