From a7454223008c78dcf5e0ff727bca64241f99daa1 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Thu, 10 Dec 2015 20:01:50 +0100 Subject: Moved logcat to a module, increased error robustness for loading last messages --- .../android/logcat/tasks/ReadLogCatAsyncTask.java | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java (limited to 'libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java') diff --git a/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java new file mode 100644 index 00000000..1136faf8 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java @@ -0,0 +1,105 @@ +package de.thedevstack.android.logcat.tasks; + +import android.os.AsyncTask; +import android.widget.ArrayAdapter; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; + +import de.thedevstack.android.logcat.Logging; + +/** + * Task to read the logcat of the App. + * The command logcat -d -v time is used to load the logs. + * This reader uses a white list to restrict the messages to display, otherwise it might be flooded with useless log messages. + * The white list checks if a log messages contains one of the following strings: + * + */ +public class ReadLogCatAsyncTask extends AsyncTask { + /** + * The array adapter to publish the log messages to. + */ + private final ArrayAdapter arrayAdapter; + /** + * The command to execute logcat. + */ + private static final String[] LOG_CAT_CMD = { "logcat", "-d", "-v", "time"}; + /** + * The white list to filter log messages. + */ + private static final String[] WHITE_LIST = { Logging.getLogTagPrefix(), "E/", "W/" }; + + /** + * Initializes the Task with the array adapter to publish the log messages to. + * @param arrayAdapter the array adapter + */ + public ReadLogCatAsyncTask(ArrayAdapter arrayAdapter) { + this.arrayAdapter = arrayAdapter; + } + + /** + * Executes the logcat command, reads the output of the command and returns all log messages. + * @param params no params will be passed. (interface compliance) + * @return the array of log messages + */ + @Override + protected String[] doInBackground(Void... params) { + ArrayList logCatOutput = new ArrayList<>(); + BufferedReader bufferedReader = null; + try { + Process process = Runtime.getRuntime().exec(ReadLogCatAsyncTask.LOG_CAT_CMD); + bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + logCatOutput.add(line); + } + } catch (IOException e) { + Logging.e("ReadLogCat", "error while retrieving information from logcat: " + e.getMessage(), e); + } finally { + if (null != bufferedReader) { + try { + bufferedReader.close(); + } catch (IOException e) { + } + } + } + logCatOutput.trimToSize(); + return logCatOutput.toArray(new String[0]); + } + + /** + * Clears the array adapter and adds the filtered log messages. + * @param items all log messages + */ + @Override + protected void onPostExecute(String[] items) { + this.arrayAdapter.clear(); + if (null != items && items.length > 0) { + for (String item : items) { + if (!filter(item)) { + this.arrayAdapter.add(item); + } + } + } + } + + /** + * Checks whether a log message contains a white listed string or not. + * @param item the item to filter + * @return true if the string should be filtered (removed from the list) false otherwise. + */ + protected boolean filter(String item) { + for (String whiteListed : ReadLogCatAsyncTask.WHITE_LIST) { + if (item.contains(whiteListed)) { + return false; + } + } + return true; + } +} -- cgit v1.2.3