Skip to content

Commit

Permalink
[ECO-5166][CHA-RC2] Implement logging for the room
Browse files Browse the repository at this point in the history
1. Renamed property roomLogger to logger
2. Added basic trace, debug and error loggings for the Room
  • Loading branch information
sacOO7 committed Dec 6, 2024
1 parent 6850da4 commit 8efd8c4
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions chat-android/src/main/java/com/ably/chat/Messages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ internal class DefaultMessagesSubscription(

internal class DefaultMessages(
val room: DefaultRoom,
) : Messages, ContributesToRoomLifecycleImpl(room.roomLogger) {
) : Messages, ContributesToRoomLifecycleImpl(room.logger) {

override val featureName: String = "messages"

private var channelStateListener: ChannelStateListener

private val logger = room.roomLogger.withContext(tag = "Messages")
private val logger = room.logger.withContext(tag = "Messages")

private val roomId = room.roomId

Expand Down
4 changes: 2 additions & 2 deletions chat-android/src/main/java/com/ably/chat/Occupancy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ data class OccupancyEvent(

internal class DefaultOccupancy(
private val room: DefaultRoom,
) : Occupancy, ContributesToRoomLifecycleImpl(room.roomLogger) {
) : Occupancy, ContributesToRoomLifecycleImpl(room.logger) {

override val featureName: String = "occupancy"

override val attachmentErrorCode: ErrorCode = ErrorCode.OccupancyAttachmentFailed

override val detachmentErrorCode: ErrorCode = ErrorCode.OccupancyDetachmentFailed

private val logger = room.roomLogger.withContext(tag = "Occupancy")
private val logger = room.logger.withContext(tag = "Occupancy")

override val channel: Channel = room.messages.channel

Expand Down
4 changes: 2 additions & 2 deletions chat-android/src/main/java/com/ably/chat/Presence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ data class PresenceEvent(

internal class DefaultPresence(
private val room: DefaultRoom,
) : Presence, ContributesToRoomLifecycleImpl(room.roomLogger) {
) : Presence, ContributesToRoomLifecycleImpl(room.logger) {

override val featureName = "presence"

Expand All @@ -146,7 +146,7 @@ internal class DefaultPresence(

override val channel: Channel = room.messages.channel

private val logger = room.roomLogger.withContext(tag = "Presence")
private val logger = room.logger.withContext(tag = "Presence")

private val presence = channel.presence

Expand Down
19 changes: 15 additions & 4 deletions chat-android/src/main/java/com/ably/chat/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ internal class DefaultRoom(
internal val clientId: String,
logger: Logger,
) : Room {
internal val roomLogger = logger.withContext("Room", mapOf("roomId" to roomId))
private val logger = logger.withContext("Room", mapOf("roomId" to roomId))

/**
* RoomScope is a crucial part of the Room lifecycle. It manages sequential and atomic operations.
Expand All @@ -133,6 +133,7 @@ internal class DefaultRoom(
override val presence: Presence
get() {
if (_presence == null) { // CHA-RC2b
logger.error("Presence access failed, it is not enabled")
throw ablyException("Presence is not enabled for this room", ErrorCode.BadRequest)
}
return _presence as Presence
Expand All @@ -142,6 +143,7 @@ internal class DefaultRoom(
override val reactions: RoomReactions
get() {
if (_reactions == null) { // CHA-RC2b
logger.error("Reactions access failed, it is not enabled")
throw ablyException("Reactions are not enabled for this room", ErrorCode.BadRequest)
}
return _reactions as RoomReactions
Expand All @@ -151,6 +153,7 @@ internal class DefaultRoom(
override val typing: Typing
get() {
if (_typing == null) { // CHA-RC2b
logger.error("Typing access failed, it is not enabled")
throw ablyException("Typing is not enabled for this room", ErrorCode.BadRequest)
}
return _typing as Typing
Expand All @@ -160,12 +163,13 @@ internal class DefaultRoom(
override val occupancy: Occupancy
get() {
if (_occupancy == null) { // CHA-RC2b
logger.error("Occupancy access failed, it is not enabled")
throw ablyException("Occupancy is not enabled for this room", ErrorCode.BadRequest)
}
return _occupancy as Occupancy
}

private val statusLifecycle = DefaultRoomLifecycle(roomLogger)
private val statusLifecycle = DefaultRoomLifecycle(this.logger)

override val status: RoomStatus
get() = statusLifecycle.status
Expand All @@ -176,7 +180,9 @@ internal class DefaultRoom(
private var lifecycleManager: RoomLifecycleManager

init {
options.validateRoomOptions() // CHA-RC2a
this.logger.debug("Initializing based on options: $options")

options.validateRoomOptions(this.logger) // CHA-RC2a

// CHA-RC2e - Add contributors/features as per the order of precedence
val roomFeatures = mutableListOf<ContributesToRoomLifecycle>(messages)
Expand Down Expand Up @@ -205,7 +211,9 @@ internal class DefaultRoom(
_occupancy = occupancyContributor
}

lifecycleManager = RoomLifecycleManager(roomScope, statusLifecycle, roomFeatures, roomLogger)
lifecycleManager = RoomLifecycleManager(roomScope, statusLifecycle, roomFeatures, this.logger)

this.logger.debug("Initialized with features: ${roomFeatures.joinToString { it.featureName }}")
}

override fun onStatusChange(listener: RoomLifecycle.Listener): Subscription =
Expand All @@ -216,10 +224,12 @@ internal class DefaultRoom(
}

override suspend fun attach() {
logger.trace("attach;")
lifecycleManager.attach()
}

override suspend fun detach() {
logger.trace("detach;")
lifecycleManager.detach()
}

Expand All @@ -228,6 +238,7 @@ internal class DefaultRoom(
* This is an internal method and only called from Rooms interface implementation.
*/
internal suspend fun release() {
logger.trace("release;")
lifecycleManager.release()
}

Expand Down
3 changes: 2 additions & 1 deletion chat-android/src/main/java/com/ably/chat/RoomOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ class OccupancyOptions {
* Throws AblyException for invalid room configuration.
* Spec: CHA-RC2a
*/
internal fun RoomOptions.validateRoomOptions() {
internal fun RoomOptions.validateRoomOptions(logger: Logger) {
typing?.let {
if (typing.timeoutMs <= 0) {
logger.error("Typing timeout must be greater than 0, found ${typing.timeoutMs}")
throw ablyException("Typing timeout must be greater than 0", ErrorCode.InvalidRequestBody)
}
}
Expand Down
4 changes: 2 additions & 2 deletions chat-android/src/main/java/com/ably/chat/RoomReactions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ data class SendReactionParams(

internal class DefaultRoomReactions(
private val room: DefaultRoom,
) : RoomReactions, ContributesToRoomLifecycleImpl(room.roomLogger) {
) : RoomReactions, ContributesToRoomLifecycleImpl(room.logger) {

override val featureName = "reactions"

Expand All @@ -118,7 +118,7 @@ internal class DefaultRoomReactions(

override val detachmentErrorCode: ErrorCode = ErrorCode.ReactionsDetachmentFailed

private val logger = room.roomLogger.withContext(tag = "Reactions")
private val logger = room.logger.withContext(tag = "Reactions")

// (CHA-ER3) Ephemeral room reactions are sent to Ably via the Realtime connection via a send method.
// (CHA-ER3a) Reactions are sent on the channel using a message in a particular format - see spec for format.
Expand Down
4 changes: 2 additions & 2 deletions chat-android/src/main/java/com/ably/chat/Typing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ data class TypingEvent(val currentlyTyping: Set<String>)
internal class DefaultTyping(
private val room: DefaultRoom,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
) : Typing, ContributesToRoomLifecycleImpl(room.roomLogger) {
) : Typing, ContributesToRoomLifecycleImpl(room.logger) {
private val typingIndicatorsChannelName = "${room.roomId}::\$chat::\$typingIndicators"

override val featureName = "typing"
Expand All @@ -103,7 +103,7 @@ internal class DefaultTyping(

override val detachmentErrorCode: ErrorCode = ErrorCode.TypingDetachmentFailed

private val logger = room.roomLogger.withContext(tag = "Typing")
private val logger = room.logger.withContext(tag = "Typing")

private val typingScope = CoroutineScope(dispatcher.limitedParallelism(1) + SupervisorJob())

Expand Down

0 comments on commit 8efd8c4

Please sign in to comment.