Skip to content

Commit

Permalink
Support adding multiple event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanparajuli committed Feb 18, 2020
1 parent 9c67895 commit 8ddfad0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen

recyclerView.removeOnScrollListener(onScrollListener)
logcatService?.let {
it.logcat.setEventListener(null)
it.logcat.removeEventListener(this)
it.logcat.unbind(activity as AppCompatActivity)
}
serviceBinder.close()
Expand All @@ -599,7 +599,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen

scrollRecyclerView()

logcat.setEventListener(this)
logcat.addEventListener(this)
logcat.bind(activity as AppCompatActivity)

if (viewModel.stopRecording || arguments?.getBoolean(STOP_RECORDING) == true) {
Expand Down
40 changes: 24 additions & 16 deletions logcat/src/main/java/com/dp/logcat/Logcat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.lifecycle.LifecycleOwner
import com.dp.logger.Logger
import com.logcat.collections.FixedCircularArray
import java.io.*
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.thread
import kotlin.concurrent.withLock
Expand All @@ -26,8 +27,7 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable {

private var recordStartIndex = -1

@Volatile
private var listener: LogsReceivedListener? = null
private val listeners = Collections.newSetFromMap(WeakHashMap<LogsReceivedListener, Boolean>())

private var pollCondition = ConditionVariable()

Expand Down Expand Up @@ -98,7 +98,7 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable {
}

if (filteredLogs.isNotEmpty()) {
handler.post { listener?.onReceivedLogs(filteredLogs) }
handler.post { listeners.forEach { it.onReceivedLogs(filteredLogs) } }
}

pendingLogs.clear()
Expand Down Expand Up @@ -134,20 +134,13 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable {
}
}

fun clearLogs(onClear: (() -> Unit)? = null) {
val wasPaused = paused
pause()

fun clearLogs(onClear: (() -> Unit)? = null) = withPaused {
logsLock.withLock {
logs.clear()
pendingLogs.clear()
}

onClear?.invoke()

if (!wasPaused) {
resume()
}
}

fun restart() {
Expand All @@ -162,16 +155,31 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable {

fun isRunning() = isProcessAlive

fun setEventListener(listener: LogsReceivedListener?) {
private inline fun <T> withPaused(block: () -> T): T {
val wasPaused = paused
pause()
val result = block()
if (!wasPaused) {
resume()
}
return result
}

fun addEventListener(listener: LogsReceivedListener) = withPaused {
logsLock.withLock {
this.listener = listener
listeners += listener
}
}

if (!wasPaused) {
resume()
fun removeEventListener(listener: LogsReceivedListener) = withPaused {
logsLock.withLock {
listeners -= listener
}
}

fun clearEventListeners() = withPaused {
logsLock.withLock {
listeners.clear()
}
}

Expand Down Expand Up @@ -270,7 +278,7 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable {
override fun close() {
stop()
logsLock.withLock {
listener = null
listeners.clear()
}
}

Expand Down

0 comments on commit 8ddfad0

Please sign in to comment.