Skip to content

Commit 0caab85

Browse files
Setup a convenience protocol and funnel all logging through it
1 parent 7ea44e8 commit 0caab85

32 files changed

+155
-106
lines changed

Packages/ConfCore/ConfCore/BookmarkSyncObject.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public struct BookmarkSyncObject: CustomCloudKitCodable, BelongsToSession {
2222
var isDeleted: Bool
2323
}
2424

25-
extension Bookmark: SyncObjectConvertible, BelongsToSession {
25+
extension Bookmark: SyncObjectConvertible, BelongsToSession, Logging {
26+
public static let log = makeLogger(subsystem: "ConfCore", category: "\(String(describing: Bookmark.self))+Sync")
2627

2728
public static var syncThrottlingInterval: TimeInterval {
2829
return 0
@@ -50,7 +51,7 @@ extension Bookmark: SyncObjectConvertible, BelongsToSession {
5051
do {
5152
bookmark.snapshot = try Data(contentsOf: snapshotURL)
5253
} catch {
53-
Logger.default.fault("Failed to load bookmark snapshot from CloudKit: \(String(describing: error), privacy: .public)")
54+
log.fault("Failed to load bookmark snapshot from CloudKit: \(String(describing: error), privacy: .public)")
5455
bookmark.snapshot = Data()
5556
}
5657
} else {
@@ -62,7 +63,7 @@ extension Bookmark: SyncObjectConvertible, BelongsToSession {
6263

6364
public var syncObject: BookmarkSyncObject? {
6465
guard let sessionId = session.first?.identifier else {
65-
Logger.default.fault("Bookmark \(self.identifier) is not associated to a session. That's illegal!")
66+
log.fault("Bookmark \(self.identifier) is not associated to a session. That's illegal!")
6667

6768
return nil
6869
}

Packages/ConfCore/ConfCore/ContributorsFetcher.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import Cocoa
1010
import OSLog
1111

12-
public final class ContributorsFetcher {
12+
public final class ContributorsFetcher: Logging {
1313

1414
public static let shared: ContributorsFetcher = ContributorsFetcher()
1515

16-
private let log = Logger(subsystem: "WWDC", category: "ContributorsFetcher")
16+
public static let log = makeLogger()
1717

1818
fileprivate struct Constants {
1919
static let contributorsURL = "https://api.github.com/repos/insidegui/WWDC/contributors"
@@ -55,7 +55,7 @@ public final class ContributorsFetcher {
5555
return
5656
}
5757

58-
self.syncQueue.async {
58+
self.syncQueue.async { [log] in
5959
do {
6060
self.names += try self.parseResponse(data)
6161
} catch {

Packages/ConfCore/ConfCore/Environment.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public struct Environment: Equatable {
4848

4949
if shouldNotify {
5050
DispatchQueue.main.async {
51-
Logger.default.info("Environment base URL: \(environment.baseURL)")
51+
log.info("Environment base URL: \(environment.baseURL)")
5252

5353
NotificationCenter.default.post(name: .WWDCEnvironmentDidChange, object: environment)
5454
}
@@ -113,3 +113,7 @@ extension Environment {
113113
featuredSectionsPath: "/explore.json")
114114

115115
}
116+
117+
extension Environment: Logging {
118+
public static let log = makeLogger()
119+
}

Packages/ConfCore/ConfCore/Error+CloudKit.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import Foundation
1010
import CloudKit
1111
import OSLog
1212

13+
// TODO: Where to?
14+
let ckLog = makeLogger(subsystem: "WWDC", category: "CloudKit")
15+
1316
extension Error {
1417

1518
var isCloudKitConflict: Bool {
@@ -20,30 +23,30 @@ extension Error {
2023

2124
func resolveConflict(with resolver: (CKRecord, CKRecord) -> CKRecord?) -> CKRecord? {
2225
guard let effectiveError = self as? CKError else {
23-
Logger.default.fault("resolveConflict called on an error that was not a CKError. The error was \(String(describing: self), privacy: .public)")
26+
ckLog.fault("resolveConflict called on an error that was not a CKError. The error was \(String(describing: self), privacy: .public)")
2427
return nil
2528
}
2629

2730
guard effectiveError.code == .serverRecordChanged else {
28-
Logger.default.fault("resolveConflict called on a CKError that was not a serverRecordChanged error. The error was \(String(describing: effectiveError), privacy: .public)")
31+
ckLog.fault("resolveConflict called on a CKError that was not a serverRecordChanged error. The error was \(String(describing: effectiveError), privacy: .public)")
2932
return nil
3033
}
3134

3235
guard let clientRecord = effectiveError.userInfo[CKRecordChangedErrorClientRecordKey] as? CKRecord else {
33-
Logger.default.fault("Failed to obtain client record from serverRecordChanged error. The error was \(String(describing: effectiveError), privacy: .public)")
36+
ckLog.fault("Failed to obtain client record from serverRecordChanged error. The error was \(String(describing: effectiveError), privacy: .public)")
3437
return nil
3538
}
3639

3740
guard let serverRecord = effectiveError.userInfo[CKRecordChangedErrorServerRecordKey] as? CKRecord else {
38-
Logger.default.fault("Failed to obtain server record from serverRecordChanged error. The error was \(String(describing: effectiveError), privacy: .public)")
41+
ckLog.fault("Failed to obtain server record from serverRecordChanged error. The error was \(String(describing: effectiveError), privacy: .public)")
3942
return nil
4043
}
4144

4245
return resolver(clientRecord, serverRecord)
4346
}
4447

4548
@discardableResult func retryCloudKitOperationIfPossible(_ log: Logger? = nil, in queue: DispatchQueue = .main, with block: @escaping () -> Void) -> Bool {
46-
let effectiveLog = log ?? .default
49+
let effectiveLog: Logger = log ?? ckLog
4750

4851
guard let effectiveError = self as? CKError else { return false }
4952

Packages/ConfCore/ConfCore/FavoriteSyncObject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension Favorite: SyncObjectConvertible, BelongsToSession {
4343

4444
public var syncObject: FavoriteSyncObject? {
4545
guard let sessionId = session.first?.identifier else {
46-
Logger.default.fault("Favorite \(self.identifier) is not associated to a session. That's illegal!")
46+
ckLog.fault("Favorite \(self.identifier) is not associated to a session. That's illegal!")
4747

4848
return nil
4949
}

Packages/ConfCore/ConfCore/Logger+Static.swift

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Logging.swift
3+
//
4+
//
5+
// Created by Allen Humphreys on 6/5/23.
6+
//
7+
8+
import OSLog
9+
10+
public struct LoggingConfig {
11+
public let subsystem: String
12+
public let category: String
13+
}
14+
15+
public protocol Logging {
16+
static var log: Logger { get }
17+
var log: Logger { get }
18+
static func defaultLoggerConfig() -> LoggingConfig
19+
static func makeLogger(config: LoggingConfig) -> Logger
20+
}
21+
22+
public extension Logging {
23+
static func defaultLoggerConfig() -> LoggingConfig {
24+
let fullyQualifiedTypeName = String(reflecting: Self.self)
25+
let fullyQualifiedTypeNameComponents = fullyQualifiedTypeName.split(separator: ".", maxSplits: 1)
26+
let subsystem = fullyQualifiedTypeNameComponents[0]
27+
let category = fullyQualifiedTypeNameComponents[1]
28+
return LoggingConfig(subsystem: String(subsystem), category: String(category))
29+
}
30+
@inline(__always)
31+
static func makeLogger(config: LoggingConfig = defaultLoggerConfig()) -> Logger {
32+
makeLogger(subsystem: config.subsystem, category: config.category)
33+
}
34+
@inline(__always)
35+
static func makeLogger(subsystem: String, category: String = String(describing: Self.self)) -> Logger {
36+
ConfCore.makeLogger(subsystem: subsystem, category: category)
37+
}
38+
39+
/// Convenience forwarding the static log var to the instance just to make things simpler and easier. Types conforming to Logging only
40+
/// need to create the static var
41+
@inline(__always)
42+
var log: Logger { Self.log }
43+
44+
}
45+
46+
/// Mostly for identifying places that log outside of using the Logging protocol. To help with future refactors.
47+
@inline(__always)
48+
public func makeLogger(subsystem: String, category: String) -> Logger {
49+
Logger(subsystem: subsystem, category: category)
50+
}

Packages/ConfCore/ConfCore/NSCodingExtensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
import QuartzCore
1111
import OSLog
1212

13-
private let _log = Logger(subsystem: "ConfCore", category: "NSCodingExtensions")
13+
private let _log = makeLogger(subsystem: "ConfCore", category: "NSCodingExtensions")
1414

1515
public extension NSKeyedArchiver {
1616

Packages/ConfCore/ConfCore/SessionProgress.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public final class SessionProgress: Object, HasCloudKitFields, SoftDeletable {
4242
}
4343
}
4444

45-
extension Session {
45+
extension Session: Logging {
46+
public static let log = makeLogger()
4647

4748
private static let positionUpdateQueue = DispatchQueue(label: "PositionUpdate", qos: .background)
4849

@@ -90,7 +91,7 @@ extension Session {
9091

9192
try queueRealm.commitWrite()
9293
} catch {
93-
Logger.default.error("Error updating session progress: \(String(describing: error), privacy: .public)")
94+
log.error("Error updating session progress: \(String(describing: error), privacy: .public)")
9495
}
9596
}
9697

@@ -110,7 +111,7 @@ extension Session {
110111

111112
if mustCommit { try realm.commitWrite() }
112113
} catch {
113-
Logger.default.error("Error updating session progress: \(String(describing: error), privacy: .public)")
114+
log.error("Error updating session progress: \(String(describing: error), privacy: .public)")
114115
}
115116
}
116117

Packages/ConfCore/ConfCore/SessionProgressSyncObject.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public struct SessionProgressSyncObject: CustomCloudKitCodable, BelongsToSession
2121
var isDeleted: Bool
2222
}
2323

24-
extension SessionProgress: SyncObjectConvertible, BelongsToSession {
24+
extension SessionProgress: SyncObjectConvertible, BelongsToSession, Logging {
25+
public static let log = makeLogger()
2526

2627
public static var syncThrottlingInterval: TimeInterval {
2728
return 20.0
@@ -49,7 +50,7 @@ extension SessionProgress: SyncObjectConvertible, BelongsToSession {
4950

5051
public var syncObject: SessionProgressSyncObject? {
5152
guard let sessionId = session.first?.identifier else {
52-
Logger.default.fault("SessionProgress \(self.identifier) is not associated to a session. That's illegal!")
53+
log.fault("SessionProgress \(self.identifier) is not associated to a session. That's illegal!")
5354

5455
return nil
5556
}

0 commit comments

Comments
 (0)