From 08244e218a3cd08bb1abd1be31776510eee5da6a Mon Sep 17 00:00:00 2001 From: steckbrief Date: Sun, 7 Jul 2019 21:21:12 +0200 Subject: implements FS#282: In-App Logcat View; introduces piratx application implementation to access the appcontext in a static way --- .../thedevstacklogcat/src/main/AndroidManifest.xml | 4 + .../de/thedevstack/android/logcat/Logging.java | 148 +++++++++++++++++++++ .../logcat/adapters/LogCatArrayAdapter.java | 126 ++++++++++++++++++ .../android/logcat/tasks/ReadLogCatAsyncTask.java | 132 ++++++++++++++++++ .../src/main/res/values/strings.xml | 3 + 5 files changed, 413 insertions(+) create mode 100644 libs/thedevstacklogcat/src/main/AndroidManifest.xml create mode 100644 libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/Logging.java create mode 100644 libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java create mode 100644 libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java create mode 100644 libs/thedevstacklogcat/src/main/res/values/strings.xml (limited to 'libs/thedevstacklogcat/src/main') diff --git a/libs/thedevstacklogcat/src/main/AndroidManifest.xml b/libs/thedevstacklogcat/src/main/AndroidManifest.xml new file mode 100644 index 000000000..594487086 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + diff --git a/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/Logging.java b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/Logging.java new file mode 100644 index 000000000..6af7a70e7 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/Logging.java @@ -0,0 +1,148 @@ +package de.thedevstack.android.logcat; + +import android.util.Log; + +/** + * Utility class to prefix every log tag. + * This can be used for better filtering in the log cat output activity. + */ +public class Logging { + /** + * The prefix for every log tag. + */ + protected static String LOG_TAG_PREFIX = "thedevstack."; + + /** + * Changes the default log tag prefix. + * The default value is thedevstack. + * @param logTagPrefix the new log tag prefix to use + */ + public static void initLogTagPrefix(String logTagPrefix) { + if (null != logTagPrefix) { + LOG_TAG_PREFIX = logTagPrefix; + } + } + + /** + * Returns the current log tag prefix. + * @return value of Logging.LOG_TAG_PREFIX + */ + public static String getLogTagPrefix() { + return LOG_TAG_PREFIX; + } + + /** + * Send a {@link Log#VERBOSE} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int v(String tag, String msg) { + return Log.v(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#VERBOSE} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int v(String tag, String msg, Throwable tr) { + return Log.v(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } + + /** + * Send a {@link Log#DEBUG} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int d(String tag, String msg) { + return Log.d(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#DEBUG} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int d(String tag, String msg, Throwable tr) { + return Log.d(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } + + /** + * Send an {@link Log#INFO} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int i(String tag, String msg) { + return Log.i(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#INFO} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int i(String tag, String msg, Throwable tr) { + return Log.i(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } + + /** + * Send a {@link Log#WARN} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int w(String tag, String msg) { + return Log.w(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#WARN} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int w(String tag, String msg, Throwable tr) { + return Log.w(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } + + /* + * Send a {@link #WARN} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param tr An exception to log + */ + public static int w(String tag, Throwable tr) { + return Log.w(Logging.LOG_TAG_PREFIX + tag, Log.getStackTraceString(tr)); + } + + /** + * Send an {@link Log#ERROR} log message. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + */ + public static int e(String tag, String msg) { + return Log.e(Logging.LOG_TAG_PREFIX + tag, msg); + } + + /** + * Send a {@link Log#ERROR} log message and log the exception. + * @param tag Used to identify the source of a log message. It usually identifies + * the class or activity where the log call occurs. + * @param msg The message you would like logged. + * @param tr An exception to log + */ + public static int e(String tag, String msg, Throwable tr) { + return Log.e(Logging.LOG_TAG_PREFIX + tag, msg + '\n' + Log.getStackTraceString(tr)); + } +} diff --git a/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java new file mode 100644 index 000000000..eb4efc983 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/adapters/LogCatArrayAdapter.java @@ -0,0 +1,126 @@ +package de.thedevstack.android.logcat.adapters; + +import android.content.Context; +import android.widget.ArrayAdapter; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Created by tzur on 20.11.2015. + */ +public class LogCatArrayAdapter extends ArrayAdapter { + private ArrayList logcatItems = new ArrayList<>(); + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a TextView to use when + */ + public LogCatArrayAdapter(Context context, int resource) { + super(context, resource); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a layout to use when + * instantiating views. + * @param textViewResourceId The id of the TextView within the layout resource to be populated + */ + public LogCatArrayAdapter(Context context, int resource, int textViewResourceId) { + super(context, resource, textViewResourceId); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a TextView to use when + * instantiating views. + * @param objects The objects to represent in the ListView. + */ + public LogCatArrayAdapter(Context context, int resource, String[] objects) { + super(context, resource, objects); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a layout to use when + * instantiating views. + * @param textViewResourceId The id of the TextView within the layout resource to be populated + * @param objects The objects to represent in the ListView. + */ + public LogCatArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) { + super(context, resource, textViewResourceId, objects); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a TextView to use when + * instantiating views. + * @param objects The objects to represent in the ListView. + */ + public LogCatArrayAdapter(Context context, int resource, List objects) { + super(context, resource, objects); + } + + /** + * Constructor + * + * @param context The current context. + * @param resource The resource ID for a layout file containing a layout to use when + * instantiating views. + * @param textViewResourceId The id of the TextView within the layout resource to be populated + * @param objects The objects to represent in the ListView. + */ + public LogCatArrayAdapter(Context context, int resource, int textViewResourceId, List objects) { + super(context, resource, textViewResourceId, objects); + } + + @Override + public void add(String object) { + super.add(object); + logcatItems.add(object); + } + + @Override + public void addAll(Collection collection) { + super.addAll(collection); + logcatItems.addAll(collection); + } + + @Override + public void addAll(String... items) { + super.addAll(items); + Collections.addAll(logcatItems, items); + } + + @Override + public void clear() { + super.clear(); + logcatItems.clear(); + } + + @Override + public void remove(String object) { + super.remove(object); + logcatItems.remove(object); + } + + /** + * Returns an unmodifiable copy of the log cat entries. + * @return UnmodifiableList of logcat entries. + */ + public List getItems() { + return Collections.unmodifiableList(this.logcatItems); + } +} 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 000000000..e16009ee3 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/java/de/thedevstack/android/logcat/tasks/ReadLogCatAsyncTask.java @@ -0,0 +1,132 @@ +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; +import de.thedevstack.android.logcat.adapters.LogCatArrayAdapter; + +/** + * 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: + *
    + *
  • {@value Logging#LOG_TAG_PREFIX}
  • + *
  • E/ - for every error message
  • + *
  • W/ - for every warning message
  • + *
+ */ +public class ReadLogCatAsyncTask extends AsyncTask { + /** + * The array adapter to publish the log messages to. + */ + private final LogCatArrayAdapter 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(LogCatArrayAdapter 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; + BufferedReader errorReader = null; + try { + Process process = Runtime.getRuntime().exec(ReadLogCatAsyncTask.LOG_CAT_CMD); + bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); + errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + logCatOutput.add(line); + } + + String errorLine = ""; + StringBuilder sb = new StringBuilder(); + while ((errorLine = errorReader.readLine()) != null) { + sb.append(errorLine); + sb.append('\n'); + } + int exitValue = process.waitFor(); + + Logging.d("ReadLogCat", "Logcat command returned with exitValue '" + exitValue + "'."); + + String errorMessage = sb.toString(); + if (0 != exitValue && !errorMessage.isEmpty()) { + Logging.e("ReadLogCat", errorMessage); + logCatOutput.add(errorMessage); + } + } catch (IOException e) { + Logging.e("ReadLogCat", "error while retrieving information from logcat: " + e.getMessage(), e); + } catch (InterruptedException e) { + Logging.e("ReadLogCat", "error while retrieving information from logcat: " + e.getMessage(), e); + } finally { + if (null != bufferedReader) { + try { + bufferedReader.close(); + } catch (IOException e) { + } + } + if (null != errorReader) { + try { + errorReader.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; + } +} diff --git a/libs/thedevstacklogcat/src/main/res/values/strings.xml b/libs/thedevstacklogcat/src/main/res/values/strings.xml new file mode 100644 index 000000000..45f5e7fd0 --- /dev/null +++ b/libs/thedevstacklogcat/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Copy + -- cgit v1.2.3