Files
Sportstime/SportsTime/Core/Services/SyncLogger.swift
Trey t 91c5eac22d fix: codebase audit fixes — safety, accessibility, and production hygiene
Address 16 issues from external audit:
- Move StoreKit transaction listener ownership to StoreManager singleton with proper deinit
- Remove noisy VoiceOver announcements, add missing accessibility on StatPill and BootstrapLoadingView
- Replace String @retroactive Identifiable with IdentifiableShareCode wrapper
- Add crash guard in AchievementEngine getContributingVisitIds + cache stadium lookups
- Pre-compute GamesHistoryViewModel filtered properties to avoid redundant SwiftUI recomputation
- Remove force-unwraps in ProgressMapView with safe guard-let fallback
- Add diff-based update gating in ItineraryTableViewWrapper to prevent unnecessary reloads
- Replace deprecated UIScreen.main with UIWindowScene lookup
- Add deinit task cancellation in ScheduleViewModel and SuggestedTripsGenerator
- Wrap ~234 unguarded print() calls across 27 files in #if DEBUG

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 00:07:53 -06:00

82 lines
2.3 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!
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 {
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)
}
}
}