From 2fb31bcc3f9e4920891765ab8e0cf2e33227ea3e Mon Sep 17 00:00:00 2001 From: steckbrief Date: Thu, 17 Dec 2015 21:09:18 +0100 Subject: Log error in case logcat command could not be executed; Show toast in case nothing is copied --- .../android/logcat/tasks/ReadLogCatAsyncTask.java | 31 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (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 index 1136faf8..e16009ee 100644 --- 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 @@ -9,6 +9,7 @@ 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. @@ -25,7 +26,7 @@ public class ReadLogCatAsyncTask extends AsyncTask { /** * The array adapter to publish the log messages to. */ - private final ArrayAdapter arrayAdapter; + private final LogCatArrayAdapter arrayAdapter; /** * The command to execute logcat. */ @@ -39,7 +40,7 @@ public class ReadLogCatAsyncTask extends AsyncTask { * Initializes the Task with the array adapter to publish the log messages to. * @param arrayAdapter the array adapter */ - public ReadLogCatAsyncTask(ArrayAdapter arrayAdapter) { + public ReadLogCatAsyncTask(LogCatArrayAdapter arrayAdapter) { this.arrayAdapter = arrayAdapter; } @@ -52,15 +53,35 @@ public class ReadLogCatAsyncTask extends AsyncTask { 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 { @@ -68,6 +89,12 @@ public class ReadLogCatAsyncTask extends AsyncTask { } catch (IOException e) { } } + if (null != errorReader) { + try { + errorReader.close(); + } catch (IOException e) { + } + } } logCatOutput.trimToSize(); return logCatOutput.toArray(new String[0]); -- cgit v1.2.3