-
Notifications
You must be signed in to change notification settings - Fork 205
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
Use v2 API format #111
Use v2 API format #111
Changes from 8 commits
166ce57
958766c
0f0c27a
9f21085
bc6cbfe
a2c51be
6f95a06
44e2f85
90a5568
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
all: build | ||
|
||
.PHONY: build test clean | ||
|
||
build: | ||
./gradlew build | ||
|
||
clean: | ||
./gradlew clean | ||
|
||
test: | ||
./gradlew :connectedCheck |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.bugsnag.android; | ||
|
||
/** | ||
* Recognized types of breadcrumbs | ||
*/ | ||
public enum BreadcrumbType { | ||
/** | ||
* An error was sent to Bugsnag (internal use only) | ||
*/ | ||
ERROR ("error"), | ||
/** | ||
* A log message | ||
*/ | ||
LOG ("log"), | ||
/** | ||
* A manual invocation of `leaveBreadcrumb` (default) | ||
*/ | ||
MANUAL ("manual"), | ||
/** | ||
* A navigation event, such as a window opening or closing | ||
*/ | ||
NAVIGATION ("navigation"), | ||
/** | ||
* A background process such as a database query | ||
*/ | ||
PROCESS ("process"), | ||
/** | ||
* A network request | ||
*/ | ||
REQUEST ("request"), | ||
/** | ||
* A change in application state, such as launch or memory warning | ||
*/ | ||
STATE ("state"), | ||
/** | ||
* A user action, such as tapping a button | ||
*/ | ||
USER ("user"); | ||
|
||
private final String type; | ||
|
||
BreadcrumbType(String type) { | ||
this.type = type; | ||
} | ||
|
||
String serialize() { return type; } | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,45 +3,89 @@ | |
import android.support.annotation.NonNull; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.util.Date; | ||
import java.util.Queue; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
|
||
class Breadcrumbs implements JsonStream.Streamable { | ||
private static class Breadcrumb { | ||
private static class Breadcrumb implements JsonStream.Streamable { | ||
private static final int MAX_MESSAGE_LENGTH = 140; | ||
private static final String DEFAULT_NAME = "manual"; | ||
private static final String MESSAGE_METAKEY = "message"; | ||
private final String TIMESTAMP_KEY = "timestamp"; | ||
private final String NAME_KEY = "name"; | ||
private final String METADATA_KEY = "metaData"; | ||
private final String TYPE_KEY = "type"; | ||
final String timestamp; | ||
final String message; | ||
final String name; | ||
final BreadcrumbType type; | ||
final HashMap<String, String> metadata; | ||
|
||
Breadcrumb(@NonNull String message) { | ||
this.timestamp = DateUtils.toISO8601(new Date()); | ||
this.message = message.substring(0, Math.min(message.length(), MAX_MESSAGE_LENGTH)); | ||
this.type = BreadcrumbType.MANUAL; | ||
HashMap<String, String> metadata = new HashMap<String, String>(); | ||
metadata.put(MESSAGE_METAKEY, | ||
message.substring(0, Math.min(message.length(), MAX_MESSAGE_LENGTH))); | ||
this.metadata = metadata; | ||
this.name = DEFAULT_NAME; | ||
} | ||
|
||
Breadcrumb(@NonNull String name, BreadcrumbType type, HashMap<String, String> metadata) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.timestamp = DateUtils.toISO8601(new Date()); | ||
this.type = type; | ||
this.metadata = metadata; | ||
this.name = name; | ||
} | ||
|
||
public void toStream(@NonNull JsonStream writer) throws IOException { | ||
writer.beginObject(); | ||
writer.name(TIMESTAMP_KEY).value(this.timestamp); | ||
writer.name(NAME_KEY).value(this.name); | ||
writer.name(TYPE_KEY).value(this.type.serialize()); | ||
writer.name(METADATA_KEY); | ||
writer.beginObject(); | ||
for (Map.Entry<String, String> entry : this.metadata.entrySet()) { | ||
writer.name(entry.getKey()).value(entry.getValue()); | ||
} | ||
writer.endObject(); | ||
writer.endObject(); | ||
} | ||
|
||
public int payloadSize() throws IOException { | ||
StringWriter writer = new StringWriter(); | ||
JsonStream jsonStream = new JsonStream(writer); | ||
toStream(jsonStream); | ||
|
||
return writer.toString().length(); | ||
} | ||
} | ||
|
||
private static final int DEFAULT_MAX_SIZE = 20; | ||
private static final int MAX_PAYLOAD_SIZE = 4096; | ||
private final Queue<Breadcrumb> store = new ConcurrentLinkedQueue<>(); | ||
private int maxSize = DEFAULT_MAX_SIZE; | ||
|
||
public void toStream(@NonNull JsonStream writer) throws IOException { | ||
writer.beginArray(); | ||
|
||
for (Breadcrumb breadcrumb : store) { | ||
writer.beginArray(); | ||
writer.value(breadcrumb.timestamp); | ||
writer.value(breadcrumb.message); | ||
writer.endArray(); | ||
breadcrumb.toStream(writer); | ||
} | ||
|
||
writer.endArray(); | ||
} | ||
|
||
void add(@NonNull String message) { | ||
if (store.size() >= maxSize) { | ||
// Remove oldest breadcrumb | ||
store.poll(); | ||
} | ||
store.add(new Breadcrumb(message)); | ||
addToStore(new Breadcrumb(message)); | ||
} | ||
|
||
void add(@NonNull String name, BreadcrumbType type, HashMap<String, String> metadata) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
addToStore(new Breadcrumb(name, type, metadata)); | ||
} | ||
|
||
void clear() { | ||
|
@@ -58,4 +102,20 @@ void setSize(int size) { | |
} | ||
} | ||
} | ||
|
||
private void addToStore(Breadcrumb breadcrumb) { | ||
try { | ||
if (breadcrumb.payloadSize() > MAX_PAYLOAD_SIZE) { | ||
Logger.warn("Dropping breadcrumb because payload exceeds 4KB limit"); | ||
return; | ||
} | ||
if (store.size() >= maxSize) { | ||
// Remove oldest breadcrumb | ||
store.poll(); | ||
} | ||
store.add(breadcrumb); | ||
} catch (IOException ex) { | ||
Logger.warn("Dropping breadcrumb because it could not be serialized", ex); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.bugsnag.android; | ||
|
||
import android.content.Context; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* Static access to a Bugsnag Client, the easiest way to use Bugsnag in your Android app. | ||
|
@@ -372,6 +373,18 @@ public static void leaveBreadcrumb(String message) { | |
getClient().leaveBreadcrumb(message); | ||
} | ||
|
||
/** | ||
* Leave a "breadcrumb" log message representing an action or event which | ||
* occurred in your app, to aid with debugging | ||
* | ||
* @param name A short label (max 32 chars) | ||
* @param type A category for the breadcrumb | ||
* @param metadata Additional diagnostic information about the app environment | ||
*/ | ||
public static void leaveBreadcrumb(String name, BreadcrumbType type, HashMap<String, String> metadata) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should take |
||
getClient().leaveBreadcrumb(name, type, metadata); | ||
} | ||
|
||
/** | ||
* Set the maximum number of breadcrumbs to keep and sent to Bugsnag. | ||
* By default, we'll keep and send the 20 most recent breadcrumb log | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import android.support.annotation.Nullable; | ||
import android.text.TextUtils; | ||
import java.util.Locale; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* A Bugsnag Client instance allows you to use Bugsnag in your Android app. | ||
|
@@ -536,6 +537,10 @@ public void leaveBreadcrumb(String breadcrumb) { | |
breadcrumbs.add(breadcrumb); | ||
} | ||
|
||
public void leaveBreadcrumb(String name, BreadcrumbType type, HashMap<String, String> metadata) { | ||
breadcrumbs.add(name, type, metadata); | ||
} | ||
|
||
/** | ||
* Set the maximum number of breadcrumbs to keep and sent to Bugsnag. | ||
* By default, we'll keep and send the 20 most recent breadcrumb log | ||
|
@@ -580,6 +585,11 @@ private void notify(final Error error, boolean blocking) { | |
return; | ||
} | ||
|
||
// Add a breadcrumb for this error occurring | ||
HashMap<String, String> breadcrumbMetadata = new HashMap<String, String>(); | ||
breadcrumbMetadata.put("message", error.getExceptionMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
breadcrumbs.add(error.getExceptionName(), BreadcrumbType.ERROR, breadcrumbMetadata); | ||
|
||
// Capture the state of the app and device and attach diagnostics to the error | ||
error.setAppData(appData); | ||
error.setDeviceData(deviceData); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about making this the implementation of
toString()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler wasn't a fan, though maybe I missed something:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you make it public? If you override toString you need to make it public otherwise it will be trying to reduce the access privileges of an inherited method which is not allowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense and would've made a good error message. Patched in b63033e. Non-breaking change since it was package scoped before.