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/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(

View File

@ -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
}
}

View File

@ -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)")
}
}
}

View File

@ -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
}

View File

@ -79,7 +79,7 @@ extension Player {
return false
}
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
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

View File

@ -41,19 +41,19 @@ final class BiddingTable: AbstractTable<BiddingPlayer> {
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<BiddingPlayer> {
@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<BiddingPlayer> {
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 {

View File

@ -23,7 +23,7 @@ final class DealingTable: AbstractTable<DealingPlayer> {
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 {

View File

@ -116,12 +116,12 @@ final class FinishedTable: AbstractTable<FinishedPlayer> {
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)

View File

@ -77,15 +77,15 @@ final class PlayingTable: AbstractTable<PlayingPlayer> {
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<PlayingPlayer> {
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 {

View File

@ -106,12 +106,12 @@ final class WaitingTable: AbstractTable<WaitingPlayer> {
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)

View File

@ -24,7 +24,7 @@ final class WeddingTable: AbstractTable<WeddingPlayer> {
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<WeddingPlayer> {
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<WeddingPlayer> {
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)
}

View File

@ -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)
}