From b3c58ce4c7067ba4be534a4ac2c47a1bd20bca82 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 6 Feb 2023 21:44:56 +0100 Subject: [PATCH] Improve logging --- Package.swift | 2 +- Sources/App/API/DeviceResponse.swift | 6 +++--- Sources/App/Config.swift | 5 +++-- Sources/App/DeviceManager.swift | 14 +++++++------- Sources/App/configure.swift | 5 +++++ 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Package.swift b/Package.swift index 6e91515..efe709f 100644 --- a/Package.swift +++ b/Package.swift @@ -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( diff --git a/Sources/App/API/DeviceResponse.swift b/Sources/App/API/DeviceResponse.swift index b5157bc..1673378 100644 --- a/Sources/App/API/DeviceResponse.swift +++ b/Sources/App/API/DeviceResponse.swift @@ -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 diff --git a/Sources/App/Config.swift b/Sources/App/Config.swift index 10ce33e..13eb2c6 100644 --- a/Sources/App/Config.swift +++ b/Sources/App/Config.swift @@ -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 } } diff --git a/Sources/App/DeviceManager.swift b/Sources/App/DeviceManager.swift index 91d5492..fab03ad 100644 --- a/Sources/App/DeviceManager.swift +++ b/Sources/App/DeviceManager.swift @@ -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 } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index a8595d6..147515e 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -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) +}