From c4efe14773ff8e8f0e607b2d8bd778caf9d5b520 Mon Sep 17 00:00:00 2001 From: tulioccalazans Date: Tue, 23 Jan 2018 15:47:25 -0200 Subject: [PATCH 1/2] New feature: Log only defined TAGs. --- build.gradle | 2 +- .../ereza/logcatreporter/LogcatReporter.java | 71 +++++++++++++++---- .../sample/SampleCrashingApplication.java | 17 +++-- .../sample/activity/MainActivity.java | 27 ++++++- 4 files changed, 95 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index 0f7be57..57c97e6 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { } } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0' + classpath 'com.android.tools.build:gradle:3.0.1' classpath 'io.fabric.tools:gradle:1.24.5' } } diff --git a/library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java b/library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java index a30300f..6b3da95 100644 --- a/library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java +++ b/library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java @@ -22,6 +22,7 @@ import java.io.BufferedReader; import java.io.InputStreamReader; +import java.util.ArrayList; public class LogcatReporter { @@ -30,28 +31,45 @@ public class LogcatReporter { private static final int DEFAULT_WAIT_TIME_IN_MILLIS = 500; private static final int DEFAULT_LINE_COUNT = 1000; + private static ArrayList tags; + private static int lineCount; public static void install() { - install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS); + install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, null); + } + + public static void install(ArrayList tags) { + install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, tags); } public static void install(int lineCount) { - install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS); + install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, null); } - public static void install(final int lineCount, final int waitTimeInMillis) { - LogcatReporter.lineCount = lineCount; - try { - Runtime.getRuntime().exec("logcat -c"); - Log.i(TAG, "Logs have been cleared."); - } catch (Throwable t) { - Log.e(TAG, "Could not clear logs, in case of crash, the logs may contain more info from past executions."); + public static void install(int lineCount, ArrayList tags) { + install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, tags); + } + + public static void install(final int lineCount, final int waitTimeInMillis, ArrayList tags) { + + LogcatReporter.tags = tags; + + if(tags != null && tags.size() > 0) { + tags.add(TAG); + tags.add("AndroidRuntime"); } + + LogcatReporter.lineCount = lineCount; + + clearLogCat(); + final Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler(); + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable ex) { + Log.i(TAG, "Crash detected, sending Logcat to Crashlytics!"); logLogcat(); try { //Sleep for a moment, try to let the Crashlytics log service catch up... @@ -63,9 +81,19 @@ public void uncaughtException(Thread thread, Throwable ex) { originalHandler.uncaughtException(thread, ex); } }); + Log.i(TAG, "LogcatReporter has been installed"); } + public static void clearLogCat() { + try { + Runtime.getRuntime().exec("logcat -c"); + Log.i(TAG, "Logs have been cleared."); + } catch (Throwable t) { + Log.e(TAG, "Could not clear logs, in case of crash, the logs may contain more info from past executions."); + } + } + public static void reportExceptionWithLogcat(Throwable t) { logLogcat(); Crashlytics.logException(t); @@ -74,16 +102,29 @@ public static void reportExceptionWithLogcat(Throwable t) { private static void logLogcat() { //Get the log (try at least) try { - Log.i(TAG, "Crash detected, sending Logcat to Crashlytics!"); - Process process = Runtime.getRuntime().exec("logcat -t " + lineCount + " -v threadtime"); - BufferedReader bufferedReader = new BufferedReader( - new InputStreamReader(process.getInputStream())); + + String command = "logcat -t " + lineCount + " -v threadtime"; + String regexTags = ""; + + if(tags != null && tags.size() > 0) { + //regexTags += " | grep -o -P '"; + for (int i = 0; i < tags.size(); i++) { + regexTags += ".*" + tags.get(i) + "[\\s]*:.*|"; + } + + regexTags = regexTags.substring(0, regexTags.length()-1); + //regexTags += "'"; + } + + Process process = Runtime.getRuntime().exec(command); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { - Crashlytics.log("|| " + line); + if(regexTags== "" || line.startsWith("-") || line.matches(regexTags)) + Crashlytics.log("|| " + line); } } catch (Throwable t) { - Crashlytics.log("(No log available, an error ocurred while getting it)"); + Crashlytics.log("(No log available, an error occurred while getting it)"); } } } diff --git a/sample/src/main/java/cat/ereza/logcatreporter/sample/SampleCrashingApplication.java b/sample/src/main/java/cat/ereza/logcatreporter/sample/SampleCrashingApplication.java index 517f690..ecff598 100644 --- a/sample/src/main/java/cat/ereza/logcatreporter/sample/SampleCrashingApplication.java +++ b/sample/src/main/java/cat/ereza/logcatreporter/sample/SampleCrashingApplication.java @@ -17,13 +17,20 @@ package cat.ereza.logcatreporter.sample; import android.app.Application; +import android.util.Log; import com.crashlytics.android.Crashlytics; +import java.util.ArrayList; +import java.util.Arrays; + import cat.ereza.logcatreporter.LogcatReporter; import io.fabric.sdk.android.Fabric; public class SampleCrashingApplication extends Application{ + + public static final String TAG = "CLTest"; + @Override public void onCreate() { super.onCreate(); @@ -32,10 +39,10 @@ public void onCreate() { Fabric.with(this, new Crashlytics()); //Install LogcatReporter - LogcatReporter.install(); - - //You can algo use the following: -// LogcatReporter.install(2000, 500); - + //LogcatReporter.install(); + //LogcatReporter.install(2000, 500); + //You can define some specific TAGs to log, otherwise all logs found will be logged + LogcatReporter.install(new ArrayList<>(Arrays.asList( TAG, "ActivityThread" ))); + Log.i(TAG, "Lc: LogcatReporter.install()"); } } diff --git a/sample/src/main/java/cat/ereza/logcatreporter/sample/activity/MainActivity.java b/sample/src/main/java/cat/ereza/logcatreporter/sample/activity/MainActivity.java index 53aadf7..5fcb14c 100644 --- a/sample/src/main/java/cat/ereza/logcatreporter/sample/activity/MainActivity.java +++ b/sample/src/main/java/cat/ereza/logcatreporter/sample/activity/MainActivity.java @@ -23,9 +23,13 @@ import android.widget.Button; import android.widget.Toast; +import com.crashlytics.android.Crashlytics; + import java.util.UUID; +import cat.ereza.logcatreporter.LogcatReporter; import cat.ereza.logcatreporter.sample.R; +import cat.ereza.logcatreporter.sample.SampleCrashingApplication; public class MainActivity extends Activity { @@ -41,7 +45,7 @@ protected void onCreate(Bundle savedInstanceState) { @Override public void onClick(View view) { String loggedString = "This is a test log! - " + UUID.randomUUID().toString(); - Log.e("TEST_TAG", loggedString); + Log.e(SampleCrashingApplication.TAG, loggedString); Toast.makeText(MainActivity.this, getString(R.string.logged_toast, loggedString), Toast.LENGTH_SHORT).show(); } }); @@ -52,5 +56,26 @@ public void onClick(View view) { throw new RuntimeException("Hi! I'm an exception and I made the app crash!"); } }); + + logUser(); + } + + private void logUser() { + // You can call any combination of these three methods + Crashlytics.setUserIdentifier("678910"); + Crashlytics.setUserEmail("user10@fabric.io"); + Crashlytics.setUserName("Test User 10"); + + // Or set some key/value manually + Crashlytics.setInt("CompanyId", 130); + + Log.i(SampleCrashingApplication.TAG, "Lc: CL settings OK"); + + // After submit to crashlytics, strings logged by Crashlytics.log are cleaned. + Crashlytics.log("User data saved."); + Crashlytics.log("Dados do usuário salvos... 8"); + LogcatReporter.reportExceptionWithLogcat(new Exception("This is not a crash, only an issue.")); + + Log.i(SampleCrashingApplication.TAG, "Lc: CL log OK"); } } \ No newline at end of file From 8866a879f7b40fa2fcbe58d94f4c55a5238fa5c2 Mon Sep 17 00:00:00 2001 From: Tulio Calazans Date: Thu, 25 Oct 2018 09:22:50 -0300 Subject: [PATCH 2/2] Improvements --- .../ereza/logcatreporter/LogcatReporter.java | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java b/library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java index 6b3da95..e5aa3e1 100644 --- a/library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java +++ b/library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java @@ -16,53 +16,73 @@ package cat.ereza.logcatreporter; +import android.text.TextUtils; import android.util.Log; - import com.crashlytics.android.Crashlytics; - import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; public class LogcatReporter { - private static final String TAG = "LogcatReporter"; + public static final String TAG = "LogcatReporter"; - private static final int DEFAULT_WAIT_TIME_IN_MILLIS = 500; - private static final int DEFAULT_LINE_COUNT = 1000; + private static final int DEFAULT_WAIT_TIME_IN_MILLIS = 1500; + private static final int DEFAULT_LINE_COUNT = 1500; + private static String packageName; private static ArrayList tags; private static int lineCount; + private static int waitTimeInMillis; public static void install() { - install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, null); + install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, null, null); + } + + public static void install(String packageName) { + install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, null, packageName); } public static void install(ArrayList tags) { - install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, tags); + install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, tags, null); + } + + public static void install(ArrayList tags, String packageName) { + install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, tags, packageName); } public static void install(int lineCount) { - install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, null); + install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, null, null); + } + + public static void install(int lineCount, String packageName) { + install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, null, packageName); } public static void install(int lineCount, ArrayList tags) { - install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, tags); + install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, tags, null); } - public static void install(final int lineCount, final int waitTimeInMillis, ArrayList tags) { + public static void install(int lineCount, ArrayList tags, String packageName) { + install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, tags, packageName); + } + public static void install(int lineCount, int waitTimeInMillis, ArrayList tags, String packageName) { + + LogcatReporter.packageName = packageName; LogcatReporter.tags = tags; if(tags != null && tags.size() > 0) { tags.add(TAG); tags.add("AndroidRuntime"); + tags.add("ActivityThread"); } LogcatReporter.lineCount = lineCount; + LogcatReporter.waitTimeInMillis = waitTimeInMillis; - clearLogCat(); + //clearLogCat(); final Thread.UncaughtExceptionHandler originalHandler = Thread.getDefaultUncaughtExceptionHandler(); @@ -70,10 +90,11 @@ public static void install(final int lineCount, final int waitTimeInMillis, Arra @Override public void uncaughtException(Thread thread, Throwable ex) { Log.i(TAG, "Crash detected, sending Logcat to Crashlytics!"); + Log.e(TAG, "UncaughtException detected.", ex); logLogcat(); try { //Sleep for a moment, try to let the Crashlytics log service catch up... - Thread.sleep(waitTimeInMillis); + Thread.sleep(LogcatReporter.waitTimeInMillis); } catch (Throwable t) { Log.e(TAG, "The reporting thread was interrupted, the log may be incomplete!"); } @@ -120,7 +141,7 @@ private static void logLogcat() { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { - if(regexTags== "" || line.startsWith("-") || line.matches(regexTags)) + if(regexTags== "" || line.startsWith("-") || ( !TextUtils.isEmpty(packageName) && line.contains(packageName) ) || line.matches(regexTags)) Crashlytics.log("|| " + line); } } catch (Throwable t) { @@ -128,3 +149,4 @@ private static void logLogcat() { } } } +