From 77a214d2a20010a94aefcec6e86606c304b91304 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 6 Feb 2023 22:03:02 +0100 Subject: [PATCH] Improve logging --- Package.swift | 2 +- Sources/App/Management/Configuration.swift | 4 ++-- Sources/App/Management/SQLiteDatabase.swift | 6 +++--- Sources/App/Management/TableManagement.swift | 10 +++++----- Sources/App/Model/Players/Player.swift | 4 ++-- Sources/App/Model/Tables/BiddingTable.swift | 12 ++++++------ Sources/App/Model/Tables/DealingTable.swift | 2 +- Sources/App/Model/Tables/FinishedTable.swift | 4 ++-- Sources/App/Model/Tables/PlayingTable.swift | 10 +++++----- Sources/App/Model/Tables/WaitingTable.swift | 4 ++-- Sources/App/Model/Tables/WeddingTable.swift | 11 ++++++----- Sources/App/configure.swift | 9 +++++++-- 12 files changed, 42 insertions(+), 36 deletions(-) diff --git a/Package.swift b/Package.swift index 8bc3ee9..3c49f29 100644 --- a/Package.swift +++ b/Package.swift @@ -15,7 +15,7 @@ let package = Package( // https://github.com/Joannis/SMTPKitten <- No updates since 0ct 2020 // https://github.com/Joannis/VaporSMTPKit <- No updates since 0ct. 2020, uses SMTPKitten .package(url: "https://github.com/Kitura/Swift-SMTP", from: "6.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/Management/Configuration.swift b/Sources/App/Management/Configuration.swift index f930eb9..a568acb 100644 --- a/Sources/App/Management/Configuration.swift +++ b/Sources/App/Management/Configuration.swift @@ -42,7 +42,7 @@ extension Configuration { do { data = try Data(contentsOf: url) } catch { - print("Failed to load configuration data from \(url.path): \(error)") + log("Failed to load configuration data from \(url.path): \(error)") throw error } try self.init(data: data) @@ -52,7 +52,7 @@ extension Configuration { do { self = try JSONDecoder().decode(Configuration.self, from: data) } catch { - print("Failed to decode configuration: \(error)") + log("Failed to decode configuration: \(error)") throw error } } diff --git a/Sources/App/Management/SQLiteDatabase.swift b/Sources/App/Management/SQLiteDatabase.swift index de5513b..e70c382 100644 --- a/Sources/App/Management/SQLiteDatabase.swift +++ b/Sources/App/Management/SQLiteDatabase.swift @@ -94,7 +94,7 @@ final class SQLiteDatabase { let count = try await User.query(on: database).count() registeredPlayerCountMetric.update(count) } catch { - print("Failed to update player count metric: \(error)") + log("Failed to update player count metric: \(error)") } } @@ -125,7 +125,7 @@ final class SQLiteDatabase { private func sendEmail(name: PlayerName, email: String, token: String) { guard let mailData = mail else { - print("Recovery email not set up") + log("Recovery email not set up") return } let recipient = Mail.User(name: name, email: email) @@ -154,7 +154,7 @@ final class SQLiteDatabase { mailData.smtp.send(mail) { (error) in if let error = error { - print("Failed to send recovery email to \(email): \(error)") + log("Failed to send recovery email to \(email): \(error)") } } } diff --git a/Sources/App/Management/TableManagement.swift b/Sources/App/Management/TableManagement.swift index 50cd4e7..2248d9c 100644 --- a/Sources/App/Management/TableManagement.swift +++ b/Sources/App/Management/TableManagement.swift @@ -45,7 +45,7 @@ final class TableManagement { do { try await loadTables(from: database) } catch { - print("Failed to load tables: \(error)") + log("Failed to load tables: \(error)") } } } @@ -59,7 +59,7 @@ final class TableManagement { let id = table.id! self.tables[id] = WaitingTable(id: id, name: table.name, isPublic: table.isPublic, players: table.players) } - print("\(tables.count) tables loaded") + log("\(tables.count) tables loaded") logTableCount() logPlayingPlayerCount() logConnectedPlayerCount() @@ -191,7 +191,7 @@ final class TableManagement { func performAction(player: PlayerName, action: PlayerAction) -> PlayerActionResult { guard let table = currentTable(for: player) else { - print("Player \(player) wants to \(action.id), but no table joined") + log("Player \(player) wants to \(action.id), but no table joined") return .noTableJoined } let (result, newTable) = table.perform(action: action, forPlayer: player) @@ -212,7 +212,7 @@ final class TableManagement { func select(game: GameType, player: PlayerName) -> PlayerActionResult { guard let aTable = currentTable(for: player) else { - print("Player \(player) wants to play \(game.rawValue), but no table joined") + log("Player \(player) wants to play \(game.rawValue), but no table joined") return .noTableJoined } guard let table = aTable as? BiddingTable else { @@ -223,7 +223,7 @@ final class TableManagement { return result } guard let newTable = newTable else { - print("Game selected by \(player), but no playing table \(table.name) created") + log("Game selected by \(player), but no playing table \(table.name) created") table.sendUpdateToAllPlayers() return result } diff --git a/Sources/App/Model/Players/Player.swift b/Sources/App/Model/Players/Player.swift index cef335e..640203c 100644 --- a/Sources/App/Model/Players/Player.swift +++ b/Sources/App/Model/Players/Player.swift @@ -79,7 +79,7 @@ extension Player { return false } socket.close().whenFailure { [weak self] error in - print("Failed to close socket for player: \(self?.name ?? ""): \(error)") + log("Failed to close socket for player: \(self?.name ?? ""): \(error)") } self.socket = nil return true @@ -94,7 +94,7 @@ extension Player { do { try socket.send(encodeJSON(info)) } catch { - print("Failed to send info: \(error)") + log("Failed to send info: \(error)") return false } return true diff --git a/Sources/App/Model/Tables/BiddingTable.swift b/Sources/App/Model/Tables/BiddingTable.swift index 1a224c4..560b034 100644 --- a/Sources/App/Model/Tables/BiddingTable.swift +++ b/Sources/App/Model/Tables/BiddingTable.swift @@ -41,19 +41,19 @@ final class BiddingTable: AbstractTable { func select(game: GameType, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { guard let player = players.player(named: name) else { - print("Player \(name) unexpectedly missing from bidding table \(self.name)") + log("Player \(name) unexpectedly missing from bidding table \(self.name)") return (.tableStateInvalid, nil) } guard player.selectsGame else { - print("Player \(name) does not select the game") + log("Player \(name) does not select the game") return (.tableStateInvalid, nil) } guard gameToOutbid.allows(game: game) else { - print("Game \(game) not allowed for class \(gameToOutbid)") + log("Game \(game) not allowed for class \(gameToOutbid)") return (.tableStateInvalid, nil) } guard player.canPlay(game: game) else { - print("Player \(game) can't play game \(game)") + log("Player \(game) can't play game \(game)") return (.tableStateInvalid, nil) } @@ -64,7 +64,7 @@ final class BiddingTable: AbstractTable { @discardableResult private func selectNextBidder() -> Bool { guard let index = players.firstIndex(where: { $0.isNextActor }) else { - print("Bidding: No current actor found to select next bidder") + log("Bidding: No current actor found to select next bidder") return false } players[index].isNextActor = false @@ -75,7 +75,7 @@ final class BiddingTable: AbstractTable { override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { guard let player = players.player(named: name) else { - print("Player \(name) unexpectedly missing from bidding table \(self.name)") + log("Player \(name) unexpectedly missing from bidding table \(self.name)") return (.tableStateInvalid, nil) } guard player.canPerform(action) else { diff --git a/Sources/App/Model/Tables/DealingTable.swift b/Sources/App/Model/Tables/DealingTable.swift index c3c12a3..36d017b 100644 --- a/Sources/App/Model/Tables/DealingTable.swift +++ b/Sources/App/Model/Tables/DealingTable.swift @@ -23,7 +23,7 @@ final class DealingTable: AbstractTable { override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { guard let player = players.player(named: name) else { - print("Player \(name) unexpectedly missing from dealing table \(self.name)") + log("Player \(name) unexpectedly missing from dealing table \(self.name)") return (.tableStateInvalid, nil) } guard player.canPerform(action) else { diff --git a/Sources/App/Model/Tables/FinishedTable.swift b/Sources/App/Model/Tables/FinishedTable.swift index 1cddfce..912133d 100644 --- a/Sources/App/Model/Tables/FinishedTable.swift +++ b/Sources/App/Model/Tables/FinishedTable.swift @@ -116,12 +116,12 @@ final class FinishedTable: AbstractTable { return (.tableStateInvalid, nil) } guard let player = players.player(named: name) else { - print("Unexpectedly missing player \(name) for deal action at finished table \(self.name)") + log("Unexpectedly missing player \(name) for deal action at finished table \(self.name)") return (.tableStateInvalid, nil) } guard player.canPerform(.deal) else { - print("Finished table: Player \(name) can't perform deal") + log("Finished table: Player \(name) can't perform deal") return (.tableStateInvalid, nil) } let waiting = WaitingTable(oldTableAdvancedByOne: self) diff --git a/Sources/App/Model/Tables/PlayingTable.swift b/Sources/App/Model/Tables/PlayingTable.swift index 9dcbeda..c5c3490 100644 --- a/Sources/App/Model/Tables/PlayingTable.swift +++ b/Sources/App/Model/Tables/PlayingTable.swift @@ -77,15 +77,15 @@ final class PlayingTable: AbstractTable { override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { guard let player = players.player(named: name) else { - print("Player \(name) unexpectedly missing from playing table \(self.name)") + log("Player \(name) unexpectedly missing from playing table \(self.name)") return (.tableStateInvalid, nil) } guard action == .doubleDuringGame else { - print("Player \(name) wants to perform action \(action) on playing table") + log("Player \(name) wants to perform action \(action) on playing table") return (.tableStateInvalid, nil) } guard player.canPerform(.doubleDuringGame) else { - print("Player \(name) is not allowed to raise") + log("Player \(name) is not allowed to raise") return (.tableStateInvalid, nil) } player.numberOfRaises += 1 @@ -96,11 +96,11 @@ final class PlayingTable: AbstractTable { override func play(card: Card, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { guard let player = players.player(named: name) else { - print("Player \(name) unexpectedly missing from playing table \(self.name)") + log("Player \(name) unexpectedly missing from playing table \(self.name)") return (.tableStateInvalid, nil) } guard player.isNextActor else { - print("Player \(name) wants to play card but is not active") + log("Player \(name) wants to play card but is not active") return (.tableStateInvalid, nil) } guard player.canPlay(card: card, for: nextTrick, in: game) else { diff --git a/Sources/App/Model/Tables/WaitingTable.swift b/Sources/App/Model/Tables/WaitingTable.swift index 338a194..cec2e0b 100644 --- a/Sources/App/Model/Tables/WaitingTable.swift +++ b/Sources/App/Model/Tables/WaitingTable.swift @@ -106,12 +106,12 @@ final class WaitingTable: AbstractTable { return (.tableStateInvalid, nil) } guard let player = players.player(named: name) else { - print("Unexpected action \(action) for missing player \(name) at table \(self.name)") + log("Unexpected action \(action) for missing player \(name) at table \(self.name)") return (.tableStateInvalid, nil) } guard player.canPerform(.deal) else { - print("Player \(name) cant perform deal, although table is full") + log("Player \(name) cant perform deal, although table is full") return (.tableStateInvalid, nil) } let table = DealingTable(table: self) diff --git a/Sources/App/Model/Tables/WeddingTable.swift b/Sources/App/Model/Tables/WeddingTable.swift index b5f9d81..41165ea 100644 --- a/Sources/App/Model/Tables/WeddingTable.swift +++ b/Sources/App/Model/Tables/WeddingTable.swift @@ -24,7 +24,7 @@ final class WeddingTable: AbstractTable { override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { guard let player = players.player(named: name) else { - print("Player \(name) unexpectedly missing from wedding table \(self.name)") + log("Player \(name) unexpectedly missing from wedding table \(self.name)") return (.tableStateInvalid, nil) } guard player.canPerform(action) else { @@ -37,6 +37,7 @@ final class WeddingTable: AbstractTable { case .withdrawFromAuction: return performWithdrawl(forPlayer: player) case .increaseOrMatchGame: + log("Unexpected action \(action) should not be possible") fatalError() default: return (.tableStateInvalid, nil) @@ -96,19 +97,19 @@ final class WeddingTable: AbstractTable { override func play(card: Card, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { guard requiresCardSelection else { - print("Wedding is not in stage where card should be selected") + log("Wedding is not in stage where card should be selected") return (.tableStateInvalid, nil) } guard let player = players.player(named: name) else { - print("Player \(name) unexpectedly missing from wedding table \(self.name)") + log("Player \(name) unexpectedly missing from wedding table \(self.name)") return (.tableStateInvalid, nil) } guard player.selectsGame else { - print("Player \(name) is not the one selecting a wedding card") + log("Player \(name) is not the one selecting a wedding card") return (.tableStateInvalid, nil) } guard player.canExchange(card: card) else { - print("Invalid card \(card) to exchange in wedding") + log("Invalid card \(card) to exchange in wedding") return (.tableStateInvalid, nil) } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 62dbd8a..a7e6126 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -46,12 +46,12 @@ public func configure(_ app: Application) throws { if !configuration.production { app.logger.logLevel = .info - print("[DEVELOPMENT] Using in-memory database") + log("[DEVELOPMENT] Using in-memory database") app.databases.use(.sqlite(.memory), as: .sqlite) } else { app.logger.logLevel = .notice let dbFile = storageFolder.appendingPathComponent("db.sqlite").path - print("[PRODUCTION] Using database at \(dbFile)") + log("[PRODUCTION] Using database at \(dbFile)") app.databases.use(.sqlite(.file(dbFile)), as: .sqlite) } app.migrations.add(UserTableMigration()) @@ -83,3 +83,8 @@ public func configure(_ app: Application) throws { status.update(.nominal) } + +func log(_ message: String) { + MetricObserver.standard?.log(message) + print(message) +}