[Android] [PATCH 1/3] Logcat: send logcats via a callback

Thomas Guillem thomas at gllm.fr
Mon Feb 9 19:31:55 CET 2015


---
 vlc-android/src/org/videolan/vlc/util/Logcat.java | 72 ++++++++++++++++++++++-
 1 file changed, 69 insertions(+), 3 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/util/Logcat.java b/vlc-android/src/org/videolan/vlc/util/Logcat.java
index c9552b8..97c1bc6 100644
--- a/vlc-android/src/org/videolan/vlc/util/Logcat.java
+++ b/vlc-android/src/org/videolan/vlc/util/Logcat.java
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * Logcat.java
  *****************************************************************************
- * Copyright © 2011-2014 VLC authors and VideoLAN
+ * Copyright © 2011-2015 VLC authors and VideoLAN
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,8 +28,75 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 
-public class Logcat {
+public class Logcat implements Runnable {
     public final static String TAG = "VLC/Util/Logcat";
+    private Callback mCallback = null;
+    private Thread mThread = null;
+    private Process mProcess = null;
+    private boolean mRun = false;
+
+    public interface Callback {
+        public void onLog(String log);
+    }
+
+    public Logcat() {
+    }
+
+    @Override
+    public void run() {
+        final String[] args = { "logcat", "-v", "time" };
+
+        try {
+            synchronized (this) {
+                if (!mRun)
+                    return;
+                mProcess = Runtime.getRuntime().exec(args);
+            }
+            InputStreamReader input = new InputStreamReader(
+                    mProcess.getInputStream());
+            BufferedReader br = new BufferedReader(input);
+            String line;
+
+            while ((line = br.readLine()) != null)
+                mCallback.onLog(line);
+
+            br.close();
+            input.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Start a thread that will send logcat via a callback
+     * @param callback
+     */
+    public synchronized void start(Callback callback) {
+        if (callback == null)
+            throw new IllegalArgumentException("callback should not be null");
+        if (mThread != null || mProcess != null)
+            throw new IllegalStateException("logcat is already started");
+        mCallback = callback;
+        mRun = true;
+        mThread = new Thread(this);
+        mThread.start();
+    }
+
+    /**
+     * Stop the thread previously started
+     */
+    public synchronized void stop() {
+        mRun = false;
+        if (mProcess != null)
+            mProcess.destroy();
+        try {
+            mThread.join();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        mThread = null;
+        mCallback = null;
+    }
 
     /**
      * Writes the current app logcat to a file.
@@ -94,5 +161,4 @@ public class Logcat {
 
         return log.toString();
     }
-
 }
-- 
2.1.3



More information about the Android mailing list