Skip to content

Instantly share code, notes, and snippets.

@bonnyfone
Created February 10, 2015 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonnyfone/11e7f3a89aa2d10a689e to your computer and use it in GitHub Desktop.
Save bonnyfone/11e7f3a89aa2d10a689e to your computer and use it in GitHub Desktop.
LogcatMonitor concept
package com.designfuture.music.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;
import com.musixmatch.android.util.LogHelper;
/**
* Versatile logcat monitor
* @author ziby
*
*/
public class LogcatMonitor {
private static final String TAG = "LogcatMonitor";
/**
* Interface to implement in order to get notified
* @author ziby
*/
public static interface LogcatMonitorCallback{
public void onLogged(LogcatPattern what);
}
/**
* Logcat pattern to catch
* @author ziby
*/
public static class LogcatPattern{
public static enum LogcatPatternLevel {
VERBOSE("v"), DEBUG("d"), INFO("i"), WARNING("w"), ERROR("e"), FATAL("f");
private final String text;
private LogcatPatternLevel(final String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
private ArrayList<Pattern> patterns;
private LogcatPatternLevel level;
private String tag;
public LogcatPattern(String tag, LogcatPatternLevel logcatLevel){
patterns = new ArrayList<Pattern>();
this.level = logcatLevel;
this.tag = tag;
}
public void addRegexPattern(Pattern p){
patterns.add(p);
}
public void clearRegexPatterns(){
patterns.clear();
}
public String getFilter(){
return (tag == null ? "*" : tag) + ":" + level;
}
public boolean match(String againstString){
if(againstString == null || patterns == null || patterns.size() == 0)
return false;
for(Pattern p : patterns){
if(againstString.matches(p.pattern()))
return true;
}
return false;
}
}
/**
* Interna worker thread
* @author ziby
*/
private class MonitorThread extends Thread{
@Override
public void run() {
try {
ArrayList<String> filterlist = new ArrayList<String>();
filterlist.add("/system/bin/logcat");
filterlist.add("-s");
if(patterns != null)
for(LogcatPattern p : patterns)
filterlist.add(p.getFilter());
//filterlist.add("*:s");
ProcessBuilder builder = new ProcessBuilder(filterlist);
Process process = builder.start();
//Process process = Runtime.getRuntime().exec(filterlist.toArray(new String[filterlist.size()]));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = "";
while (!isInterrupted() && (line = bufferedReader.readLine()) != null) {
if(patterns != null){
synchronized (this) {
for(LogcatPattern lp : patterns){
if(lp.match(line)){
if(callback != null && callback.get() != null)
callback.get().onLogged(lp);
}
}
}
}
}
LogHelper.i(TAG, "Ended monitoring");
} catch (Exception e) {
LogHelper.w(TAG, "Error while monitoring logcat!",e);
}
}
}
private WeakReference<LogcatMonitorCallback> callback;
private MonitorThread monitor;
private ArrayList<LogcatPattern> patterns;
public LogcatMonitor(LogcatMonitorCallback callback){
this.callback = new WeakReference<LogcatMonitor.LogcatMonitorCallback>(callback);
patterns = new ArrayList<LogcatMonitor.LogcatPattern>();
}
public synchronized void addLogcatPattern(LogcatPattern pattern){
patterns.add(pattern);
}
public synchronized void clearLogcatPatterns(){
patterns.clear();
}
public synchronized void start(){
stop();
monitor = new MonitorThread();
monitor.start();
}
public synchronized void stop(){
if(monitor != null){
if(monitor.isAlive())
monitor.interrupt();
}
monitor = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment