Skip to content

Commit

Permalink
Merge pull request #339 from chkwok/preserve-logcat
Browse files Browse the repository at this point in the history
Move logcat collection to prevent pollution during crash data gathering
  • Loading branch information
william-ferguson-au committed Dec 16, 2015
2 parents 3f21616 + 8e794b7 commit a6660ae
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions src/main/java/org/acra/collector/CrashReportDataFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,49 @@ public CrashReportData createCrashData(String msg, Throwable th, Map<String, Str
ACRA.log.e(LOG_TAG, "Error while retrieving STACK_TRACE data", e);
}

// Collect DropBox and logcat. This is done first because some ROMs spam the log with every get on
// Settings.
final PackageManagerWrapper pm = new PackageManagerWrapper(context);

// Before JellyBean, this required the READ_LOGS permission
// Since JellyBean, READ_LOGS is not granted to third-party apps anymore for security reasons.
// Though, we can call logcat without any permission and still get traces related to our app.
final boolean hasReadLogsPermission = pm.hasPermission(Manifest.permission.READ_LOGS) || (Compatibility.getAPILevel() >= Compatibility.VERSION_CODES.JELLY_BEAN);
if (prefs.getBoolean(ACRA.PREF_ENABLE_SYSTEM_LOGS, true) && hasReadLogsPermission) {
ACRA.log.i(LOG_TAG, "READ_LOGS granted! ACRA can include LogCat and DropBox data.");
if (crashReportFields.contains(LOGCAT)) {
try {
crashReportData.put(LOGCAT, LogCatCollector.collectLogCat(null));
} catch (RuntimeException e){
ACRA.log.e(LOG_TAG, "Error while retrieving LOGCAT data", e);
}
}
if (crashReportFields.contains(EVENTSLOG)) {
try {
crashReportData.put(EVENTSLOG, LogCatCollector.collectLogCat("events"));
} catch (RuntimeException e){
ACRA.log.e(LOG_TAG, "Error while retrieving EVENTSLOG data", e);
}
}
if (crashReportFields.contains(RADIOLOG)) {
try {
crashReportData.put(RADIOLOG, LogCatCollector.collectLogCat("radio"));
} catch (RuntimeException e){
ACRA.log.e(LOG_TAG, "Error while retrieving RADIOLOG data", e);
}
}
if (crashReportFields.contains(DROPBOX)) {
try {
crashReportData.put(DROPBOX,
DropBoxCollector.read(context, ACRA.getConfig().additionalDropBoxTags()));
} catch (RuntimeException e){
ACRA.log.e(LOG_TAG, "Error while retrieving DROPBOX data", e);
}
}
} else {
ACRA.log.i(LOG_TAG, "READ_LOGS not allowed. ACRA will not include LogCat and DropBox data.");
}

try {
crashReportData.put(ReportField.USER_APP_START_DATE, ReportUtils.getTimeString(appStartDate));
} catch (RuntimeException e){
Expand Down Expand Up @@ -389,10 +432,8 @@ public CrashReportData createCrashData(String msg, Throwable th, Map<String, Str
}
}

// Now get all the crash data that relies on the PackageManager
// Now get all the crash data that relies on the PackageManager.getPackageInfo()
// (which may or may not be here).
final PackageManagerWrapper pm = new PackageManagerWrapper(context);

try {
final PackageInfo pi = pm.getPackageInfo();
if (pi != null) {
Expand Down Expand Up @@ -424,46 +465,6 @@ public CrashReportData createCrashData(String msg, Throwable th, Map<String, Str
}
}

// Collect DropBox and logcat
// Before JellyBean, this required the READ_LOGS permission
// Since JellyBean, READ_LOGS is not granted to third-party apps anymore for security reasons.
// Though, we can call logcat without any permission and still get traces related to our app.
final boolean hasReadLogsPermission = pm.hasPermission(Manifest.permission.READ_LOGS) || (Compatibility.getAPILevel() >= Compatibility.VERSION_CODES.JELLY_BEAN);
if (prefs.getBoolean(ACRA.PREF_ENABLE_SYSTEM_LOGS, true) && hasReadLogsPermission) {
ACRA.log.i(LOG_TAG, "READ_LOGS granted! ACRA can include LogCat and DropBox data.");
if (crashReportFields.contains(LOGCAT)) {
try {
crashReportData.put(LOGCAT, LogCatCollector.collectLogCat(null));
} catch (RuntimeException e){
ACRA.log.e(LOG_TAG, "Error while retrieving LOGCAT data", e);
}
}
if (crashReportFields.contains(EVENTSLOG)) {
try {
crashReportData.put(EVENTSLOG, LogCatCollector.collectLogCat("events"));
} catch (RuntimeException e){
ACRA.log.e(LOG_TAG, "Error while retrieving EVENTSLOG data", e);
}
}
if (crashReportFields.contains(RADIOLOG)) {
try {
crashReportData.put(RADIOLOG, LogCatCollector.collectLogCat("radio"));
} catch (RuntimeException e){
ACRA.log.e(LOG_TAG, "Error while retrieving RADIOLOG data", e);
}
}
if (crashReportFields.contains(DROPBOX)) {
try {
crashReportData.put(DROPBOX,
DropBoxCollector.read(context, ACRA.getConfig().additionalDropBoxTags()));
} catch (RuntimeException e){
ACRA.log.e(LOG_TAG, "Error while retrieving DROPBOX data", e);
}
}
} else {
ACRA.log.i(LOG_TAG, "READ_LOGS not allowed. ACRA will not include LogCat and DropBox data.");
}

// Application specific log file
if (crashReportFields.contains(APPLICATION_LOG)) {
try {
Expand Down

0 comments on commit a6660ae

Please sign in to comment.