Commit b1d774af by Demid Merzlyakov

Added verbose logging (turned off by default). TODO: control via settings.

parent 4e2fac11
...@@ -10,7 +10,10 @@ import Foundation ...@@ -10,7 +10,10 @@ import Foundation
final class Logger { final class Logger {
enum LogLevel { public var minLogLevel: LogLevel
public enum LogLevel: Int {
case verbose = 0
case debug case debug
case info case info
case warning case warning
...@@ -21,14 +24,18 @@ final class Logger { ...@@ -21,14 +24,18 @@ final class Logger {
var componentName: String var componentName: String
init(componentName: String) { init(componentName: String, minLogLevel: LogLevel = .debug) {
self.componentName = componentName self.componentName = componentName
self.minLogLevel = minLogLevel
} }
private let maxCharsPerMessage = 900 // The Apple limit for NSLog is 1024. Leaving some space for timestamp, etc. Not sure if they are a part of 1024, but I think they are. Also, account for our own prefix (roughly) private let maxCharsPerMessage = 900 // The Apple limit for NSLog is 1024. Leaving some space for timestamp, etc. Not sure if they are a part of 1024, but I think they are. Also, account for our own prefix (roughly)
func log(level: LogLevel, message: String) { func log(level: LogLevel, message: String) {
#if DEBUG #if DEBUG
guard level.rawValue >= minLogLevel.rawValue else {
return
}
let prefix = "\(Logger.prefix) \(componentName) [\(logLevelString(level: level))]" let prefix = "\(Logger.prefix) \(componentName) [\(logLevelString(level: level))]"
if message.count < maxCharsPerMessage { if message.count < maxCharsPerMessage {
NSLog("\(prefix): \(message)") NSLog("\(prefix): \(message)")
...@@ -47,6 +54,10 @@ final class Logger { ...@@ -47,6 +54,10 @@ final class Logger {
#endif #endif
} }
func verbose(_ message: String) {
log(level: .verbose, message: message)
}
func debug(_ message: String) { func debug(_ message: String) {
log(level: .debug, message: message) log(level: .debug, message: message)
} }
...@@ -65,6 +76,8 @@ final class Logger { ...@@ -65,6 +76,8 @@ final class Logger {
private func logLevelString(level: LogLevel) -> String { private func logLevelString(level: LogLevel) -> String {
switch level { switch level {
case .verbose:
return "VERBOSE"
case .debug: case .debug:
return "DEBUG" return "DEBUG"
case .info: case .info:
......
...@@ -209,7 +209,7 @@ public class LocationManager { ...@@ -209,7 +209,7 @@ public class LocationManager {
public func updateHealth(for location: Location) { public func updateHealth(for location: Location) {
if let lastTimeUpdated = location.health?.lastUpdateTime { if let lastTimeUpdated = location.health?.lastUpdateTime {
guard Date().timeIntervalSince(lastTimeUpdated) >= healthSource.healthUpdateInterval else { guard Date().timeIntervalSince(lastTimeUpdated) >= healthSource.healthUpdateInterval else {
log.info("Update health (\(location)): fresh enough (last updated at \(lastTimeUpdated)), skip update.") log.verbose("Update health (\(location)): fresh enough (last updated at \(lastTimeUpdated)), skip update.")
return return
} }
} }
...@@ -243,7 +243,7 @@ public class LocationManager { ...@@ -243,7 +243,7 @@ public class LocationManager {
public func updateNotifications(for location: Location) { public func updateNotifications(for location: Location) {
if let lastTimeUpdated = location.notifications?.updatedAt { if let lastTimeUpdated = location.notifications?.updatedAt {
guard Date().timeIntervalSince(lastTimeUpdated) >= nwsAlertsManager.updateInterval else { guard Date().timeIntervalSince(lastTimeUpdated) >= nwsAlertsManager.updateInterval else {
log.info("Update notifications (\(location)): fresh enough (last updated at \(lastTimeUpdated), skip update") log.verbose("Update notifications (\(location)): fresh enough (last updated at \(lastTimeUpdated), skip update")
return return
} }
} }
...@@ -279,7 +279,7 @@ public class LocationManager { ...@@ -279,7 +279,7 @@ public class LocationManager {
} }
if let lastTimeUpdated = location.lastWeatherUpdateDate { if let lastTimeUpdated = location.lastWeatherUpdateDate {
guard Date().timeIntervalSince(lastTimeUpdated) >= weatherUpdateSource.weatherUpdateInterval else { guard Date().timeIntervalSince(lastTimeUpdated) >= weatherUpdateSource.weatherUpdateInterval else {
log.info("Update weather (\(location)): fresh enough (last updated at \(lastTimeUpdated)), skip update.") log.verbose("Update weather (\(location)): fresh enough (last updated at \(lastTimeUpdated)), skip update.")
return return
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment