Commit 62b78e67 authored by Thomas Guillem's avatar Thomas Guillem

Logcat: send logcats via a callback

parent cceb98ea
/*****************************************************************************
* 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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment