From cfb561d4b2e9d1fcbacce4820c7e00431f64f211 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 12 Mar 2026 20:52:20 -0700 Subject: [PATCH] Add filename, functionname, linenumber to error log. --- .../CloudKit/CloudKitAccountDelegate.swift | 4 ++-- .../Account/Feedbin/FeedbinAccountDelegate.swift | 4 ++-- .../Account/Feedly/FeedlyAccountDelegate.swift | 4 ++-- .../NewsBlur/NewsBlurAccountDelegate.swift | 4 ++-- .../Sources/ErrorLog/ErrorLogDatabase.swift | 13 +++++++++---- .../ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift | 14 +++++++++++++- .../Sources/ErrorLog/ErrorLogNotification.swift | 8 +++++++- .../ErrorLog/Sources/ErrorLog/ErrorLogTable.swift | 10 ++++++++-- .../ErrorLogTests/ErrorLogDatabaseTests.swift | 15 +++++++++------ 9 files changed, 54 insertions(+), 22 deletions(-) diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index 995150db1..cfbb141a9 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -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) } diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 2e93dee2e..fe7b263bb 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -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) } } diff --git a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index 648929608..9dda56621 100644 --- a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -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) } } diff --git a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index 6057ced3f..0b04d1a36 100644 --- a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -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) } } diff --git a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogDatabase.swift b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogDatabase.swift index 51114d5db..6034b8d7d 100644 --- a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogDatabase.swift +++ b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogDatabase.swift @@ -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) } } } diff --git a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift index d7f820f3f..22362da47 100644 --- a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift +++ b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogEntry.swift @@ -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 ] } diff --git a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogNotification.swift b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogNotification.swift index 1ac261aa6..dfeefe14a 100644 --- a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogNotification.swift +++ b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogNotification.swift @@ -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 ] } diff --git a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogTable.swift b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogTable.swift index 98c27dc10..f08d59c95 100644 --- a/Modules/ErrorLog/Sources/ErrorLog/ErrorLogTable.swift +++ b/Modules/ErrorLog/Sources/ErrorLog/ErrorLogTable.swift @@ -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) } } diff --git a/Modules/ErrorLog/Tests/ErrorLogTests/ErrorLogDatabaseTests.swift b/Modules/ErrorLog/Tests/ErrorLogTests/ErrorLogDatabaseTests.swift index ca23ce957..6007affaf 100644 --- a/Modules/ErrorLog/Tests/ErrorLogTests/ErrorLogDatabaseTests.swift +++ b/Modules/ErrorLog/Tests/ErrorLogTests/ErrorLogDatabaseTests.swift @@ -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()