Skip to content

Commit

Permalink
add ability to attach logcat output to items, change default caught e…
Browse files Browse the repository at this point in the history
…xception level to warning, new config options - resolve #2 resolve #3
  • Loading branch information
sbezboro committed Sep 24, 2013
1 parent 42c30a8 commit a816aa4
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 31 deletions.
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Java library for reporting exceptions, errors, and log messages to [Rollbar](htt

## Setup ##

Download [rollbar-android.jar](https://github.com/rollbar/rollbar-android/releases/download/v0.0.2/rollbar-android-0.0.2.jar) and place it in your Android project's `libs` directory.
Download [rollbar-android.jar](https://github.com/rollbar/rollbar-android/releases/download/v0.0.4/rollbar-android-0.0.4.jar) and place it in your Android project's `libs` directory.

Add the following line in your custom Application subclass's `onCreate()` to initialize Rollbar:

Expand Down Expand Up @@ -36,19 +36,49 @@ Rollbar.reportMessage("A test message", "debug");

The following configuration methods are available:

* **Rollbar.setPersonData(String id, String username, String email)**

Sets the properties of the current user (called a "person" in Rollbar parlance) to be sent along with every report.

Default: all `null`


* **Rollbar.setEndpoint(String endpoint)**

Sets the base URL that items will be posted to.
Sets the endpoint URL that items will be posted to.

Default: `https://api.rollbar.com/api/1/items/`


* **Rollbar.setReportUncaughtExceptions(boolean report)**

Sets whether uncaught exceptions are reported to Rollbar or not.
Sets whether or not to report uncaught exceptions to Rollbar.

Default: `true`


* **Rollbar.setIncludeLogcat(boolean includeLogcat)**

Sets whether or not to include logcat output in reports to Rollbar.

Note: For devices with API level 15 and lower, you will need to include the `android.permission.READ_LOGS` permission in your app's `AndroidManifest.xml` for logcat collection to work.

Default: `false`


* **Rollbar.setDefaultCaughtExceptionLevel(String level)**

Sets the level caught exceptions are reported as by default.

Default: `warning`


* **Rollbar.setUncaughtExceptionLevel(String level)**

Sets the level uncaught exceptions are reported as.

Default: `error`

## Deobfuscation ##

If you use [ProGuard](http://developer.android.com/tools/help/proguard.html) to obfuscate your code in production, reported stack traces will not be very useful in most cases.
Expand Down
106 changes: 84 additions & 22 deletions src/main/java/com/rollbar/android/Notifier.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.rollbar.android;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -25,11 +29,12 @@
import com.rollbar.android.http.HttpResponseHandler;

public class Notifier {
private static final String NOTIFIER_VERSION = "0.0.3";
private static final String NOTIFIER_VERSION = "0.0.4";
private static final String DEFAULT_ENDPOINT = "https://api.rollbar.com/api/1/items/";
private static final String ITEM_DIR_NAME = "rollbar-items";

private static final int DEFAULT_ITEM_SCHEDULE_DELAY = 1;
private static final int MAX_LOGCAT_SIZE = 100;

private static int itemCounter = 0;

Expand All @@ -42,9 +47,11 @@ public class Notifier {
private String environment;

private JSONObject personData;

private String endpoint;
private boolean reportUncaughtExceptions;
private boolean includeLogcat;
private String defaultCaughtExceptionLevel;
private String uncaughtExceptionLevel;

private int versionCode;
private String versionName;
Expand All @@ -70,8 +77,11 @@ public Notifier(Context context, String accessToken, String environment) {
Log.e(Rollbar.TAG, "Error getting package info.");
}


endpoint = DEFAULT_ENDPOINT;
reportUncaughtExceptions = true;
defaultCaughtExceptionLevel = "warning";
uncaughtExceptionLevel = "error";

handlerScheduled = false;

Expand All @@ -85,28 +95,37 @@ public Notifier(Context context, String accessToken, String environment) {

scheduleItemFileHandler();
}

public void setPersonData(JSONObject personData) {
this.personData = personData;
}

public void setPersonData(String id, String username, String email) {
JSONObject personData = new JSONObject();

private JSONArray getLogcatInfo() {
JSONArray log = null;

int pid = android.os.Process.myPid();

try {
personData.put("id", id);
Process process = Runtime.getRuntime().exec("logcat -d");

if (username != null) {
personData.put("username", username);
}
if (email != null) {
personData.put("email", email);
InputStreamReader isr = new InputStreamReader(process.getInputStream());
BufferedReader br = new BufferedReader(isr, 8192);

List<String> lines = new ArrayList<String>();

String line;
while ((line = br.readLine()) != null) {
// Only include the line if the current process's pid is present
if (line.contains(String.valueOf(pid))) {
lines.add(line);
if (lines.size() > MAX_LOGCAT_SIZE) {
lines.remove(0);
}
}
}

this.personData = personData;
} catch (JSONException e) {
Log.e(Rollbar.TAG, "JSON error creating person data.", e);
log = new JSONArray(lines);
} catch (IOException e) {
Log.e(Rollbar.TAG, "Unable to collect logcat info.", e);
}

return log;
}

private JSONObject buildNotifierData() throws JSONException {
Expand All @@ -121,12 +140,18 @@ private JSONObject buildClientData() throws JSONException {
JSONObject client = new JSONObject();

client.put("timestamp", System.currentTimeMillis() / 1000);


JSONObject androidData = new JSONObject();
androidData.put("phone_model", android.os.Build.MODEL);
androidData.put("android_version", android.os.Build.VERSION.RELEASE);
androidData.put("code_version", this.versionCode);
androidData.put("version_name", this.versionName);

if (includeLogcat) {
androidData.put("logs", getLogcatInfo());
}

client.put("android", androidData);

return client;
Expand Down Expand Up @@ -172,8 +197,7 @@ private JSONArray loadItems(File file) {
StringBuffer content = new StringBuffer();

byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) != -1) {
while (in.read(buffer) != -1) {
content.append(new String(buffer));
}

Expand Down Expand Up @@ -286,7 +310,7 @@ public void run() {

public void uncaughtException(Throwable throwable) {
if (reportUncaughtExceptions) {
reportException(throwable, "error");
reportException(throwable, uncaughtExceptionLevel);

rollbarThread.interrupt();

Expand Down Expand Up @@ -342,6 +366,10 @@ public void reportException(Throwable throwable, String level) {
trace.put("frames", frames);
trace.put("exception", exceptionData);
body.put("trace", trace);

if (level == null) {
level = defaultCaughtExceptionLevel;
}

JSONObject data = buildData(level, body);
queueItem(data);
Expand All @@ -365,13 +393,47 @@ public void reportMessage(String message, String level) {
}
}

public void setPersonData(JSONObject personData) {
this.personData = personData;
}

public void setPersonData(String id, String username, String email) {
JSONObject personData = new JSONObject();

try {
personData.put("id", id);

if (username != null) {
personData.put("username", username);
}
if (email != null) {
personData.put("email", email);
}

this.personData = personData;
} catch (JSONException e) {
Log.e(Rollbar.TAG, "JSON error creating person data.", e);
}
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;

}

public void setReportUncaughtExceptions(boolean reportUncaughtExceptions) {
this.reportUncaughtExceptions = reportUncaughtExceptions;
}

public void setIncludeLogcat(boolean includeLogcat) {
this.includeLogcat = includeLogcat;
}

public void setDefaultCaughtExceptionLevel(String level) {
this.defaultCaughtExceptionLevel = level;
}

public void setUncaughtExceptionLevel(String level) {
this.uncaughtExceptionLevel = level;
}

}
44 changes: 38 additions & 6 deletions src/main/java/com/rollbar/android/Rollbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void run() {
}

public static void reportException(Throwable throwable) {
reportException(throwable, "error");
reportException(throwable, null);
}

public static void reportMessage(final String message, final String level) {
Expand All @@ -39,7 +39,23 @@ public void run() {
}

public static void reportMessage(String message) {
reportMessage(message, "error");
reportMessage(message, "info");
}

public static void setPersonData(final JSONObject personData) {
ensureInit(new Runnable() {
public void run() {
notifier.setPersonData(personData);
}
});
}

public static void setPersonData(final String id, final String username, final String email) {
ensureInit(new Runnable() {
public void run() {
notifier.setPersonData(id, username, email);
}
});
}

public static void setEndpoint(final String endpoint) {
Expand All @@ -58,12 +74,28 @@ public void run() {
});
}

public static void setPersonData(JSONObject personData) {
notifier.setPersonData(personData);
public static void setIncludeLogcat(final boolean includeLogcat) {
ensureInit(new Runnable() {
public void run() {
notifier.setIncludeLogcat(includeLogcat);
}
});
}

public static void setDefaultCaughtExceptionLevel(final String level) {
ensureInit(new Runnable() {
public void run() {
notifier.setDefaultCaughtExceptionLevel(level);
}
});
}

public static void setPersonData(String id, String username, String email) {
notifier.setPersonData(id, username, email);
public static void setUncaughtExceptionLevel(final String level) {
ensureInit(new Runnable() {
public void run() {
notifier.setUncaughtExceptionLevel(level);
}
});
}

private static void ensureInit(Runnable runnable) {
Expand Down

0 comments on commit a816aa4

Please sign in to comment.