1.0 能用
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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
|
||||
)
|
||||
|
||||
/**
|
||||
* 全局日志容器:
|
||||
* - Android 侧各处直接调用 info/warn/error/success
|
||||
* - Rust 侧通过 JNI 静态方法 VpnBridge.nativeLog(String) 回调进来
|
||||
* - UI 用 LazyColumn 消费 StateFlow,最多保留 MAX 条
|
||||
*/
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user