Skip to content

Commit

Permalink
[ios] add more logs to the icloud sync
Browse files Browse the repository at this point in the history
Signed-off-by: Kiryl Kaveryn <kirylkaveryn@gmail.com>
  • Loading branch information
kirylkaveryn authored and biodranik committed Aug 2, 2024
1 parent 0e8fb07 commit 18bed8d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 76 deletions.
21 changes: 7 additions & 14 deletions iphone/Maps/Core/iCloud/CloudStorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ final class CloudStorageSynchronizationState: NSObject {
private extension CloudStorageManager {
// MARK: - Synchronization Lifecycle
func startSynchronization() {
LOG(.debug, "Start synchronization...")
LOG(.info, "Start synchronization...")
switch cloudDirectoryMonitor.state {
case .started:
LOG(.debug, "Synchronization is already started")
Expand Down Expand Up @@ -135,7 +135,7 @@ private extension CloudStorageManager {
}

func stopSynchronization(withError error: Error? = nil) {
LOG(.debug, "Stop synchronization")
LOG(.info, "Stop synchronization")
localDirectoryMonitor.stop()
cloudDirectoryMonitor.stop()
fileWriter = nil
Expand All @@ -148,13 +148,13 @@ private extension CloudStorageManager {
}

func pauseSynchronization() {
LOG(.debug, "Pause synchronization")
LOG(.info, "Pause synchronization")
localDirectoryMonitor.pause()
cloudDirectoryMonitor.pause()
}

func resumeSynchronization() {
LOG(.debug, "Resume synchronization")
LOG(.info, "Resume synchronization")
localDirectoryMonitor.resume()
cloudDirectoryMonitor.resume()
}
Expand Down Expand Up @@ -230,10 +230,7 @@ private extension CloudStorageManager {
synchronizationError = nil
return
}

LOG(.debug, "Start processing events...")
events.forEach { [weak self] event in
LOG(.debug, "Processing event: \(event)")
guard let self, let fileWriter else { return }
fileWriter.processEvent(event, completion: writingResultHandler(for: event))
}
Expand All @@ -249,13 +246,9 @@ private extension CloudStorageManager {
UserDefaults.standard.set(true, forKey: kUDDidFinishInitialCloudSynchronization)
}
case .reloadCategoriesAtURLs(let urls):
DispatchQueue.main.async {
urls.forEach { self.bookmarksManager.reloadCategory(atFilePath: $0.path) }
}
urls.forEach { self.bookmarksManager.reloadCategory(atFilePath: $0.path) }
case .deleteCategoriesAtURLs(let urls):
DispatchQueue.main.async {
urls.forEach { self.bookmarksManager.deleteCategory(atFilePath: $0.path) }
}
urls.forEach { self.bookmarksManager.deleteCategory(atFilePath: $0.path) }
case .failure(let error):
self.processError(error)
}
Expand Down Expand Up @@ -284,7 +277,7 @@ private extension CloudStorageManager {
stopSynchronization(withError: error)
}
default:
LOG(.debug, "Non-synchronization Error: \(error.localizedDescription)")
LOG(.error, "System Error: \(error.localizedDescription)")
stopSynchronization(withError: error)
}
}
Expand Down
26 changes: 13 additions & 13 deletions iphone/Maps/Core/iCloud/DefaultLocalDirectoryMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ final class DefaultLocalDirectoryMonitor: LocalDirectoryMonitor {
guard state != .started else { return }

let nowTimer = Timer.scheduledTimer(withTimeInterval: .zero, repeats: false) { [weak self] _ in
LOG(.debug, "LocalMonitor: Initial timer firing...")
LOG(.debug, "Initial timer firing...")
self?.debounceTimerDidFire()
}

LOG(.debug, "Start local monitor.")
if let dispatchSource {
dispatchSourceDebounceState = .debounce(source: dispatchSource, timer: nowTimer)
resume()
Expand All @@ -92,7 +93,7 @@ final class DefaultLocalDirectoryMonitor: LocalDirectoryMonitor {

func stop() {
guard state == .started else { return }
LOG(.debug, "LocalMonitor: Stop.")
LOG(.debug, "Stop.")
suspendDispatchSource()
didFinishGatheringIsCalled = false
dispatchSourceDebounceState = .stopped
Expand All @@ -101,21 +102,21 @@ final class DefaultLocalDirectoryMonitor: LocalDirectoryMonitor {

func pause() {
guard state == .started else { return }
LOG(.debug, "LocalMonitor: Pause.")
LOG(.debug, "Pause.")
suspendDispatchSource()
state = .paused
}

func resume() {
guard state != .started else { return }
LOG(.debug, "LocalMonitor: Resume.")
LOG(.debug, "Resume.")
resumeDispatchSource()
state = .started
}

// MARK: - Private
private func queueDidFire() {
LOG(.debug, "LocalMonitor: Queue did fire.")
LOG(.debug, "Queue did fire.")
let debounceTimeInterval = 0.5
switch dispatchSourceDebounceState {
case .started(let source):
Expand All @@ -135,9 +136,9 @@ final class DefaultLocalDirectoryMonitor: LocalDirectoryMonitor {
}

private func debounceTimerDidFire() {
LOG(.debug, "LocalMonitor: Debounce timer did fire.")
LOG(.debug, "Debounce timer did fire.")
guard state == .started else {
LOG(.debug, "LocalMonitor: State is not started. Skip iteration.")
LOG(.debug, "State is not started. Skip iteration.")
return
}
guard case .debounce(let source, let timer) = dispatchSourceDebounceState else { fatalError() }
Expand All @@ -148,16 +149,15 @@ final class DefaultLocalDirectoryMonitor: LocalDirectoryMonitor {
let files = try fileManager
.contentsOfDirectory(at: directory, includingPropertiesForKeys: [.contentModificationDateKey], options: [.skipsHiddenFiles])
.filter { $0.pathExtension == fileType.fileExtension }
LOG(.info, "Local directory content: \(files.map { $0.lastPathComponent }) ")
let contents: LocalContents = try files.map { try LocalMetadataItem(fileUrl: $0) }

if !didFinishGatheringIsCalled {
didFinishGatheringIsCalled = true
LOG(.debug, "LocalMonitor: didFinishGathering called.")
LOG(.debug, "LocalMonitor: contentMetadataItems count: \(contents.count)")
LOG(.debug, "didFinishGathering will be called")
delegate?.didFinishGathering(contents: contents)
} else {
LOG(.debug, "LocalMonitor: didUpdate called.")
LOG(.debug, "LocalMonitor: contentMetadataItems count: \(contents.count)")
LOG(.debug, "didUpdate will be called")
delegate?.didUpdate(contents: contents)
}
} catch {
Expand All @@ -167,7 +167,7 @@ final class DefaultLocalDirectoryMonitor: LocalDirectoryMonitor {

private func suspendDispatchSource() {
if !dispatchSourceIsSuspended {
LOG(.debug, "LocalMonitor: Suspend dispatch source.")
LOG(.debug, "Suspend dispatch source.")
dispatchSource?.suspend()
dispatchSourceIsSuspended = true
dispatchSourceIsResumed = false
Expand All @@ -176,7 +176,7 @@ final class DefaultLocalDirectoryMonitor: LocalDirectoryMonitor {

private func resumeDispatchSource() {
if !dispatchSourceIsResumed {
LOG(.debug, "LocalMonitor: Resume dispatch source.")
LOG(.debug, "Resume dispatch source.")
dispatchSource?.resume()
dispatchSourceIsResumed = true
dispatchSourceIsSuspended = false
Expand Down
10 changes: 10 additions & 0 deletions iphone/Maps/Core/iCloud/MetadataItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ extension CloudMetadataItem {
}
}

extension MetadataItem {
var shortDebugDescription: String {
"File path: \(fileUrl.path), lastModified: \(lastModificationDate)"
}
}

extension LocalMetadataItem {
func relatedCloudItemUrl(to cloudContainer: URL) -> URL {
cloudContainer.appendingPathComponent(fileName)
Expand All @@ -104,6 +110,10 @@ extension Array where Element: MetadataItem {
func firstByName(_ item: any MetadataItem) -> Element? {
return first(where: { $0.fileName == item.fileName })
}

var shortDebugDescription: String {
map { $0.shortDebugDescription }.joined(separator: "\n")
}
}

extension Array where Element == CloudMetadataItem {
Expand Down
Loading

0 comments on commit 18bed8d

Please sign in to comment.