From f1822f48836bfdec672be33382c45f9f353ab777 Mon Sep 17 00:00:00 2001 From: Brandon Titus Date: Fri, 1 Mar 2024 19:26:57 -0700 Subject: [PATCH] Add logging to DownloadManager+URLSessionDelegate --- .../DownloadManager+URLSessionDelegate.swift | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/podcasts/DownloadManager+URLSessionDelegate.swift b/podcasts/DownloadManager+URLSessionDelegate.swift index c874d2d664..7ec8a2bc1e 100644 --- a/podcasts/DownloadManager+URLSessionDelegate.swift +++ b/podcasts/DownloadManager+URLSessionDelegate.swift @@ -56,6 +56,8 @@ extension DownloadManager: URLSessionDelegate, URLSessionDownloadDelegate { return } + FileLog.shared.addMessage("Download failed \(error.domain) \(error.code): \(error.localizedDescription)") + // check for ones we cancelled guard let episode = episodeForTask(task, forceReload: true) else { return } // we no longer have this episode removeEpisodeFromCache(episode) @@ -130,6 +132,15 @@ extension DownloadManager: URLSessionDelegate, URLSessionDownloadDelegate { } } + func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) { + let message = log(metrics: metrics) + + if let error = task.error { + let url = task.currentRequest?.url?.absoluteString ?? "unknown" + FileLog.shared.addMessage("Failed Download for URL: \(url) \(message)") + } + } + private func episodeForTask(_ task: URLSessionDownloadTask, forceReload: Bool) -> BaseEpisode? { guard let downloadId = task.taskDescription else { return nil } @@ -153,4 +164,14 @@ extension DownloadManager: URLSessionDelegate, URLSessionDownloadDelegate { DataManager.sharedManager.saveEpisode(downloadStatus: .downloadFailed, downloadError: message, downloadTaskId: nil, episode: episode) NotificationCenter.postOnMainThread(notification: Constants.Notifications.episodeDownloadStatusChanged, object: episode.uuid) } + + private func log(metrics: URLSessionTaskMetrics) -> String { + let duration = metrics.taskInterval.duration + let redirectCount = metrics.redirectCount + let isProxy = metrics.transactionMetrics.last?.isProxyConnection + let isCellular = metrics.transactionMetrics.last?.isCellular + let isMultipath = metrics.transactionMetrics.last?.isMultipath + let tlsCipherSuite = metrics.transactionMetrics.last?.negotiatedTLSCipherSuite + return "Duration: \(duration) Redirects: \(redirectCount) isProxy: \(String(describing: isProxy)) isCellular: \(String(describing: isCellular)) isMultipath: \(String(describing: isMultipath)) tlsCipherSuite: \(String(describing: tlsCipherSuite))" + } }