Improve logging
This commit is contained in:
parent
790662a1ec
commit
b3c58ce4c7
@ -8,7 +8,7 @@ let package = Package(
|
||||
],
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
|
||||
.package(url: "https://github.com/christophhagen/clairvoyant.git", from: "0.3.0"),
|
||||
.package(url: "https://github.com/christophhagen/clairvoyant.git", from: "0.4.0"),
|
||||
],
|
||||
targets: [
|
||||
.target(
|
||||
|
@ -54,13 +54,13 @@ struct DeviceResponse {
|
||||
the remaining bytes contain the message.
|
||||
- Parameter buffer: The buffer where the message bytes are stored
|
||||
*/
|
||||
init?(_ buffer: ByteBuffer) {
|
||||
init?(_ buffer: ByteBuffer, request: String) {
|
||||
guard let byte = buffer.getBytes(at: 0, length: 1) else {
|
||||
print("No bytes received from device")
|
||||
log("\(request): No bytes received from device")
|
||||
return nil
|
||||
}
|
||||
guard let event = MessageResult(rawValue: byte[0]) else {
|
||||
print("Unknown response \(byte[0]) received from device")
|
||||
log("\(request): Unknown response \(byte[0]) received from device")
|
||||
return nil
|
||||
}
|
||||
self.event = event
|
||||
|
@ -23,19 +23,20 @@ extension Config {
|
||||
|
||||
init(loadFrom url: URL) throws {
|
||||
guard FileManager.default.fileExists(atPath: url.path) else {
|
||||
log("No configuration file found at \(url.path)")
|
||||
fatalError("No configuration file found")
|
||||
}
|
||||
let data: Data
|
||||
do {
|
||||
data = try Data(contentsOf: url)
|
||||
} catch {
|
||||
print("Failed to read config data: \(error)")
|
||||
log("Failed to read config data: \(error)")
|
||||
throw error
|
||||
}
|
||||
do {
|
||||
self = try JSONDecoder().decode(Config.self, from: data)
|
||||
} catch {
|
||||
print("Failed to decode config data: \(error)")
|
||||
log("Failed to decode config data: \(error)")
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
@ -63,12 +63,12 @@ final class DeviceManager {
|
||||
func authenticateDevice(hash: String) {
|
||||
guard let key = Data(fromHexEncodedString: hash),
|
||||
SHA256.hash(data: key) == self.deviceKey else {
|
||||
print("Invalid device key")
|
||||
log("Invalid device key")
|
||||
_ = connection?.close()
|
||||
deviceIsAuthenticated = false
|
||||
return
|
||||
}
|
||||
print("Device authenticated")
|
||||
log("Device authenticated")
|
||||
deviceIsAuthenticated = true
|
||||
}
|
||||
|
||||
@ -82,7 +82,7 @@ final class DeviceManager {
|
||||
return
|
||||
}
|
||||
defer { requestInProgress = nil }
|
||||
promise.succeed(DeviceResponse(data) ?? .unexpectedSocketEvent)
|
||||
promise.succeed(DeviceResponse(data, request: RouteAPI.socket.rawValue) ?? .unexpectedSocketEvent)
|
||||
}
|
||||
|
||||
func didCloseDeviceSocket() {
|
||||
@ -91,11 +91,11 @@ final class DeviceManager {
|
||||
}
|
||||
deviceIsAuthenticated = false
|
||||
guard connection != nil else {
|
||||
print("Socket closed, but no connection anyway")
|
||||
log("Socket closed, but no connection anyway")
|
||||
return
|
||||
}
|
||||
connection = nil
|
||||
print("Socket closed")
|
||||
log("Socket closed")
|
||||
}
|
||||
|
||||
func removeDeviceConnection() {
|
||||
@ -105,14 +105,14 @@ final class DeviceManager {
|
||||
}
|
||||
try? socket.close().wait()
|
||||
connection = nil
|
||||
print("Removed device connection")
|
||||
log("Removed device connection")
|
||||
}
|
||||
|
||||
func createNewDeviceConnection(_ socket: WebSocket) {
|
||||
isOpeningNewConnection = true
|
||||
removeDeviceConnection()
|
||||
connection = socket
|
||||
print("Socket connected")
|
||||
log("Socket connected")
|
||||
isOpeningNewConnection = false
|
||||
}
|
||||
}
|
||||
|
@ -67,3 +67,8 @@ private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data)
|
||||
}
|
||||
return (deviceKey: authContent[0], remoteKey: authContent[1])
|
||||
}
|
||||
|
||||
func log(_ message: String) {
|
||||
MetricObserver.standard?.log(message)
|
||||
print(message)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user