From 70362456c0c2233d9f7b9bd62e71b06052035201 Mon Sep 17 00:00:00 2001 From: sober Date: Thu, 13 Jun 2024 19:24:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0log=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/lizongying/mytv/MainActivity.kt | 5 +++-- .../java/com/lizongying/mytv/logFile.java | 21 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/lizongying/mytv/MainActivity.kt b/app/src/main/java/com/lizongying/mytv/MainActivity.kt index 5295c83..8254943 100644 --- a/app/src/main/java/com/lizongying/mytv/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv/MainActivity.kt @@ -99,9 +99,9 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer } logFile.startCapturingToFile(outputFile) - Log.d("FileCreation", "File created: ${outputFile.absolutePath}") + Log.d(TAG, "File created: ${outputFile.absolutePath}") } catch (e: Exception) { - Log.e("FileCreation", "Error creating file", e) + Log.e(TAG, "Error creating file", e) } window.decorView.apply { @@ -659,6 +659,7 @@ class MainActivity : FragmentActivity(), Request.RequestListener, OnSharedPrefer private companion object { const val TAG = "MainActivity" + const val PERMISSION_REQUEST_CODE = 1 } override fun onSharedPreferenceChanged(key: String) { diff --git a/app/src/main/java/com/lizongying/mytv/logFile.java b/app/src/main/java/com/lizongying/mytv/logFile.java index ab08e13..3e161bc 100644 --- a/app/src/main/java/com/lizongying/mytv/logFile.java +++ b/app/src/main/java/com/lizongying/mytv/logFile.java @@ -1,19 +1,27 @@ package com.lizongying.mytv; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import android.util.Log; public class logFile { + private static final String TAG = "logFile"; // 日志标签定义 + public static void startCapturingToFile(File outputFile) { new Thread(() -> { + Process clearProcess = null; + Process logcatProcess = null; + try { - Process process = Runtime.getRuntime().exec("logcat -c"); // 清除当前的日志缓存 - process = Runtime.getRuntime().exec("logcat"); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - FileWriter writer = new FileWriter(outputFile, true); + // 清除当前logcat缓存 + clearProcess = Runtime.getRuntime().exec("logcat -c"); + + logcatProcess = Runtime.getRuntime().exec("logcat"); + BufferedReader reader = new BufferedReader(new InputStreamReader(logcatProcess.getInputStream())); + BufferedWriter writer =new BufferedWriter(new FileWriter(outputFile, true)); String line; while ((line = reader.readLine()) != null) { @@ -25,7 +33,10 @@ public class logFile { reader.close(); } catch (Exception e) { - Log.e("log", "Error startCapturingToFile", e); + Log.e(TAG, "Error capturing log to file", e); + } finally { + if (clearProcess != null) clearProcess.destroy(); + if (logcatProcess != null) logcatProcess.destroy(); } }).start(); }