Attempt to see logs properly

This commit is contained in:
Christoph Hagen 2023-12-08 19:54:51 +01:00
parent 160c9a1a97
commit 4489092a6f
4 changed files with 32 additions and 15 deletions

View File

@ -39,20 +39,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 {
print("No configuration file found at \(url.path)") printAndFlush("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)") printAndFlush("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)") printAndFlush("Failed to decode config data: \(error)")
throw error throw error
} }
} }

View File

@ -108,7 +108,7 @@ final class DeviceManager {
private func resumeDeviceRequest(with data: Data) { private func resumeDeviceRequest(with data: Data) {
guard let receivedMessageData else { guard let receivedMessageData else {
print("[WARN] Received \(data.count) bytes after message completion") printAndFlush("[WARN] Received \(data.count) bytes after message completion")
self.requestInProgress = nil self.requestInProgress = nil
return return
} }
@ -120,12 +120,12 @@ final class DeviceManager {
} }
self.receivedMessageData = nil self.receivedMessageData = nil
guard let requestInProgress else { guard let requestInProgress else {
print("[WARN] Received \(newData.count) bytes, but no continuation to resume") printAndFlush("[WARN] Received \(newData.count) bytes, but no continuation to resume")
return return
} }
self.requestInProgress = nil self.requestInProgress = nil
guard newData.count == SignedMessage.size else { guard newData.count == SignedMessage.size else {
print("[WARN] Received \(newData.count) bytes, expected \(SignedMessage.size) for a message.") printAndFlush("[WARN] Received \(newData.count) bytes, expected \(SignedMessage.size) for a message.")
requestInProgress.resume(throwing: MessageResult.invalidMessageSizeFromDevice) requestInProgress.resume(throwing: MessageResult.invalidMessageSizeFromDevice)
return return
} }
@ -134,13 +134,13 @@ final class DeviceManager {
private func resumeDeviceRequest(with result: MessageResult) { private func resumeDeviceRequest(with result: MessageResult) {
guard let receivedMessageData else { guard let receivedMessageData else {
print("[WARN] Result after message completed: \(result)") printAndFlush("[WARN] Result after message completed: \(result)")
self.requestInProgress = nil self.requestInProgress = nil
return return
} }
self.receivedMessageData = nil self.receivedMessageData = nil
guard let requestInProgress else { guard let requestInProgress else {
print("[WARN] Request in progress (\(receivedMessageData.count) bytes), but no continuation found for result: \(result)") printAndFlush("[WARN] Request in progress (\(receivedMessageData.count) bytes), but no continuation found for result: \(result)")
return return
} }
self.requestInProgress = nil self.requestInProgress = nil
@ -191,12 +191,12 @@ final class DeviceManager {
socket.pingInterval = .seconds(10) socket.pingInterval = .seconds(10)
socket.onText { [weak self] socket, text in socket.onText { [weak self] socket, text in
print("[WARN] Received text over socket: \(text)") printAndFlush("[WARN] Received text over socket: \(text)")
// Close connection to prevent spamming the log // Close connection to prevent spamming the log
try? await socket.close() try? await socket.close()
guard let self else { guard let self else {
print("[WARN] No reference to self to handle text over socket") printAndFlush("[WARN] No reference to self to handle text over socket")
return return
} }
self.didCloseDeviceSocket() self.didCloseDeviceSocket()
@ -204,7 +204,7 @@ final class DeviceManager {
socket.onBinary { [weak self] _, data in socket.onBinary { [weak self] _, data in
guard let self else { guard let self else {
print("[WARN] No reference to self to process binary data on socket") printAndFlush("[WARN] No reference to self to process binary data on socket")
return return
} }
self.processDeviceResponse(data) self.processDeviceResponse(data)
@ -212,7 +212,7 @@ final class DeviceManager {
socket.onClose.whenComplete { [weak self] _ in socket.onClose.whenComplete { [weak self] _ in
guard let self else { guard let self else {
print("[WARN] No reference to self to handle socket closing") printAndFlush("[WARN] No reference to self to handle socket closing")
return return
} }
self.didCloseDeviceSocket() self.didCloseDeviceSocket()

16
Sources/App/Print.swift Normal file
View File

@ -0,0 +1,16 @@
import Foundation
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
func printAndFlush(_ message: String) {
print(message)
flushStdout()
}
func flushStdout() {
fflush(stdout)
}

View File

@ -57,9 +57,9 @@ public func shutdown() async {
do { do {
try await asyncScheduler.shutdownGracefully() try await asyncScheduler.shutdownGracefully()
} catch { } catch {
print("Failed to shut down MultiThreadedEventLoopGroup: \(error)") printAndFlush("Failed to shut down MultiThreadedEventLoopGroup: \(error)")
} }
print("[\(df.string(from: Date()))] Server shutdown") printAndFlush("[\(df.string(from: Date()))] Server shutdown")
} }
private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) { private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {
@ -84,10 +84,11 @@ private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data)
func log(_ message: String) { func log(_ message: String) {
guard let observer = MetricObserver.standard else { guard let observer = MetricObserver.standard else {
print(message) printAndFlush(message)
return return
} }
asyncScheduler.schedule { asyncScheduler.schedule {
await observer.log(message) await observer.log(message)
flushStdout()
} }
} }