Skip to content

Commit aeffa2e

Browse files
Merge pull request #148 from ably-labs/some-logging-improvements
Some logging improvements
2 parents 8002523 + 4adb981 commit aeffa2e

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

Sources/AblyChat/DefaultRoomLifecycleContributor.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Ably
22

3-
internal actor DefaultRoomLifecycleContributor: RoomLifecycleContributor, EmitsDiscontinuities {
3+
internal actor DefaultRoomLifecycleContributor: RoomLifecycleContributor, EmitsDiscontinuities, CustomDebugStringConvertible {
44
internal nonisolated let channel: DefaultRoomLifecycleContributorChannel
55
internal nonisolated let feature: RoomFeature
66
private var discontinuitySubscriptions: [Subscription<ARTErrorInfo>] = []
@@ -24,9 +24,15 @@ internal actor DefaultRoomLifecycleContributor: RoomLifecycleContributor, EmitsD
2424
discontinuitySubscriptions.append(subscription)
2525
return subscription
2626
}
27+
28+
// MARK: - CustomDebugStringConvertible
29+
30+
internal nonisolated var debugDescription: String {
31+
"(\(id): \(feature), \(channel))"
32+
}
2733
}
2834

29-
internal final class DefaultRoomLifecycleContributorChannel: RoomLifecycleContributorChannel {
35+
internal final class DefaultRoomLifecycleContributorChannel: RoomLifecycleContributorChannel, CustomDebugStringConvertible {
3036
private let underlyingChannel: any RealtimeChannelProtocol
3137

3238
internal init(underlyingChannel: any RealtimeChannelProtocol) {
@@ -55,4 +61,10 @@ internal final class DefaultRoomLifecycleContributorChannel: RoomLifecycleContri
5561
underlyingChannel.on { subscription.emit($0) }
5662
return subscription
5763
}
64+
65+
// MARK: - CustomDebugStringConvertible
66+
67+
internal var debugDescription: String {
68+
"\(underlyingChannel)"
69+
}
5870
}

Sources/AblyChat/RoomLifecycleManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ internal actor DefaultRoomLifecycleManager<Contributor: RoomLifecycleContributor
806806
do {
807807
logger.log(message: "Attaching contributor \(contributor)", level: .info)
808808
try await contributor.channel.attach()
809+
logger.log(message: "Successfully attached contributor \(contributor)", level: .info)
809810
} catch let contributorAttachError {
810811
let contributorState = await contributor.channel.state
811812
logger.log(message: "Failed to attach contributor \(contributor), which is now in state \(contributorState), error \(contributorAttachError)", level: .info)

Tests/AblyChatTests/IntegrationTests.swift

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,55 @@
11
import Ably
2-
import AblyChat
2+
@testable import AblyChat
33
import Testing
44

55
/// Some very basic integration tests, just to check that things are kind of working.
66
///
77
/// It would be nice to give this a time limit, but unfortunately the `timeLimit` trait is only available on iOS 16 etc and above. CodeRabbit suggested writing a timeout function myself and wrapping the contents of the test in it, but I didn’t have time to try understanding its suggested code, so it can wait.
88
@Suite
99
struct IntegrationTests {
10-
private static func createSandboxRealtime(apiKey: String) -> ARTRealtime {
10+
private class AblyCocoaLogger: ARTLog {
11+
private let label: String
12+
13+
init(label: String) {
14+
self.label = label
15+
}
16+
17+
override func log(_ message: String, with level: ARTLogLevel) {
18+
super.log("\(label): \(message)", with: level)
19+
}
20+
}
21+
22+
private final class ChatLogger: LogHandler {
23+
private let label: String
24+
private let defaultLogHandler = DefaultLogHandler()
25+
26+
init(label: String) {
27+
self.label = label
28+
}
29+
30+
func log(message: String, level: LogLevel, context: LogContext?) {
31+
defaultLogHandler.log(message: "\(label): \(message)", level: level, context: context)
32+
}
33+
}
34+
35+
private static func createSandboxRealtime(apiKey: String, loggingLabel: String) -> ARTRealtime {
1136
let realtimeOptions = ARTClientOptions(key: apiKey)
1237
realtimeOptions.environment = "sandbox"
1338
realtimeOptions.clientId = UUID().uuidString
1439

40+
if TestLogger.loggingEnabled {
41+
realtimeOptions.logLevel = .verbose
42+
realtimeOptions.logHandler = AblyCocoaLogger(label: loggingLabel)
43+
}
44+
1545
return ARTRealtime(options: realtimeOptions)
1646
}
1747

18-
private static func createSandboxChatClient(apiKey: String) -> DefaultChatClient {
19-
let realtime = createSandboxRealtime(apiKey: apiKey)
20-
return DefaultChatClient(realtime: realtime, clientOptions: nil)
48+
private static func createSandboxChatClient(apiKey: String, loggingLabel: String) -> DefaultChatClient {
49+
let realtime = createSandboxRealtime(apiKey: apiKey, loggingLabel: loggingLabel)
50+
let clientOptions = TestLogger.loggingEnabled ? ClientOptions(logHandler: ChatLogger(label: loggingLabel), logLevel: .trace) : nil
51+
52+
return DefaultChatClient(realtime: realtime, clientOptions: clientOptions)
2153
}
2254

2355
@Test
@@ -27,8 +59,8 @@ struct IntegrationTests {
2759
let apiKey = try await Sandbox.createAPIKey()
2860

2961
// (1) Create a couple of chat clients — one for sending and one for receiving
30-
let txClient = Self.createSandboxChatClient(apiKey: apiKey)
31-
let rxClient = Self.createSandboxChatClient(apiKey: apiKey)
62+
let txClient = Self.createSandboxChatClient(apiKey: apiKey, loggingLabel: "tx")
63+
let rxClient = Self.createSandboxChatClient(apiKey: apiKey, loggingLabel: "rx")
3264

3365
// (2) Fetch a room
3466
let roomID = "basketball"

0 commit comments

Comments
 (0)