Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit a816aa4

Browse files
committed
add ability to attach logcat output to items, change default caught exception level to warning, new config options - resolve #2 resolve #3
1 parent 42c30a8 commit a816aa4

File tree

3 files changed

+155
-31
lines changed

3 files changed

+155
-31
lines changed

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Java library for reporting exceptions, errors, and log messages to [Rollbar](htt
55

66
## Setup ##
77

8-
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.
8+
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.
99

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

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

3737
The following configuration methods are available:
3838

39+
* **Rollbar.setPersonData(String id, String username, String email)**
40+
41+
Sets the properties of the current user (called a "person" in Rollbar parlance) to be sent along with every report.
42+
43+
Default: all `null`
44+
45+
3946
* **Rollbar.setEndpoint(String endpoint)**
4047

41-
Sets the base URL that items will be posted to.
48+
Sets the endpoint URL that items will be posted to.
4249

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

4552

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

48-
Sets whether uncaught exceptions are reported to Rollbar or not.
55+
Sets whether or not to report uncaught exceptions to Rollbar.
4956

5057
Default: `true`
5158

59+
60+
* **Rollbar.setIncludeLogcat(boolean includeLogcat)**
61+
62+
Sets whether or not to include logcat output in reports to Rollbar.
63+
64+
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.
65+
66+
Default: `false`
67+
68+
69+
* **Rollbar.setDefaultCaughtExceptionLevel(String level)**
70+
71+
Sets the level caught exceptions are reported as by default.
72+
73+
Default: `warning`
74+
75+
76+
* **Rollbar.setUncaughtExceptionLevel(String level)**
77+
78+
Sets the level uncaught exceptions are reported as.
79+
80+
Default: `error`
81+
5282
## Deobfuscation ##
5383

5484
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.

src/main/java/com/rollbar/android/Notifier.java

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.rollbar.android;
22

3+
import java.io.BufferedReader;
34
import java.io.ByteArrayOutputStream;
45
import java.io.File;
56
import java.io.FileInputStream;
67
import java.io.FileNotFoundException;
78
import java.io.FileWriter;
89
import java.io.IOException;
10+
import java.io.InputStreamReader;
911
import java.io.PrintStream;
12+
import java.util.ArrayList;
13+
import java.util.List;
1014
import java.util.concurrent.Executors;
1115
import java.util.concurrent.ScheduledExecutorService;
1216
import java.util.concurrent.TimeUnit;
@@ -25,11 +29,12 @@
2529
import com.rollbar.android.http.HttpResponseHandler;
2630

2731
public class Notifier {
28-
private static final String NOTIFIER_VERSION = "0.0.3";
32+
private static final String NOTIFIER_VERSION = "0.0.4";
2933
private static final String DEFAULT_ENDPOINT = "https://api.rollbar.com/api/1/items/";
3034
private static final String ITEM_DIR_NAME = "rollbar-items";
3135

3236
private static final int DEFAULT_ITEM_SCHEDULE_DELAY = 1;
37+
private static final int MAX_LOGCAT_SIZE = 100;
3338

3439
private static int itemCounter = 0;
3540

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

4449
private JSONObject personData;
45-
4650
private String endpoint;
4751
private boolean reportUncaughtExceptions;
52+
private boolean includeLogcat;
53+
private String defaultCaughtExceptionLevel;
54+
private String uncaughtExceptionLevel;
4855

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

80+
7381
endpoint = DEFAULT_ENDPOINT;
7482
reportUncaughtExceptions = true;
83+
defaultCaughtExceptionLevel = "warning";
84+
uncaughtExceptionLevel = "error";
7585

7686
handlerScheduled = false;
7787

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

8696
scheduleItemFileHandler();
8797
}
88-
89-
public void setPersonData(JSONObject personData) {
90-
this.personData = personData;
91-
}
92-
93-
public void setPersonData(String id, String username, String email) {
94-
JSONObject personData = new JSONObject();
98+
99+
private JSONArray getLogcatInfo() {
100+
JSONArray log = null;
101+
102+
int pid = android.os.Process.myPid();
95103

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

99-
if (username != null) {
100-
personData.put("username", username);
101-
}
102-
if (email != null) {
103-
personData.put("email", email);
107+
InputStreamReader isr = new InputStreamReader(process.getInputStream());
108+
BufferedReader br = new BufferedReader(isr, 8192);
109+
110+
List<String> lines = new ArrayList<String>();
111+
112+
String line;
113+
while ((line = br.readLine()) != null) {
114+
// Only include the line if the current process's pid is present
115+
if (line.contains(String.valueOf(pid))) {
116+
lines.add(line);
117+
if (lines.size() > MAX_LOGCAT_SIZE) {
118+
lines.remove(0);
119+
}
120+
}
104121
}
105122

106-
this.personData = personData;
107-
} catch (JSONException e) {
108-
Log.e(Rollbar.TAG, "JSON error creating person data.", e);
123+
log = new JSONArray(lines);
124+
} catch (IOException e) {
125+
Log.e(Rollbar.TAG, "Unable to collect logcat info.", e);
109126
}
127+
128+
return log;
110129
}
111130

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

123142
client.put("timestamp", System.currentTimeMillis() / 1000);
143+
124144

125145
JSONObject androidData = new JSONObject();
126146
androidData.put("phone_model", android.os.Build.MODEL);
127147
androidData.put("android_version", android.os.Build.VERSION.RELEASE);
128148
androidData.put("code_version", this.versionCode);
129149
androidData.put("version_name", this.versionName);
150+
151+
if (includeLogcat) {
152+
androidData.put("logs", getLogcatInfo());
153+
}
154+
130155
client.put("android", androidData);
131156

132157
return client;
@@ -172,8 +197,7 @@ private JSONArray loadItems(File file) {
172197
StringBuffer content = new StringBuffer();
173198

174199
byte[] buffer = new byte[1024];
175-
int length;
176-
while ((length = in.read(buffer)) != -1) {
200+
while (in.read(buffer) != -1) {
177201
content.append(new String(buffer));
178202
}
179203

@@ -286,7 +310,7 @@ public void run() {
286310

287311
public void uncaughtException(Throwable throwable) {
288312
if (reportUncaughtExceptions) {
289-
reportException(throwable, "error");
313+
reportException(throwable, uncaughtExceptionLevel);
290314

291315
rollbarThread.interrupt();
292316

@@ -342,6 +366,10 @@ public void reportException(Throwable throwable, String level) {
342366
trace.put("frames", frames);
343367
trace.put("exception", exceptionData);
344368
body.put("trace", trace);
369+
370+
if (level == null) {
371+
level = defaultCaughtExceptionLevel;
372+
}
345373

346374
JSONObject data = buildData(level, body);
347375
queueItem(data);
@@ -365,13 +393,47 @@ public void reportMessage(String message, String level) {
365393
}
366394
}
367395

396+
public void setPersonData(JSONObject personData) {
397+
this.personData = personData;
398+
}
399+
400+
public void setPersonData(String id, String username, String email) {
401+
JSONObject personData = new JSONObject();
402+
403+
try {
404+
personData.put("id", id);
405+
406+
if (username != null) {
407+
personData.put("username", username);
408+
}
409+
if (email != null) {
410+
personData.put("email", email);
411+
}
412+
413+
this.personData = personData;
414+
} catch (JSONException e) {
415+
Log.e(Rollbar.TAG, "JSON error creating person data.", e);
416+
}
417+
}
418+
368419
public void setEndpoint(String endpoint) {
369420
this.endpoint = endpoint;
370-
371421
}
372422

373423
public void setReportUncaughtExceptions(boolean reportUncaughtExceptions) {
374424
this.reportUncaughtExceptions = reportUncaughtExceptions;
375425
}
426+
427+
public void setIncludeLogcat(boolean includeLogcat) {
428+
this.includeLogcat = includeLogcat;
429+
}
430+
431+
public void setDefaultCaughtExceptionLevel(String level) {
432+
this.defaultCaughtExceptionLevel = level;
433+
}
434+
435+
public void setUncaughtExceptionLevel(String level) {
436+
this.uncaughtExceptionLevel = level;
437+
}
376438

377439
}

src/main/java/com/rollbar/android/Rollbar.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void run() {
2727
}
2828

2929
public static void reportException(Throwable throwable) {
30-
reportException(throwable, "error");
30+
reportException(throwable, null);
3131
}
3232

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

4141
public static void reportMessage(String message) {
42-
reportMessage(message, "error");
42+
reportMessage(message, "info");
43+
}
44+
45+
public static void setPersonData(final JSONObject personData) {
46+
ensureInit(new Runnable() {
47+
public void run() {
48+
notifier.setPersonData(personData);
49+
}
50+
});
51+
}
52+
53+
public static void setPersonData(final String id, final String username, final String email) {
54+
ensureInit(new Runnable() {
55+
public void run() {
56+
notifier.setPersonData(id, username, email);
57+
}
58+
});
4359
}
4460

4561
public static void setEndpoint(final String endpoint) {
@@ -58,12 +74,28 @@ public void run() {
5874
});
5975
}
6076

61-
public static void setPersonData(JSONObject personData) {
62-
notifier.setPersonData(personData);
77+
public static void setIncludeLogcat(final boolean includeLogcat) {
78+
ensureInit(new Runnable() {
79+
public void run() {
80+
notifier.setIncludeLogcat(includeLogcat);
81+
}
82+
});
83+
}
84+
85+
public static void setDefaultCaughtExceptionLevel(final String level) {
86+
ensureInit(new Runnable() {
87+
public void run() {
88+
notifier.setDefaultCaughtExceptionLevel(level);
89+
}
90+
});
6391
}
6492

65-
public static void setPersonData(String id, String username, String email) {
66-
notifier.setPersonData(id, username, email);
93+
public static void setUncaughtExceptionLevel(final String level) {
94+
ensureInit(new Runnable() {
95+
public void run() {
96+
notifier.setUncaughtExceptionLevel(level);
97+
}
98+
});
6799
}
68100

69101
private static void ensureInit(Runnable runnable) {

0 commit comments

Comments
 (0)