Fixes ~95 issues from deep audit across 12 categories in 82 files: - Crash prevention: double-resume in PhotoMetadataExtractor, force unwraps in DateRangePicker, array bounds checks in polls/achievements, ProGate hit-test bypass, Dictionary(uniqueKeysWithValues:) → uniquingKeysWith in 4 files - Silent failure elimination: all 34 try? sites replaced with do/try/catch + logging (SavedTrip, TripDetailView, CanonicalSyncService, BootstrapService, CanonicalModels, CKModels, SportsTimeApp, and more) - Performance: cached DateFormatters (7 files), O(1) team lookups via AppDataProvider, achievement definition dictionary, AnimatedBackground consolidated from 19 Tasks to 1, task cancellation in SharePreviewView - Concurrency: UIKit drawing → MainActor.run, background fetch timeout guard, @MainActor on ThemeManager/AppearanceManager, SyncLogger read/write race fix - Planning engine: game end time in travel feasibility, state-aware city normalization, exact city matching, DrivingConstraints parameter propagation - IAP: unknown subscription states → expired, unverified transaction logging, entitlements updated before paywall dismiss, restore visible to all users - Security: API key to Info.plist lookup, filename sanitization in PDF export, honest User-Agent, removed stale "Feels" analytics super properties - Navigation: consolidated competing navigationDestination, boolean → value-based - Testing: 8 sleep() → waitForExistence, duplicates extracted, Swift 6 compat - Service bugs: infinite retry cap, duplicate achievement prevention, TOCTOU vote fix, PollVote.odg → voterId rename, deterministic placeholder IDs, parallel MKDirections, Sendable-safe POI struct Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
85 lines
2.4 KiB
Swift
85 lines
2.4 KiB
Swift
//
|
|
// SyncLogger.swift
|
|
// SportsTime
|
|
//
|
|
// File-based logger for sync operations.
|
|
// Writes to Documents/sync_log.txt for viewing in Settings.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
final class SyncLogger: @unchecked Sendable {
|
|
static let shared = SyncLogger()
|
|
|
|
private let fileURL: URL
|
|
private let maxLines = 500
|
|
private let queue = DispatchQueue(label: "com.88oakapps.SportsTime.synclogger")
|
|
|
|
private init() {
|
|
let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
|
|
?? FileManager.default.temporaryDirectory
|
|
fileURL = docs.appendingPathComponent("sync_log.txt")
|
|
}
|
|
|
|
// MARK: - Public API
|
|
|
|
func log(_ message: String) {
|
|
let timestamp = ISO8601DateFormatter().string(from: Date())
|
|
let line = "[\(timestamp)] \(message)\n"
|
|
|
|
// Also print to console
|
|
#if DEBUG
|
|
print(message)
|
|
#endif
|
|
|
|
queue.async { [weak self] in
|
|
self?.appendToFile(line)
|
|
}
|
|
}
|
|
|
|
func readLog() -> String {
|
|
queue.sync {
|
|
guard FileManager.default.fileExists(atPath: fileURL.path) else {
|
|
return "No sync logs yet."
|
|
}
|
|
return (try? String(contentsOf: fileURL, encoding: .utf8)) ?? "Failed to read log."
|
|
}
|
|
}
|
|
|
|
func clearLog() {
|
|
queue.async { [weak self] in
|
|
guard let self = self else { return }
|
|
try? FileManager.default.removeItem(at: self.fileURL)
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func appendToFile(_ line: String) {
|
|
if !FileManager.default.fileExists(atPath: fileURL.path) {
|
|
FileManager.default.createFile(atPath: fileURL.path, contents: nil)
|
|
}
|
|
|
|
guard let handle = try? FileHandle(forWritingTo: fileURL) else { return }
|
|
defer { try? handle.close() }
|
|
|
|
handle.seekToEndOfFile()
|
|
if let data = line.data(using: .utf8) {
|
|
handle.write(data)
|
|
}
|
|
|
|
// Trim if too large
|
|
trimIfNeeded()
|
|
}
|
|
|
|
private func trimIfNeeded() {
|
|
guard let content = try? String(contentsOf: fileURL, encoding: .utf8) else { return }
|
|
let lines = content.components(separatedBy: "\n")
|
|
|
|
if lines.count > maxLines {
|
|
let trimmed = lines.suffix(maxLines).joined(separator: "\n")
|
|
try? trimmed.write(to: fileURL, atomically: true, encoding: .utf8)
|
|
}
|
|
}
|
|
}
|