Improve logging

This commit is contained in:
Christoph Hagen 2023-02-06 21:44:56 +01:00
parent 790662a1ec
commit b3c58ce4c7
5 changed files with 19 additions and 13 deletions

View File

@ -8,7 +8,7 @@ let package = Package(
], ],
dependencies: [ dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"), .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: [ targets: [
.target( .target(

View File

@ -54,13 +54,13 @@ struct DeviceResponse {
the remaining bytes contain the message. the remaining bytes contain the message.
- Parameter buffer: The buffer where the message bytes are stored - 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 { 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 return nil
} }
guard let event = MessageResult(rawValue: byte[0]) else { 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 return nil
} }
self.event = event self.event = event

View File

@ -23,19 +23,20 @@ extension Config {
init(loadFrom url: URL) throws { init(loadFrom url: URL) throws {
guard FileManager.default.fileExists(atPath: url.path) else { guard FileManager.default.fileExists(atPath: url.path) else {
log("No configuration file found at \(url.path)")
fatalError("No configuration file found") fatalError("No configuration file found")
} }
let data: Data let data: Data
do { do {
data = try Data(contentsOf: url) data = try Data(contentsOf: url)
} catch { } catch {
print("Failed to read config data: \(error)") log("Failed to read config data: \(error)")
throw error throw error
} }
do { do {
self = try JSONDecoder().decode(Config.self, from: data) self = try JSONDecoder().decode(Config.self, from: data)
} catch { } catch {
print("Failed to decode config data: \(error)") log("Failed to decode config data: \(error)")
throw error throw error
} }
} }

View File

@ -63,12 +63,12 @@ final class DeviceManager {
func authenticateDevice(hash: String) { func authenticateDevice(hash: String) {
guard let key = Data(fromHexEncodedString: hash), guard let key = Data(fromHexEncodedString: hash),
SHA256.hash(data: key) == self.deviceKey else { SHA256.hash(data: key) == self.deviceKey else {
print("Invalid device key") log("Invalid device key")
_ = connection?.close() _ = connection?.close()
deviceIsAuthenticated = false deviceIsAuthenticated = false
return return
} }
print("Device authenticated") log("Device authenticated")
deviceIsAuthenticated = true deviceIsAuthenticated = true
} }
@ -82,7 +82,7 @@ final class DeviceManager {
return return
} }
defer { requestInProgress = nil } defer { requestInProgress = nil }
promise.succeed(DeviceResponse(data) ?? .unexpectedSocketEvent) promise.succeed(DeviceResponse(data, request: RouteAPI.socket.rawValue) ?? .unexpectedSocketEvent)
} }
func didCloseDeviceSocket() { func didCloseDeviceSocket() {
@ -91,11 +91,11 @@ final class DeviceManager {
} }
deviceIsAuthenticated = false deviceIsAuthenticated = false
guard connection != nil else { guard connection != nil else {
print("Socket closed, but no connection anyway") log("Socket closed, but no connection anyway")
return return
} }
connection = nil connection = nil
print("Socket closed") log("Socket closed")
} }
func removeDeviceConnection() { func removeDeviceConnection() {
@ -105,14 +105,14 @@ final class DeviceManager {
} }
try? socket.close().wait() try? socket.close().wait()
connection = nil connection = nil
print("Removed device connection") log("Removed device connection")
} }
func createNewDeviceConnection(_ socket: WebSocket) { func createNewDeviceConnection(_ socket: WebSocket) {
isOpeningNewConnection = true isOpeningNewConnection = true
removeDeviceConnection() removeDeviceConnection()
connection = socket connection = socket
print("Socket connected") log("Socket connected")
isOpeningNewConnection = false isOpeningNewConnection = false
} }
} }

View File

@ -67,3 +67,8 @@ private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data)
} }
return (deviceKey: authContent[0], remoteKey: authContent[1]) return (deviceKey: authContent[0], remoteKey: authContent[1])
} }
func log(_ message: String) {
MetricObserver.standard?.log(message)
print(message)
}