Files
Sportstime/SportsTime/Core/Services/SyncLogger.swift
Trey t 61c4e39807 feat: update bundle ID config, CloudKit container, and add landing page
- Update CloudKit container ID to iCloud.com.88oakapps.SportsTime across all services
- Update IAP product IDs to match new bundle ID (com.88oakapps.SportsTime)
- Add app landing page with light, welcoming design matching app aesthetic
- Update entitlements and project configuration

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 22:47:55 -06:00

80 lines
2.2 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 {
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
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)
}
}
}