diff options
author | xC3FFF0E <78732474+xC3FFF0E@users.noreply.github.com> | 2023-02-28 21:34:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-28 21:34:36 +0100 |
commit | 567afbfc1b2e7d365bc155c7c01bc84031e6eab0 (patch) | |
tree | 7d94f09b6299620c822af2776948114033432726 /app/src/main/java/com/xc3fff0e/xmanager/xManagerLogger.java | |
parent | Update build.gradle (diff) | |
download | xManager-567afbfc1b2e7d365bc155c7c01bc84031e6eab0.tar xManager-567afbfc1b2e7d365bc155c7c01bc84031e6eab0.tar.gz xManager-567afbfc1b2e7d365bc155c7c01bc84031e6eab0.tar.bz2 xManager-567afbfc1b2e7d365bc155c7c01bc84031e6eab0.tar.lz xManager-567afbfc1b2e7d365bc155c7c01bc84031e6eab0.tar.xz xManager-567afbfc1b2e7d365bc155c7c01bc84031e6eab0.tar.zst xManager-567afbfc1b2e7d365bc155c7c01bc84031e6eab0.zip |
Diffstat (limited to 'app/src/main/java/com/xc3fff0e/xmanager/xManagerLogger.java')
-rw-r--r-- | app/src/main/java/com/xc3fff0e/xmanager/xManagerLogger.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/app/src/main/java/com/xc3fff0e/xmanager/xManagerLogger.java b/app/src/main/java/com/xc3fff0e/xmanager/xManagerLogger.java new file mode 100644 index 0000000..24e49ff --- /dev/null +++ b/app/src/main/java/com/xc3fff0e/xmanager/xManagerLogger.java @@ -0,0 +1,71 @@ +package com.xc3fff0e.xmanager; + +import android.content.Intent; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import android.content.Context; +import android.util.Log; + +public class xManagerLogger { + + private static Thread loggerThread = new Thread() { + @Override + public void run() { + isRunning = true; + + try { + Runtime.getRuntime().exec("logcat -c"); + Process process = Runtime.getRuntime().exec("logcat"); + + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { + String logTxt = bufferedReader.readLine(); + do { + broadcastLog(logTxt); + } while (isRunning && ((logTxt = bufferedReader.readLine()) != null)); + + // Thread got stopped, restart if not stopping wantedly + if (isRunning) { + broadcastLog("Logger got killed. Restarting."); + startLogging(); + } else { + broadcastLog("Logger stopped."); + } + } + } catch (Exception e) { + broadcastLog(e.toString()); + } + } + }; + + private static volatile boolean isRunning = false; + + public static void startLogging() { + if (!isRunning) { + loggerThread.start(); + } else { + throw new IllegalStateException("Logger already running"); + } + } + + public static void broadcastLog(String log) { + Context context = xManager.getContext(); + + Intent intent = new Intent(); + intent.setAction("com.xc3fff0e.xmanager.ACTION_NEW_DEBUG_LOG"); + intent.putExtra("log", log); + intent.putExtra("packageName", context.getPackageName()); + context.sendBroadcast(intent); + } + + public static void stopLogging() { + if (isRunning) { + isRunning = false; + broadcastLog("Stopping logger by user request."); + } else { + throw new IllegalStateException("Logger not running"); + } + } +} |