Attempt to see logs properly
This commit is contained in:
parent
160c9a1a97
commit
4489092a6f
@ -39,20 +39,20 @@ extension Config {
|
||||
|
||||
init(loadFrom url: URL) throws {
|
||||
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")
|
||||
}
|
||||
let data: Data
|
||||
do {
|
||||
data = try Data(contentsOf: url)
|
||||
} catch {
|
||||
print("Failed to read config data: \(error)")
|
||||
printAndFlush("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)")
|
||||
printAndFlush("Failed to decode config data: \(error)")
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ final class DeviceManager {
|
||||
|
||||
private func resumeDeviceRequest(with data: Data) {
|
||||
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
|
||||
return
|
||||
}
|
||||
@ -120,12 +120,12 @@ final class DeviceManager {
|
||||
}
|
||||
self.receivedMessageData = nil
|
||||
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
|
||||
}
|
||||
self.requestInProgress = nil
|
||||
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)
|
||||
return
|
||||
}
|
||||
@ -134,13 +134,13 @@ final class DeviceManager {
|
||||
|
||||
private func resumeDeviceRequest(with result: MessageResult) {
|
||||
guard let receivedMessageData else {
|
||||
print("[WARN] Result after message completed: \(result)")
|
||||
printAndFlush("[WARN] Result after message completed: \(result)")
|
||||
self.requestInProgress = nil
|
||||
return
|
||||
}
|
||||
self.receivedMessageData = nil
|
||||
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
|
||||
}
|
||||
self.requestInProgress = nil
|
||||
@ -191,12 +191,12 @@ final class DeviceManager {
|
||||
socket.pingInterval = .seconds(10)
|
||||
|
||||
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
|
||||
try? await socket.close()
|
||||
|
||||
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
|
||||
}
|
||||
self.didCloseDeviceSocket()
|
||||
@ -204,7 +204,7 @@ final class DeviceManager {
|
||||
|
||||
socket.onBinary { [weak self] _, data in
|
||||
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
|
||||
}
|
||||
self.processDeviceResponse(data)
|
||||
@ -212,7 +212,7 @@ final class DeviceManager {
|
||||
|
||||
socket.onClose.whenComplete { [weak self] _ in
|
||||
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
|
||||
}
|
||||
self.didCloseDeviceSocket()
|
||||
|
16
Sources/App/Print.swift
Normal file
16
Sources/App/Print.swift
Normal 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)
|
||||
}
|
@ -57,9 +57,9 @@ public func shutdown() async {
|
||||
do {
|
||||
try await asyncScheduler.shutdownGracefully()
|
||||
} 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) {
|
||||
@ -84,10 +84,11 @@ private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data)
|
||||
|
||||
func log(_ message: String) {
|
||||
guard let observer = MetricObserver.standard else {
|
||||
print(message)
|
||||
printAndFlush(message)
|
||||
return
|
||||
}
|
||||
asyncScheduler.schedule {
|
||||
await observer.log(message)
|
||||
flushStdout()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user