屏蔽 logcat中某个tag的方法 tag:^(?!TAG)

本文探讨了深度学习在图像处理领域的应用,包括AR特效、图像处理、音视频直播等场景,展示了深度学习如何提升图像处理的效率与效果。
tag:^(?!IMGSRV) tag:^(?!dalvikvm) tag:^(?!mdpservice) tag:^(?!inputm)   
如何实现能一次添加多个tag,告诉我改哪些地方以及如何调用即可,不用给出全部代码 ```kotlin import android.content.Context import android.content.Intent import android.os.Process import android.util.Log import java.io.* import java.text.SimpleDateFormat import java.util.* import java.util.concurrent.* import java.util.concurrent.atomic.AtomicBoolean import kotlin.math.max class LogcatHelper private constructor( private val context: Context, private val filterTag: String? = null, private val minLevel: LogPriority = LogPriority.VERBOSE, // 默认最低级别 private val pidFilterEnabled: Boolean = true // 是否只捕获本进程 ) { private val logDir: String private var logFile: File? = null private var outputStream: FileOutputStream? = null private var process: Process? = null private val isRunning = AtomicBoolean(false) private val executorService = Executors.newSingleThreadExecutor() private val pid = if (pidFilterEnabled) Process.myPid() else -1 init { val appContext = context.applicationContext logDir = "${appContext.filesDir}/logs" val dirFile = File(logDir) if (!dirFile.exists()) { dirFile.mkdirs() } Log.d("LogcatHelper", "Log directory: $logDir") } fun start() { if (isRunning.getAndSet(true)) return executorService.execute { writeLogcatToFile() } } fun stop() { isRunning.set(false) process?.destroy() try { executorService.shutdown() if (!executorService.awaitTermination(3, TimeUnit.SECONDS)) { executorService.shutdownNow() } } catch (e: InterruptedException) { executorService.shutdownNow() Thread.currentThread().interrupt() } } private fun writeLogcatToFile() { var reader: BufferedReader? = null var out: FileOutputStream? = null try { val fileName = "Logcat-${getFileName()}.log" logFile = File(logDir, fileName) out = FileOutputStream(logFile) outputStream = out // 构建 logcat 命令 val cmdList = mutableListOf("logcat", "-v", "threadtime") // 添加级别过滤(仅支持最高级别以上) when (minLevel) { LogPriority.VERBOSE -> cmdList += "*:V" LogPriority.DEBUG -> cmdList += "*:D" LogPriority.INFO -> cmdList += "*:I" LogPriority.WARN -> cmdList += "*:W" LogPriority.ERROR -> cmdList += "*:E" LogPriority.ASSERT -> cmdList += "*:A" } val pb = ProcessBuilder(cmdList) pb.redirectErrorStream(true) process = pb.start() reader = BufferedReader(InputStreamReader(process!!.inputStream)) while (isRunning.get()) { val line = reader.readLine() ?: break if (line.isEmpty()) continue // 1. 如果启用了 PID 过滤,检查是否属于当前进程 val hasPid = pid != -1 && (line.contains("($pid)") || line.contains(pid.toString())) // 2. 如果设置了 tag,检查是否包含该 tag val hasTag = filterTag == null || extractTagFromLogLine(line)?.contains(filterTag, true) == true // 3. 检查日志级别是否 >= 设定的最小级别 val lineLevel = extractLevelFromLogLine(line) val meetsLevel = lineLevel != null && lineLevel >= minLevel if ((pid == -1 || hasPid) && hasTag && (minLevel == LogPriority.VERBOSE || meetsLevel)) { val outputLine = "${getFileName()} $line\n" out.write(outputLine.toByteArray()) out.flush() // 发送广播 val intent = Intent("FloatUtilData") intent.putExtra("extra_float_util_data", line) context.sendBroadcast(intent) } } } catch (e: IOException) { Log.e("LogcatHelper", "Error reading logcat", e) } finally { reader?.closeQuietly() runCatching { out?.close() } process = null outputStream = null } } // 解析日志行中的 tag(格式示例:01-01 12:00:00.000 I/MyTag ( 1234): Hello) private fun extractTagFromLogLine(line: String): String? { val match = Regex("""^[\d-]+\s+[\d:.]+\s+[VDIWEA]/([^:\s]+)""").find(line) return match?.groupValues?.getOrNull(1) } // 提取日志级别并映射为枚举 private fun extractLevelFromLogLine(line: String): LogPriority? { return when (line.trimStart().firstOrNull()) { 'V' -> LogPriority.VERBOSE 'D' -> LogPriority.DEBUG 'I' -> LogPriority.INFO 'W' -> LogPriority.WARN 'E' -> LogPriority.ERROR 'A' -> LogPriority.ASSERT else -> null } } companion object { @Volatile private var INSTANCE: LogcatHelper? = null fun getInstance(context: Context): LogcatHelper { return INSTANCE ?: synchronized(this) { INSTANCE ?: throw IllegalStateException("LogcatHelper not initialized. Use Builder to create.") } } private fun getFileName(): String { val format = SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US) return format.format(Date()) } private fun Reader?.closeQuietly() { this?.close() } } // === 建造者模式 === class Builder(private val context: Context) { private var filterTag: String? = null private var minLevel: LogPriority = LogPriority.VERBOSE private var pidFilterEnabled: Boolean = true fun filterTag(tag: String): Builder { this.filterTag = tag return this } fun filterLevel(level: LogPriority): Builder { this.minLevel = level return this } fun disablePidFilter(): Builder { this.pidFilterEnabled = false return this } fun build(): LogcatHelper { return LogcatHelper(context, filterTag, minLevel, pidFilterEnabled).also { helper -> INSTANCE = helper // 更新单例引用 } } } } // 日志级别枚举(与 Android Log 一致) enum class LogPriority(val priority: Int) : Comparable<LogPriority> { VERBOSE(2), DEBUG(3), INFO(4), WARN(5), ERROR(6), ASSERT(7); override fun compareTo(other: LogPriority): Int { return this.priority - other.priority } } ```
最新发布
12-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值