89abb3bf03
* Implement Logging * Add timestamp in OnValueChanged * improve web and linux logging * Implement windows and format pigeon * Fix windows build and unity log level * Log commands with timestamp * Fix code doc * Remove unnecessary import * Update docs * Fix crash on windows 11 because of failed pairing * Update changelog * Fix native code formatting
54 lines
1.4 KiB
Swift
54 lines
1.4 KiB
Swift
import Foundation
|
|
|
|
final class UniversalBleLogger {
|
|
static let shared = UniversalBleLogger()
|
|
|
|
private init() {}
|
|
|
|
private lazy var dateFormatter: DateFormatter = {
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "HH:mm:ss.SSS"
|
|
return formatter
|
|
}()
|
|
|
|
private var currentLogLevel: UniversalBleLogLevel = .none
|
|
|
|
func setLogLevel(_ logLevel: UniversalBleLogLevel) {
|
|
currentLogLevel = logLevel
|
|
}
|
|
|
|
func logError(_ message: String) {
|
|
guard allows(.error) else { return }
|
|
print("UniversalBle:ERROR \(withTimestamp(message))")
|
|
}
|
|
|
|
func logWarning(_ message: String) {
|
|
guard allows(.warning) else { return }
|
|
print("UniversalBle:WARN \(withTimestamp(message))")
|
|
}
|
|
|
|
func logInfo(_ message: String) {
|
|
guard allows(.info) else { return }
|
|
print("UniversalBle:INFO \(withTimestamp(message))")
|
|
}
|
|
|
|
func logDebug(_ message: String) {
|
|
guard allows(.debug) else { return }
|
|
print("UniversalBle:DEBUG \(withTimestamp(message))")
|
|
}
|
|
|
|
func logVerbose(_ message: String) {
|
|
guard allows(.verbose) else { return }
|
|
print("UniversalBle:VERBOSE \(withTimestamp(message))")
|
|
}
|
|
|
|
private func allows(_ level: UniversalBleLogLevel) -> Bool {
|
|
return currentLogLevel != .none && level.rawValue <= currentLogLevel.rawValue
|
|
}
|
|
|
|
private func withTimestamp(_ message: String) -> String {
|
|
let time = dateFormatter.string(from: Date())
|
|
return "[\(time)] \(message)"
|
|
}
|
|
}
|