增加log记录文件

pull/871/head^2
sober 2 years ago
parent 0b61cd79f8
commit 70362456c0
  1. 5
      app/src/main/java/com/lizongying/mytv/MainActivity.kt
  2. 21
      app/src/main/java/com/lizongying/mytv/logFile.java

@ -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) {

@ -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();
}

Loading…
Cancel
Save