Commit b1d774af by Demid Merzlyakov

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

parent 4e2fac11
......@@ -10,7 +10,10 @@ import Foundation
final class Logger {
enum LogLevel {
public var minLogLevel: LogLevel
public enum LogLevel: Int {
case verbose = 0
case debug
case info
case warning
......@@ -21,14 +24,18 @@ final class Logger {
var componentName: String
init(componentName: String) {
init(componentName: String, minLogLevel: LogLevel = .debug) {
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)
func log(level: LogLevel, message: String) {
#if DEBUG
guard level.rawValue >= minLogLevel.rawValue else {
return
}
let prefix = "\(Logger.prefix) \(componentName) [\(logLevelString(level: level))]"
if message.count < maxCharsPerMessage {
NSLog("\(prefix): \(message)")
......@@ -47,6 +54,10 @@ final class Logger {
#endif
}
func verbose(_ message: String) {
log(level: .verbose, message: message)
}
func debug(_ message: String) {
log(level: .debug, message: message)
}
......@@ -65,6 +76,8 @@ final class Logger {
private func logLevelString(level: LogLevel) -> String {
switch level {
case .verbose:
return "VERBOSE"
case .debug:
return "DEBUG"
case .info:
......
......@@ -209,7 +209,7 @@ public class LocationManager {
public func updateHealth(for location: Location) {
if let lastTimeUpdated = location.health?.lastUpdateTime {
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
}
}
......@@ -243,7 +243,7 @@ public class LocationManager {
public func updateNotifications(for location: Location) {
if let lastTimeUpdated = location.notifications?.updatedAt {
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
}
}
......@@ -279,7 +279,7 @@ public class LocationManager {
}
if let lastTimeUpdated = location.lastWeatherUpdateDate {
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
}
}
......
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