Improve logging

This commit is contained in:
Christoph Hagen 2023-02-06 22:03:02 +01:00
parent a48c77c138
commit 77a214d2a2
12 changed files with 42 additions and 36 deletions

View File

@ -15,7 +15,7 @@ let package = Package(
// https://github.com/Joannis/SMTPKitten <- No updates since 0ct 2020 // https://github.com/Joannis/SMTPKitten <- No updates since 0ct 2020
// https://github.com/Joannis/VaporSMTPKit <- No updates since 0ct. 2020, uses SMTPKitten // 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/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: [ targets: [
.target( .target(

View File

@ -42,7 +42,7 @@ extension Configuration {
do { do {
data = try Data(contentsOf: url) data = try Data(contentsOf: url)
} catch { } catch {
print("Failed to load configuration data from \(url.path): \(error)") log("Failed to load configuration data from \(url.path): \(error)")
throw error throw error
} }
try self.init(data: data) try self.init(data: data)
@ -52,7 +52,7 @@ extension Configuration {
do { do {
self = try JSONDecoder().decode(Configuration.self, from: data) self = try JSONDecoder().decode(Configuration.self, from: data)
} catch { } catch {
print("Failed to decode configuration: \(error)") log("Failed to decode configuration: \(error)")
throw error throw error
} }
} }

View File

@ -94,7 +94,7 @@ final class SQLiteDatabase {
let count = try await User.query(on: database).count() let count = try await User.query(on: database).count()
registeredPlayerCountMetric.update(count) registeredPlayerCountMetric.update(count)
} catch { } 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) { private func sendEmail(name: PlayerName, email: String, token: String) {
guard let mailData = mail else { guard let mailData = mail else {
print("Recovery email not set up") log("Recovery email not set up")
return return
} }
let recipient = Mail.User(name: name, email: email) let recipient = Mail.User(name: name, email: email)
@ -154,7 +154,7 @@ final class SQLiteDatabase {
mailData.smtp.send(mail) { (error) in mailData.smtp.send(mail) { (error) in
if let error = error { if let error = error {
print("Failed to send recovery email to \(email): \(error)") log("Failed to send recovery email to \(email): \(error)")
} }
} }
} }

View File

@ -45,7 +45,7 @@ final class TableManagement {
do { do {
try await loadTables(from: database) try await loadTables(from: database)
} catch { } catch {
print("Failed to load tables: \(error)") log("Failed to load tables: \(error)")
} }
} }
} }
@ -59,7 +59,7 @@ final class TableManagement {
let id = table.id! let id = table.id!
self.tables[id] = WaitingTable(id: id, name: table.name, isPublic: table.isPublic, players: table.players) 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() logTableCount()
logPlayingPlayerCount() logPlayingPlayerCount()
logConnectedPlayerCount() logConnectedPlayerCount()
@ -191,7 +191,7 @@ final class TableManagement {
func performAction(player: PlayerName, action: PlayerAction) -> PlayerActionResult { func performAction(player: PlayerName, action: PlayerAction) -> PlayerActionResult {
guard let table = currentTable(for: player) else { 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 return .noTableJoined
} }
let (result, newTable) = table.perform(action: action, forPlayer: player) let (result, newTable) = table.perform(action: action, forPlayer: player)
@ -212,7 +212,7 @@ final class TableManagement {
func select(game: GameType, player: PlayerName) -> PlayerActionResult { func select(game: GameType, player: PlayerName) -> PlayerActionResult {
guard let aTable = currentTable(for: player) else { 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 return .noTableJoined
} }
guard let table = aTable as? BiddingTable else { guard let table = aTable as? BiddingTable else {
@ -223,7 +223,7 @@ final class TableManagement {
return result return result
} }
guard let newTable = newTable else { 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() table.sendUpdateToAllPlayers()
return result return result
} }

View File

@ -79,7 +79,7 @@ extension Player {
return false return false
} }
socket.close().whenFailure { [weak self] error in socket.close().whenFailure { [weak self] error in
print("Failed to close socket for player: \(self?.name ?? "<Released>"): \(error)") log("Failed to close socket for player: \(self?.name ?? "<Released>"): \(error)")
} }
self.socket = nil self.socket = nil
return true return true
@ -94,7 +94,7 @@ extension Player {
do { do {
try socket.send(encodeJSON(info)) try socket.send(encodeJSON(info))
} catch { } catch {
print("Failed to send info: \(error)") log("Failed to send info: \(error)")
return false return false
} }
return true return true

View File

@ -41,19 +41,19 @@ final class BiddingTable: AbstractTable<BiddingPlayer> {
func select(game: GameType, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { func select(game: GameType, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard player.selectsGame else { guard player.selectsGame else {
print("Player \(name) does not select the game") log("Player \(name) does not select the game")
return (.tableStateInvalid, nil) return (.tableStateInvalid, nil)
} }
guard gameToOutbid.allows(game: game) else { 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) return (.tableStateInvalid, nil)
} }
guard player.canPlay(game: game) else { 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) return (.tableStateInvalid, nil)
} }
@ -64,7 +64,7 @@ final class BiddingTable: AbstractTable<BiddingPlayer> {
@discardableResult @discardableResult
private func selectNextBidder() -> Bool { private func selectNextBidder() -> Bool {
guard let index = players.firstIndex(where: { $0.isNextActor }) else { 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 return false
} }
players[index].isNextActor = false players[index].isNextActor = false
@ -75,7 +75,7 @@ final class BiddingTable: AbstractTable<BiddingPlayer> {
override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard player.canPerform(action) else { guard player.canPerform(action) else {

View File

@ -23,7 +23,7 @@ final class DealingTable: AbstractTable<DealingPlayer> {
override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard player.canPerform(action) else { guard player.canPerform(action) else {

View File

@ -116,12 +116,12 @@ final class FinishedTable: AbstractTable<FinishedPlayer> {
return (.tableStateInvalid, nil) return (.tableStateInvalid, nil)
} }
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard player.canPerform(.deal) else { 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) return (.tableStateInvalid, nil)
} }
let waiting = WaitingTable(oldTableAdvancedByOne: self) let waiting = WaitingTable(oldTableAdvancedByOne: self)

View File

@ -77,15 +77,15 @@ final class PlayingTable: AbstractTable<PlayingPlayer> {
override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard action == .doubleDuringGame else { 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) return (.tableStateInvalid, nil)
} }
guard player.canPerform(.doubleDuringGame) else { guard player.canPerform(.doubleDuringGame) else {
print("Player \(name) is not allowed to raise") log("Player \(name) is not allowed to raise")
return (.tableStateInvalid, nil) return (.tableStateInvalid, nil)
} }
player.numberOfRaises += 1 player.numberOfRaises += 1
@ -96,11 +96,11 @@ final class PlayingTable: AbstractTable<PlayingPlayer> {
override func play(card: Card, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { override func play(card: Card, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard player.isNextActor else { 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) return (.tableStateInvalid, nil)
} }
guard player.canPlay(card: card, for: nextTrick, in: game) else { guard player.canPlay(card: card, for: nextTrick, in: game) else {

View File

@ -106,12 +106,12 @@ final class WaitingTable: AbstractTable<WaitingPlayer> {
return (.tableStateInvalid, nil) return (.tableStateInvalid, nil)
} }
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard player.canPerform(.deal) else { 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) return (.tableStateInvalid, nil)
} }
let table = DealingTable(table: self) let table = DealingTable(table: self)

View File

@ -24,7 +24,7 @@ final class WeddingTable: AbstractTable<WeddingPlayer> {
override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { override func perform(action: PlayerAction, forPlayer name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard player.canPerform(action) else { guard player.canPerform(action) else {
@ -37,6 +37,7 @@ final class WeddingTable: AbstractTable<WeddingPlayer> {
case .withdrawFromAuction: case .withdrawFromAuction:
return performWithdrawl(forPlayer: player) return performWithdrawl(forPlayer: player)
case .increaseOrMatchGame: case .increaseOrMatchGame:
log("Unexpected action \(action) should not be possible")
fatalError() fatalError()
default: default:
return (.tableStateInvalid, nil) return (.tableStateInvalid, nil)
@ -96,19 +97,19 @@ final class WeddingTable: AbstractTable<WeddingPlayer> {
override func play(card: Card, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) { override func play(card: Card, player name: PlayerName) -> (result: PlayerActionResult, table: ManageableTable?) {
guard requiresCardSelection else { 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) return (.tableStateInvalid, nil)
} }
guard let player = players.player(named: name) else { 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) return (.tableStateInvalid, nil)
} }
guard player.selectsGame else { 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) return (.tableStateInvalid, nil)
} }
guard player.canExchange(card: card) else { 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) return (.tableStateInvalid, nil)
} }

View File

@ -46,12 +46,12 @@ public func configure(_ app: Application) throws {
if !configuration.production { if !configuration.production {
app.logger.logLevel = .info app.logger.logLevel = .info
print("[DEVELOPMENT] Using in-memory database") log("[DEVELOPMENT] Using in-memory database")
app.databases.use(.sqlite(.memory), as: .sqlite) app.databases.use(.sqlite(.memory), as: .sqlite)
} else { } else {
app.logger.logLevel = .notice app.logger.logLevel = .notice
let dbFile = storageFolder.appendingPathComponent("db.sqlite").path 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.databases.use(.sqlite(.file(dbFile)), as: .sqlite)
} }
app.migrations.add(UserTableMigration()) app.migrations.add(UserTableMigration())
@ -83,3 +83,8 @@ public func configure(_ app: Application) throws {
status.update(.nominal) status.update(.nominal)
} }
func log(_ message: String) {
MetricObserver.standard?.log(message)
print(message)
}