[Android] Logcat: send logcats via a callback

Thomas Guillem git at videolan.org
Tue Feb 10 13:36:01 CET 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Mon Feb  9 19:17:19 2015 +0100| [62b78e67e869a3b605475104ecfd244132e8658e] | committer: Thomas Guillem

Logcat: send logcats via a callback

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=62b78e67e869a3b605475104ecfd244132e8658e
---

 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..09f67d4 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) {
+        }
+    }
+
+    /**
+     * 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();
+            mProcess = null;
+        }
+        try {
+            mThread.join();
+        } catch (InterruptedException e) {
+        }
+        mThread = null;
+        mCallback = null;
+    }
 
     /**
      * Writes the current app logcat to a file.
@@ -94,5 +161,4 @@ public class Logcat {
 
         return log.toString();
     }
-
 }



More information about the Android mailing list