43 lines
1.2 KiB
Kotlin
43 lines
1.2 KiB
Kotlin
package net.bluemangoo.indpjsk.vpn
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
import kotlinx.coroutines.flow.asStateFlow
|
|
import kotlinx.coroutines.flow.update
|
|
import java.time.LocalTime
|
|
import java.time.format.DateTimeFormatter
|
|
|
|
enum class LogLevel { INFO, WARN, ERROR, SUCCESS }
|
|
|
|
data class LogEntry(
|
|
val time: String,
|
|
val level: LogLevel,
|
|
val message: String
|
|
)
|
|
|
|
object VpnLog {
|
|
|
|
private const val MAX = 800
|
|
private val timeFmt = DateTimeFormatter.ofPattern("HH:mm:ss.SSS")
|
|
|
|
private val _logs = MutableStateFlow<List<LogEntry>>(emptyList())
|
|
val logs: StateFlow<List<LogEntry>> = _logs.asStateFlow()
|
|
|
|
private fun append(level: LogLevel, msg: String) {
|
|
val entry = LogEntry(
|
|
time = LocalTime.now().format(timeFmt),
|
|
level = level,
|
|
message = msg
|
|
)
|
|
_logs.update { (it + entry).takeLast(MAX) }
|
|
}
|
|
|
|
fun info(msg: String) = append(LogLevel.INFO, msg)
|
|
fun warn(msg: String) = append(LogLevel.WARN, msg)
|
|
fun error(msg: String) = append(LogLevel.ERROR, msg)
|
|
fun success(msg: String) = append(LogLevel.SUCCESS, msg)
|
|
|
|
fun clear() {
|
|
_logs.value = emptyList()
|
|
}
|
|
} |