mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 01:10:38 +00:00
Add filename, functionname, linenumber to error log.
This commit is contained in:
1 parent
e94cfc3a7a
commit
cfb561d4b2
9 files changed
+54
-22
No files matched your search
@@ -709,8 +709,8 @@ private extension CloudKitAccountDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
func postSyncError(_ error: Error, account: Account, operation: String) {
|
||||
let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: account.nameForDisplay, sourceID: account.type.rawValue, operation: operation, errorMessage: error.localizedDescription)
|
||||
func postSyncError(_ error: Error, account: Account, operation: String, fileName: String = #fileID, functionName: String = #function, lineNumber: Int = #line) {
|
||||
let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: account.nameForDisplay, sourceID: account.type.rawValue, operation: operation, errorMessage: error.localizedDescription, fileName: fileName, functionName: functionName, lineNumber: lineNumber)
|
||||
NotificationCenter.default.post(name: .appDidEncounterError, object: self, userInfo: errorLogUserInfo)
|
||||
}
|
||||
|
||||
|
||||
@@ -936,8 +936,8 @@ private extension FeedbinAccountDelegate {
|
||||
account.removeAllInstancesOfFeedFromTreeAtAllLevels(feed)
|
||||
}
|
||||
|
||||
func postSyncError(_ error: Error, account: Account, operation: String) {
|
||||
let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: account.nameForDisplay, sourceID: account.type.rawValue, operation: operation, errorMessage: error.localizedDescription)
|
||||
func postSyncError(_ error: Error, account: Account, operation: String, fileName: String = #fileID, functionName: String = #function, lineNumber: Int = #line) {
|
||||
let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: account.nameForDisplay, sourceID: account.type.rawValue, operation: operation, errorMessage: error.localizedDescription, fileName: fileName, functionName: functionName, lineNumber: lineNumber)
|
||||
NotificationCenter.default.post(name: .appDidEncounterError, object: self, userInfo: errorLogUserInfo)
|
||||
}
|
||||
}
|
||||
@@ -716,8 +716,8 @@ import Secrets
|
||||
|
||||
private extension FeedlyAccountDelegate {
|
||||
|
||||
func postSyncError(_ error: Error, account: Account, operation: String) {
|
||||
let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: account.nameForDisplay, sourceID: account.type.rawValue, operation: operation, errorMessage: error.localizedDescription)
|
||||
func postSyncError(_ error: Error, account: Account, operation: String, fileName: String = #fileID, functionName: String = #function, lineNumber: Int = #line) {
|
||||
let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: account.nameForDisplay, sourceID: account.type.rawValue, operation: operation, errorMessage: error.localizedDescription, fileName: fileName, functionName: functionName, lineNumber: lineNumber)
|
||||
NotificationCenter.default.post(name: .appDidEncounterError, object: self, userInfo: errorLogUserInfo)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -479,8 +479,8 @@ import Secrets
|
||||
|
||||
extension NewsBlurAccountDelegate {
|
||||
|
||||
func postSyncError(_ error: Error, account: Account, operation: String) {
|
||||
let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: account.nameForDisplay, sourceID: account.type.rawValue, operation: operation, errorMessage: error.localizedDescription)
|
||||
func postSyncError(_ error: Error, account: Account, operation: String, fileName: String = #fileID, functionName: String = #function, lineNumber: Int = #line) {
|
||||
let errorLogUserInfo = ErrorLogUserInfoKey.userInfo(sourceName: account.nameForDisplay, sourceID: account.type.rawValue, operation: operation, errorMessage: error.localizedDescription, fileName: fileName, functionName: functionName, lineNumber: lineNumber)
|
||||
NotificationCenter.default.post(name: .appDidEncounterError, object: self, userInfo: errorLogUserInfo)
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public actor ErrorLogDatabase {
|
||||
|
||||
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, errorMessage TEXT NOT NULL);"
|
||||
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
|
||||
|
||||
@@ -31,8 +31,8 @@ public actor ErrorLogDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
public func addEntry(sourceName: String, sourceID: Int, operation: String, errorMessage: String) {
|
||||
ErrorLogTable.insertEntry(sourceName: sourceName, sourceID: sourceID, operation: operation, errorMessage: errorMessage, database: database)
|
||||
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 allEntries() -> [ErrorLogEntry] {
|
||||
@@ -47,9 +47,14 @@ public actor ErrorLogDatabase {
|
||||
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, errorMessage: errorMessage)
|
||||
await addEntry(sourceName: sourceName, sourceID: sourceID, operation: operation, fileName: fileName, functionName: functionName, lineNumber: lineNumber, errorMessage: errorMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,14 +15,20 @@ public struct ErrorLogEntry: Sendable {
|
||||
public let sourceName: String
|
||||
public let sourceID: Int // 0-99 reserved for AccountType.rawValue. 100 and up for other components.
|
||||
public let operation: String
|
||||
public let fileName: String
|
||||
public let functionName: String
|
||||
public let lineNumber: Int
|
||||
public let errorMessage: String
|
||||
|
||||
public init(id: Int, date: Date, sourceName: String, sourceID: Int, operation: String, errorMessage: String) {
|
||||
public init(id: Int, date: Date, sourceName: String, sourceID: Int, operation: String, fileName: String, functionName: String, lineNumber: Int, errorMessage: String) {
|
||||
self.id = id
|
||||
self.date = date
|
||||
self.sourceName = sourceName
|
||||
self.sourceID = sourceID
|
||||
self.operation = operation
|
||||
self.fileName = fileName
|
||||
self.functionName = functionName
|
||||
self.lineNumber = lineNumber
|
||||
self.errorMessage = errorMessage
|
||||
}
|
||||
|
||||
@@ -32,6 +38,9 @@ public struct ErrorLogEntry: Sendable {
|
||||
static let sourceName = "sourceName"
|
||||
static let sourceID = "sourceID"
|
||||
static let operation = "operation"
|
||||
static let fileName = "fileName"
|
||||
static let functionName = "functionName"
|
||||
static let lineNumber = "lineNumber"
|
||||
static let errorMessage = "errorMessage"
|
||||
}
|
||||
|
||||
@@ -41,6 +50,9 @@ public struct ErrorLogEntry: Sendable {
|
||||
DatabaseKey.sourceName: sourceName,
|
||||
DatabaseKey.sourceID: sourceID,
|
||||
DatabaseKey.operation: operation,
|
||||
DatabaseKey.fileName: fileName,
|
||||
DatabaseKey.functionName: functionName,
|
||||
DatabaseKey.lineNumber: lineNumber,
|
||||
DatabaseKey.errorMessage: errorMessage
|
||||
]
|
||||
}
|
||||
|
||||
@@ -19,13 +19,19 @@ public struct ErrorLogUserInfoKey {
|
||||
public static let sourceName = "sourceName"
|
||||
public static let sourceID = "sourceID" // 0-99 are AccountType raw values. 100 and greater are for other components.
|
||||
public static let operation = "operation"
|
||||
public static let fileName = "fileName"
|
||||
public static let functionName = "functionName"
|
||||
public static let lineNumber = "lineNumber"
|
||||
public static let errorMessage = "errorMessage"
|
||||
|
||||
public static func userInfo(sourceName: String, sourceID: Int, operation: String, errorMessage: String) -> [String: Any] {
|
||||
public static func userInfo(sourceName: String, sourceID: Int, operation: String, errorMessage: String, fileName: String = #fileID, functionName: String = #function, lineNumber: Int = #line) -> [String: Any] {
|
||||
[
|
||||
Self.sourceName: sourceName,
|
||||
Self.sourceID: sourceID,
|
||||
Self.operation: operation,
|
||||
Self.fileName: fileName,
|
||||
Self.functionName: functionName,
|
||||
Self.lineNumber: lineNumber,
|
||||
Self.errorMessage: errorMessage
|
||||
]
|
||||
}
|
||||
|
||||
@@ -13,12 +13,15 @@ struct ErrorLogTable {
|
||||
|
||||
static let name = "errors"
|
||||
|
||||
static func insertEntry(sourceName: String, sourceID: Int, operation: String, errorMessage: String, database: FMDatabase) {
|
||||
static func insertEntry(sourceName: String, sourceID: Int, operation: String, fileName: String, functionName: String, lineNumber: Int, errorMessage: String, database: FMDatabase) {
|
||||
let dictionary: DatabaseDictionary = [
|
||||
ErrorLogEntry.DatabaseKey.date: Date().timeIntervalSince1970,
|
||||
ErrorLogEntry.DatabaseKey.sourceName: sourceName,
|
||||
ErrorLogEntry.DatabaseKey.sourceID: sourceID,
|
||||
ErrorLogEntry.DatabaseKey.operation: operation,
|
||||
ErrorLogEntry.DatabaseKey.fileName: fileName,
|
||||
ErrorLogEntry.DatabaseKey.functionName: functionName,
|
||||
ErrorLogEntry.DatabaseKey.lineNumber: lineNumber,
|
||||
ErrorLogEntry.DatabaseKey.errorMessage: errorMessage
|
||||
]
|
||||
database.insertRow(dictionary, insertType: .normal, tableName: name)
|
||||
@@ -57,7 +60,10 @@ private extension ErrorLogTable {
|
||||
let date = Date(timeIntervalSince1970: row.double(forColumn: ErrorLogEntry.DatabaseKey.date))
|
||||
let sourceID = Int(row.int(forColumn: ErrorLogEntry.DatabaseKey.sourceID))
|
||||
let operation = row.string(forColumn: ErrorLogEntry.DatabaseKey.operation) ?? ""
|
||||
let fileName = row.string(forColumn: ErrorLogEntry.DatabaseKey.fileName) ?? ""
|
||||
let functionName = row.string(forColumn: ErrorLogEntry.DatabaseKey.functionName) ?? ""
|
||||
let lineNumber = Int(row.int(forColumn: ErrorLogEntry.DatabaseKey.lineNumber))
|
||||
|
||||
return ErrorLogEntry(id: id, date: date, sourceName: sourceName, sourceID: sourceID, operation: operation, errorMessage: errorMessage)
|
||||
return ErrorLogEntry(id: id, date: date, sourceName: sourceName, sourceID: sourceID, operation: operation, fileName: fileName, functionName: functionName, lineNumber: lineNumber, errorMessage: errorMessage)
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import Testing
|
||||
import Foundation
|
||||
@testable import ErrorLog
|
||||
|
||||
@Suite(.serialized) struct ErrorLogDatabaseTests {
|
||||
@Suite struct ErrorLogDatabaseTests {
|
||||
|
||||
private func temporaryDatabasePath() -> String {
|
||||
let tempDir = NSTemporaryDirectory()
|
||||
@@ -28,7 +28,7 @@ import Foundation
|
||||
defer { deleteDatabaseFiles(at: path) }
|
||||
|
||||
let database = ErrorLogDatabase(databasePath: path)
|
||||
await database.addEntry(sourceName: "TestAccount", sourceID: 1, operation: "Refreshing", errorMessage: "Something went wrong")
|
||||
await database.addEntry(sourceName: "TestAccount", sourceID: 1, operation: "Refreshing", fileName: "Account/TestFile.swift", functionName: "testFunction()", lineNumber: 42, errorMessage: "Something went wrong")
|
||||
|
||||
let entries = await database.allEntries()
|
||||
#expect(entries.count == 1)
|
||||
@@ -37,6 +37,9 @@ import Foundation
|
||||
#expect(entry.sourceName == "TestAccount")
|
||||
#expect(entry.sourceID == 1)
|
||||
#expect(entry.operation == "Refreshing")
|
||||
#expect(entry.fileName == "Account/TestFile.swift")
|
||||
#expect(entry.functionName == "testFunction()")
|
||||
#expect(entry.lineNumber == 42)
|
||||
#expect(entry.errorMessage == "Something went wrong")
|
||||
#expect(entry.id > 0)
|
||||
}
|
||||
@@ -46,9 +49,9 @@ import Foundation
|
||||
defer { deleteDatabaseFiles(at: path) }
|
||||
|
||||
let database = ErrorLogDatabase(databasePath: path)
|
||||
await database.addEntry(sourceName: "First", sourceID: 1, operation: "Refreshing", errorMessage: "Error 1")
|
||||
await database.addEntry(sourceName: "Second", sourceID: 2, operation: "Syncing", errorMessage: "Error 2")
|
||||
await database.addEntry(sourceName: "Third", sourceID: 3, operation: "Downloading feed", errorMessage: "Error 3")
|
||||
await database.addEntry(sourceName: "First", sourceID: 1, operation: "Refreshing", fileName: "Account/A.swift", functionName: "refresh()", lineNumber: 10, errorMessage: "Error 1")
|
||||
await database.addEntry(sourceName: "Second", sourceID: 2, operation: "Syncing", fileName: "Account/B.swift", functionName: "sync()", lineNumber: 20, errorMessage: "Error 2")
|
||||
await database.addEntry(sourceName: "Third", sourceID: 3, operation: "Downloading feed", fileName: "Account/C.swift", functionName: "download()", lineNumber: 30, errorMessage: "Error 3")
|
||||
|
||||
let entries = await database.allEntries()
|
||||
#expect(entries.count == 3)
|
||||
@@ -65,7 +68,7 @@ import Foundation
|
||||
|
||||
let database = ErrorLogDatabase(databasePath: path)
|
||||
for i in 1...210 {
|
||||
await database.addEntry(sourceName: "Account", sourceID: 1, operation: "Refreshing", errorMessage: "Error \(i)")
|
||||
await database.addEntry(sourceName: "Account", sourceID: 1, operation: "Refreshing", fileName: "Account/Test.swift", functionName: "refresh()", lineNumber: i, errorMessage: "Error \(i)")
|
||||
}
|
||||
|
||||
let entriesBeforePrune = await database.allEntries()
|
||||
|
||||
Reference in new issue
Block a user