Skip to content

Avoid backup cache crash #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions Sources/SupabaseLogging/LogsCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,40 @@ final class LogsCache<T: Codable> {
}
return poppedLogs
}

func backupCache() {
queue.sync(flags: .barrier) {
do {
let jsonCompatibleLogs = cachedLogs.map { log -> [String: Any] in
if let logEntry = log as? LogEntry {
return [
"label": logEntry.label,
"file": logEntry.file,
"line": logEntry.line,
"source": logEntry.source,
"function": logEntry.function,
"level": logEntry.level,
"message": logEntry.message,
"loggedAt": ISO8601DateFormatter().string(from: logEntry.loggedAt), // Convert Date to String
"metadata": logEntry.metadata // Assuming metadata is already JSON-compatible
]
}
return [:] // Return an empty dictionary if conversion fails
}

func backupCache() {
queue.sync(flags: .barrier) {
do {
let data = try JSONSerialization.data(withJSONObject: cachedLogs)
try data.write(to: LogsCache.fileURL())
self.cachedLogs = []
} catch {
if isDebug {
print("Error saving Logs cache.")
let data = try JSONSerialization.data(withJSONObject: jsonCompatibleLogs, options: .prettyPrinted)
try data.write(to: LogsCache.fileURL())
self.cachedLogs = []
} catch {
if isDebug {
print("Error saving logs to cache: \(error.localizedDescription)")
print("Error details: \(error)")
}
}
}
}
}
}

private static func fileURL() throws -> URL {
try FileManager.default.url(
for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: false
Expand All @@ -58,7 +77,8 @@ final class LogsCache<T: Codable> {
self.cachedLogs = logs
} catch {
if isDebug {
print("Error recovering logs from cache.")
print("Error recovering logs from cache: \(error.localizedDescription)")
print("Error details: \(error)")
}
}
}
Expand Down