Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Log only defined TAGs. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand Down
105 changes: 84 additions & 21 deletions library/src/main/java/cat/ereza/logcatreporter/LogcatReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,105 @@

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 = 1500;
private static final int DEFAULT_LINE_COUNT = 1500;

private static final int DEFAULT_WAIT_TIME_IN_MILLIS = 500;
private static final int DEFAULT_LINE_COUNT = 1000;
private static String packageName;
private static ArrayList<String> tags;

private static int lineCount;
private static int waitTimeInMillis;

public static void install() {
install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS);
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<String> tags) {
install(DEFAULT_LINE_COUNT, DEFAULT_WAIT_TIME_IN_MILLIS, tags, null);
}

public static void install(ArrayList<String> 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);
install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, null, 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, String packageName) {
install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, null, packageName);
}

public static void install(int lineCount, ArrayList<String> tags) {
install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, tags, null);
}

public static void install(int lineCount, ArrayList<String> tags, String packageName) {
install(lineCount, DEFAULT_WAIT_TIME_IN_MILLIS, tags, packageName);
}

public static void install(int lineCount, int waitTimeInMillis, ArrayList<String> 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();

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!");
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!");
}
//Let Crashlytics handle everything
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);
Expand All @@ -74,16 +123,30 @@ 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("-") || ( !TextUtils.isEmpty(packageName) && line.contains(packageName) ) || 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)");
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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();
}
});
Expand All @@ -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("[email protected]");
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");
}
}