fix(schedule): use start of day for date range queries
- Fix startDate to use Calendar.startOfDay instead of Date() to include games earlier in the current day - Add SyncLogger for file-based sync logging viewable in Settings - Add "View Sync Logs" button in Settings debug section - Add diagnostics and NBA game logging to ScheduleViewModel - Add dropped game logging to DataProvider.filterRichGames - Use SyncLogger in SportsTimeApp and CloudKitService for sync operations Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
79
SportsTime/Core/Services/SyncLogger.swift
Normal file
79
SportsTime/Core/Services/SyncLogger.swift
Normal file
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// SyncLogger.swift
|
||||
// SportsTime
|
||||
//
|
||||
// File-based logger for sync operations.
|
||||
// Writes to Documents/sync_log.txt for viewing in Settings.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
final class SyncLogger {
|
||||
static let shared = SyncLogger()
|
||||
|
||||
private let fileURL: URL
|
||||
private let maxLines = 500
|
||||
private let queue = DispatchQueue(label: "com.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
|
||||
print(message)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user