38 lines
1.0 KiB
Kotlin
38 lines
1.0 KiB
Kotlin
package com.gacy.android.service
|
|||
|
|
|
||
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||
|
|
import kotlinx.coroutines.flow.StateFlow
|
||
|
|
import kotlinx.coroutines.flow.asStateFlow
|
||
|
|
import java.text.SimpleDateFormat
|
||
|
|
import java.util.Date
|
||
|
|
import java.util.Locale
|
||
|
|
|
||
|
|
object LogBuffer {
|
||
|
|
|
||
|
|
private const val MAX_LOG_LINES = 500
|
||
|
|
private val timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
|
||
|
|
|
||
|
|
private val _logs = MutableStateFlow<List<String>>(emptyList())
|
||
|
|
val logs: StateFlow<List<String>> = _logs.asStateFlow()
|
||
|
|
|
||
|
|
@Synchronized
|
||
|
|
fun append(message: String) {
|
||
|
|
val timestamp = timeFormat.format(Date())
|
||
|
|
val formatted = "[$timestamp] $message"
|
||
|
|
val current = _logs.value.toMutableList()
|
||
|
|
current.add(formatted)
|
||
|
|
if (current.size > MAX_LOG_LINES) {
|
||
|
|
current.removeAt(0)
|
||
|
|
}
|
||
|
|
_logs.value = current
|
||
|
|
}
|
||
|
|
|
||
|
|
@Synchronized
|
||
|
|
fun clear() {
|
||
|
|
_logs.value = emptyList()
|
||
|
|
}
|
||
|
|
|
||
|
|
fun getAllText(): String {
|
||
|
|
return _logs.value.joinToString("\n")
|
||
|
|
}
|
||
|
|
}
|