Skip to content

Commit 11ff88a

Browse files
Convert Transcripts package, finesse logging a bit
1 parent 0caab85 commit 11ff88a

File tree

13 files changed

+75
-38
lines changed

13 files changed

+75
-38
lines changed

Packages/ConfCore/ConfCore/ConfCoreExports.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@
99
@_exported import Foundation
1010
@_exported import RealmSwift
1111
@_exported import Siesta
12-
@_exported import Transcripts

Packages/ConfCore/ConfCore/StorageMigrator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ final class StorageMigrator: Logging {
187187
migration.deleteData(forType: "FeaturedAuthor")
188188
}
189189

190-
private static func resetSessionInstances(with migration: Migration, oldVersion: SchemaVersion, log: OSLog) {
190+
private static func resetSessionInstances(with migration: Migration, oldVersion: SchemaVersion, log: Logger) {
191191
migration.deleteData(forType: "SessionInstance")
192192
}
193193

Packages/ConfCore/ConfCore/TranscriptIndexer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public final class TranscriptIndexer: Logging {
7575

7676
let shouldIndex = transcriptableSessions.count > minTranscriptableSessionLimit
7777
if !shouldIndex {
78-
os_log("needsUpdate is false because %d <= %d", log: self.log, type: .debug, transcriptableSessions.count, minTranscriptableSessionLimit)
78+
log.debug("needsUpdate is false because \(transcriptableSessions.count) <= \(minTranscriptableSessionLimit)")
7979
}
8080

8181
return shouldIndex
@@ -94,7 +94,7 @@ public final class TranscriptIndexer: Logging {
9494
var didStop: () -> Void = { }
9595

9696
public func downloadTranscriptsIfNeeded() {
97-
os_log("%{public}@", log: self.log, type: .debug, #function)
97+
log.debug(#function)
9898
downloader = makeDownloader()
9999

100100
didStart()

Packages/ConfCore/ConfCore/TranscriptIndexingClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ final class TranscriptIndexingClient: NSObject, TranscriptIndexingClientProtocol
7979

8080
if !effectiveIgnoreCache && migratedTranscriptsToNativeVersion {
8181
guard TranscriptIndexer.needsUpdate(in: storage) else {
82-
os_log("Skipping transcript indexing: TranscriptIndexer indicates no update is needed", log: self.log, type: .debug)
82+
log.debug("Skipping transcript indexing: TranscriptIndexer indicates no update is needed")
8383
return
8484
}
8585
}

Packages/ConfCore/ConfCore/TranscriptIndexingService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import OSLog
1616
public static let log = makeLogger()
1717

1818
public func indexTranscriptsIfNeeded(manifestURL: URL, ignoringCache: Bool, storageURL: URL, schemaVersion: UInt64) {
19-
os_log(.debug, log: self.log, "Attempting to index transcripts. manifest: \(manifestURL, privacy: .public), ignoringCache: \(ignoringCache)")
19+
log.debug("Attempting to index transcripts. manifest: \(manifestURL, privacy: .public), ignoringCache: \(ignoringCache)")
2020
do {
2121
let config = Realm.Configuration(fileURL: storageURL, schemaVersion: schemaVersion)
2222
let realm = try Realm(configuration: config)

Packages/Transcripts/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// swift-tools-version:5.3
1+
// swift-tools-version:5.8
22

33
import PackageDescription
44

55
let package = Package(
66
name: "Transcripts",
77
platforms: [
8-
.macOS(.v10_15)
8+
.macOS(.v12)
99
],
1010
products: [
1111
.library(

Packages/Transcripts/Transcripts/TranscriptDownloader.swift

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
//
88

99
import Foundation
10-
import os.log
10+
import OSLog
1111

1212
public final class TranscriptDownloader {
1313

14-
private let log = OSLog(subsystem: Transcripts.subsystemName, category: String(describing: TranscriptDownloader.self))
14+
private let log = Logger(subsystem: Transcripts.subsystemName, category: String(describing: TranscriptDownloader.self))
1515

1616
let loader: Loader
1717
let manifestURL: URL
@@ -39,7 +39,7 @@ public final class TranscriptDownloader {
3939
private var validSessionIdentifiers: [String]?
4040

4141
public func fetch(validSessionIdentifiers: [String]? = nil, progress: @escaping ProgressHandler, completion: @escaping CompletionHandler) {
42-
os_log("%{public}@", log: log, type: .debug, #function)
42+
log.debug(#function)
4343

4444
transcriptCountLoaded = 0
4545

@@ -51,10 +51,10 @@ public final class TranscriptDownloader {
5151
}
5252

5353
private func callCompletion() {
54-
os_log("COMPLETED", log: self.log, type: .default)
54+
log.debug("COMPLETED")
5555

5656
if !failedTranscriptIdentifiers.isEmpty {
57-
os_log("Failed transcript IDs: %@", log: self.log, type: .default, failedTranscriptIdentifiers.joined(separator: ", "))
57+
log.debug("Failed transcript IDs: \(self.failedTranscriptIdentifiers.joined(separator: ", "), privacy: .public)")
5858
}
5959

6060
queue.async { [weak self] in
@@ -80,7 +80,7 @@ public final class TranscriptDownloader {
8080
coalescer.run(for: [contents], queue: queue) { [weak self] coalescedContents in
8181
guard let self = self else { return }
8282

83-
os_log("Handing out %d transcript(s) for storage", log: self.log, type: .debug, coalescedContents.count)
83+
self.log.debug("Handing out \(coalescedContents.count) transcript(s) for storage")
8484

8585
self.storage.store(coalescedContents, manifest: manifest)
8686
}
@@ -96,11 +96,11 @@ public final class TranscriptDownloader {
9696
case .success(let manifest):
9797
self.currentManifest = manifest
9898

99-
os_log("Transcript manifest downloaded. %d transcripts available.", log: self.log, type: .debug, manifest.individual.count)
99+
self.log.debug("Transcript manifest downloaded. \(manifest.individual.count) transcripts available.")
100100

101101
self.processManifest(manifest)
102102
case .failure(let error):
103-
os_log("Error downloading transcript manifest: %{public}@", log: self.log, type: .error, String(describing: error))
103+
self.log.error("Error downloading transcript manifest: \(String(describing: error), privacy: .public)")
104104

105105
self.callCompletion()
106106
}
@@ -125,7 +125,7 @@ public final class TranscriptDownloader {
125125
guard let validIdentifiers = validSessionIdentifiers else { return true }
126126

127127
if !validIdentifiers.contains(identifier) {
128-
os_log("Ignoring %@ on manifest: not a session we know about. Maybe next time...", log: self.log, type: .debug, identifier)
128+
self.log.debug("Ignoring \(identifier, privacy: .public) on manifest: not a session we know about. Maybe next time...")
129129

130130
return false
131131
} else {
@@ -139,35 +139,62 @@ public final class TranscriptDownloader {
139139
transcriptCountToLoad = validFeeds.count
140140
transcriptCountLoaded = 0
141141

142-
validFeeds.forEach { feed in
143-
downloadTranscriptIfNeeded(identifier: feed.key, url: feed.value.url, etag: feed.value.etag)
142+
let transcripts: [(identifier: String, url: URL, status: CacheStatus)] = validFeeds.map { feed in
143+
let identifier = feed.key
144+
return (identifier: identifier, url: feed.value.url, status: cacheStatus(identifier: identifier, etag: feed.value.etag))
145+
}
146+
147+
let transcriptsByStatus = Dictionary(grouping: transcripts, by: \.status)
148+
let cached = transcriptsByStatus[.match, default: []]
149+
let mismatched = transcriptsByStatus[.etagMismatch, default: []]
150+
let noPreviousEtag = transcriptsByStatus[.noPreviousEtag, default: []]
151+
152+
let cachedEtagMessage = cached.count == 0 ? "none" : noPreviousEtag.map(\.identifier).joined(separator: ", ")
153+
let mismatchedMessage = mismatched.count == 0 ? "none" : mismatched.map(\.identifier).joined(separator: ", ")
154+
let noPreviousEtagMessage = noPreviousEtag.count == 0 ? "none" : noPreviousEtag.map(\.identifier).joined(separator: ", ")
155+
156+
log.trace(
157+
"""
158+
Transcript Status:
159+
\tCached: \(cachedEtagMessage)
160+
\tMissing etag: \(noPreviousEtagMessage)
161+
\tMismatched etag: \(mismatchedMessage)
162+
"""
163+
)
164+
165+
transcriptCountLoaded += cached.count
166+
167+
(mismatched + noPreviousEtag).forEach { (identifier, url, _) in
168+
downloadTranscript(identifier: identifier, url: url)
144169
}
145170
}
146171

147-
private func downloadTranscriptIfNeeded(identifier: String, url: URL, etag: String) {
148-
if let previousEtag = storage.previousEtag(for: identifier) {
149-
guard etag != previousEtag else {
150-
os_log("Cached transcript %@ still valid, skipping download", log: self.log, type: .debug, identifier)
151-
transcriptCountLoaded += 1
152-
return
153-
}
154-
} else {
155-
os_log("No previous etag for %@, assuming new and downloading", log: self.log, type: .debug, identifier)
172+
enum CacheStatus {
173+
case etagMismatch, noPreviousEtag, match
174+
}
175+
176+
private func cacheStatus(identifier: String, etag: String) -> CacheStatus {
177+
guard let previousEtag = storage.previousEtag(for: identifier) else {
178+
return .noPreviousEtag
156179
}
157180

181+
return etag == previousEtag ? .match : .etagMismatch
182+
}
183+
184+
private func downloadTranscript(identifier: String, url: URL) {
158185
loader.load(from: url, decoder: { try JSONDecoder().decode(TranscriptContent.self, from: $0) }) { [weak self] result in
159186
guard let self = self else { return }
160187

161188
defer { self.queue.async { self.transcriptCountLoaded += 1 } }
162189

163190
switch result {
164191
case .success(let content):
165-
os_log("Downloaded %@", log: self.log, type: .debug, identifier)
192+
self.log.debug("Downloaded \(identifier, privacy: .public)")
166193
self.queue.async { self.store(content) }
167194
case .failure(let error):
168195
self.queue.async { self.failedTranscriptIdentifiers.append(identifier) }
169196

170-
os_log("Failed to download %@: %{public}@", log: self.log, type: .error, identifier, String(describing: error))
197+
self.log.error("Failed to download \(identifier, privacy: .public): \(String(describing: error), privacy: .public)")
171198
}
172199
}
173200
}

Packages/Transcripts/Transcripts/URLSessionLoader.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class URLSessionLoader: Loader {
1313

1414
public init() { }
1515

16-
private let log = OSLog(subsystem: Transcripts.subsystemName, category: String(describing: URLSessionLoader.self))
16+
private let log = Logger(subsystem: Transcripts.subsystemName, category: String(describing: URLSessionLoader.self))
1717

1818
private lazy var session: URLSession = {
1919
let config = URLSessionConfiguration.default
@@ -27,15 +27,15 @@ public final class URLSessionLoader: Loader {
2727

2828
guard let data = data else {
2929
if let error = error {
30-
os_log("Error loading from %@: %{public}@", log: self.log, type: .error, url.absoluteString, String(describing: error))
30+
self.log.error("Error loading from \(url.absoluteString, privacy: .public): \(String(describing: error), privacy: .public)")
3131

3232
completion(.failure(LoaderError.networking(error)))
3333
} else if let response = response as? HTTPURLResponse {
34-
os_log("HTTP error loading from %@: %d", log: self.log, type: .error, url.absoluteString, response.statusCode)
34+
self.log.error("HTTP error loading from \(url.absoluteString, privacy: .public): \(response.statusCode)")
3535

3636
completion(.failure(.http(response.statusCode)))
3737
} else {
38-
os_log("Error loading from %@: no data", log: self.log, type: .error, url.absoluteString)
38+
self.log.error("Error loading from \(url.absoluteString, privacy: .public): no data")
3939

4040
completion(.failure(LoaderError(localizedDescription: "No data returned from the server.")))
4141
}
@@ -48,7 +48,7 @@ public final class URLSessionLoader: Loader {
4848

4949
completion(.success(decoded))
5050
} catch {
51-
os_log("Failed to decode %@ from %@: %{public}@", log: self.log, type: .error, String(describing: T.self), url.absoluteString, String(describing: error))
51+
self.log.error("Failed to decode \(String(describing: T.self), privacy: .public) from \(url.absoluteString, privacy: .public): \(String(describing: error), privacy: .public)")
5252

5353
completion(.failure(.serialization(error)))
5454
}

PlayerUI/Views/PUIPlayerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ extension PUIPlayerView: AVPictureInPictureControllerDelegate {
16261626
_ pictureInPictureController: AVPictureInPictureController,
16271627
failedToStartPictureInPictureWithError error: Error
16281628
) {
1629-
os_log(.error, log: log, "Failed to start PiP \(error, privacy: .public)")
1629+
log.error("Failed to start PiP \(error, privacy: .public)")
16301630
}
16311631

16321632
// Stop

WWDC.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@
283283
4DBFA4D920E160CB00BDF34B /* AVAsset+AsyncHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AVAsset+AsyncHelpers.swift"; sourceTree = "<group>"; };
284284
4DDF6A772177A00C008E5539 /* DownloadsManagementTableCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadsManagementTableCellView.swift; sourceTree = "<group>"; };
285285
4DF6641520C8A85000FD1684 /* SessionsTableViewController+SupportingTypesAndExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SessionsTableViewController+SupportingTypesAndExtensions.swift"; sourceTree = "<group>"; };
286+
91037C8C2A32AF62009AF15E /* Transcripts */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Transcripts; path = Packages/Transcripts; sourceTree = "<group>"; };
286287
DD0159A61ECFE26200F980F1 /* DeepLink.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeepLink.swift; sourceTree = "<group>"; };
287288
DD0159A81ED09F5D00F980F1 /* AppCoordinator+Bookmarks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AppCoordinator+Bookmarks.swift"; sourceTree = "<group>"; };
288289
DD0159CE1ED0CD3A00F980F1 /* PreferencesWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = "<group>"; };
@@ -833,6 +834,7 @@
833834
DDB1A25D2577E51200995FF1 /* LocalDependencies */ = {
834835
isa = PBXGroup;
835836
children = (
837+
91037C8C2A32AF62009AF15E /* Transcripts */,
836838
DDB1A25E2577E51E00995FF1 /* ConfCore */,
837839
);
838840
name = LocalDependencies;

0 commit comments

Comments
 (0)