mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 16:26:04 +00:00
68 lines
2.6 KiB
Swift
68 lines
2.6 KiB
Swift
//
|
|
// ErrorLogDatabase.swift
|
|
// ErrorLog
|
|
//
|
|
// Created by Brent Simmons on 3/11/26.
|
|
//
|
|
|
|
import Foundation
|
|
import RSCore
|
|
import RSDatabase
|
|
import RSDatabaseObjC
|
|
|
|
public actor ErrorLogDatabase {
|
|
|
|
public nonisolated let databasePath: String
|
|
|
|
private let database: FMDatabase
|
|
|
|
private static let tableCreationStatements = "CREATE TABLE if not EXISTS errors (id INTEGER PRIMARY KEY AUTOINCREMENT, date REAL NOT NULL, sourceName TEXT NOT NULL, sourceID INTEGER NOT NULL, operation TEXT NOT NULL DEFAULT '', fileName TEXT NOT NULL DEFAULT '', functionName TEXT NOT NULL DEFAULT '', lineNumber INTEGER NOT NULL DEFAULT 0, errorMessage TEXT NOT NULL);"
|
|
|
|
private static let pruneLimit = 200
|
|
|
|
public init(databasePath: String) {
|
|
self.databasePath = databasePath
|
|
let database = FMDatabase.openAndSetUpDatabase(path: databasePath)
|
|
database.runCreateStatements(Self.tableCreationStatements)
|
|
ErrorLogTable.pruneEntries(limit: Self.pruneLimit, database: database)
|
|
database.vacuumIfNeeded()
|
|
|
|
self.database = database
|
|
|
|
if !Platform.isRunningUnitTests {
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidEncounterError(_:)), name: .appDidEncounterError, object: nil)
|
|
}
|
|
}
|
|
|
|
public func addEntry(sourceName: String, sourceID: Int, operation: String, fileName: String, functionName: String, lineNumber: Int, errorMessage: String) {
|
|
ErrorLogTable.insertEntry(sourceName: sourceName, sourceID: sourceID, operation: operation, fileName: fileName, functionName: functionName, lineNumber: lineNumber, errorMessage: errorMessage, database: database)
|
|
}
|
|
|
|
public func vacuum() {
|
|
database.vacuum()
|
|
}
|
|
|
|
public func allEntries() -> [ErrorLogEntry] {
|
|
ErrorLogTable.allEntries(database: database)
|
|
}
|
|
|
|
// MARK: - Notifications
|
|
|
|
@objc nonisolated func handleAppDidEncounterError(_ notification: Notification) {
|
|
guard let errorMessage = notification.userInfo?[ErrorLogUserInfoKey.errorMessage] as? String,
|
|
let sourceName = notification.userInfo?[ErrorLogUserInfoKey.sourceName] as? String,
|
|
let sourceID = notification.userInfo?[ErrorLogUserInfoKey.sourceID] as? Int else {
|
|
return
|
|
}
|
|
|
|
let operation = notification.userInfo?[ErrorLogUserInfoKey.operation] as? String ?? ""
|
|
let fileName = notification.userInfo?[ErrorLogUserInfoKey.fileName] as? String ?? ""
|
|
let functionName = notification.userInfo?[ErrorLogUserInfoKey.functionName] as? String ?? ""
|
|
let lineNumber = notification.userInfo?[ErrorLogUserInfoKey.lineNumber] as? Int ?? 0
|
|
|
|
Task {
|
|
await addEntry(sourceName: sourceName, sourceID: sourceID, operation: operation, fileName: fileName, functionName: functionName, lineNumber: lineNumber, errorMessage: errorMessage)
|
|
}
|
|
}
|
|
}
|