use of com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog in project mobile-center-sdk-android by Microsoft.
the class SasquatchCrashesListener method getErrorAttachments.
@Override
public Iterable<ErrorAttachmentLog> getErrorAttachments(ErrorReport report) {
/* Attach some text. */
ErrorAttachmentLog textLog = ErrorAttachmentLog.attachmentWithText("This is a text attachment.", "text.txt");
/* Attach app icon to test binary. */
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.mipmap.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();
ErrorAttachmentLog binaryLog = ErrorAttachmentLog.attachmentWithBinary(bitMapData, "icon.jpeg", "image/jpeg");
/* Return attachments as list. */
return Arrays.asList(textLog, binaryLog);
}
use of com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog in project mobile-center-sdk-android by Microsoft.
the class CrashesTest method queuePendingCrashesShouldProcess.
@Test
public void queuePendingCrashesShouldProcess() throws IOException, ClassNotFoundException, JSONException {
/* Setup mock. */
Context mockContext = mock(Context.class);
Channel mockChannel = mock(Channel.class);
ErrorReport report = new ErrorReport();
mockStatic(ErrorLogHelper.class);
when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
CrashesListener mockListener = mock(CrashesListener.class);
when(mockListener.shouldProcess(report)).thenReturn(true);
when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
when(mockAttachment.getContentType()).thenReturn("");
when(mockAttachment.getFileName()).thenReturn("");
when(mockAttachment.getData()).thenReturn(new byte[0]);
when(mockAttachment.isValid()).thenReturn(true);
ErrorAttachmentLog mockEmptyAttachment = mock(ErrorAttachmentLog.class);
final int skipAttachmentLogsCount = 2;
List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment, mockEmptyAttachment, null);
when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
Crashes crashes = Crashes.getInstance();
crashes.setLogSerializer(logSerializer);
crashes.setInstanceListener(mockListener);
crashes.onStarted(mockContext, "", mockChannel);
/* Test. */
verify(mockListener).shouldProcess(report);
verify(mockListener).shouldAwaitUserConfirmation();
verify(mockListener).getErrorAttachments(report);
verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object log) {
return log.equals(mErrorLog);
}
}), eq(crashes.getGroupName()));
verify(mockChannel, times(errorAttachmentLogList.size() - skipAttachmentLogsCount)).enqueue(mockAttachment, crashes.getGroupName());
}
use of com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog in project mobile-center-sdk-android by Microsoft.
the class Crashes method handleUserConfirmation.
@VisibleForTesting
private synchronized void handleUserConfirmation(@UserConfirmationDef final int userConfirmation) {
if (mChannel == null) {
MobileCenterLog.error(LOG_TAG, "Crashes service not initialized, discarding calls.");
return;
}
Runnable runnable = new Runnable() {
@Override
public void run() {
if (userConfirmation == DONT_SEND) {
/* Clean up all pending error log and throwable files. */
for (Iterator<UUID> iterator = mUnprocessedErrorReports.keySet().iterator(); iterator.hasNext(); ) {
UUID id = iterator.next();
iterator.remove();
removeAllStoredErrorLogFiles(id);
}
} else {
if (userConfirmation == ALWAYS_SEND) {
StorageHelper.PreferencesStorage.putBoolean(PREF_KEY_ALWAYS_SEND, true);
}
Iterator<Map.Entry<UUID, ErrorLogReport>> unprocessedIterator = mUnprocessedErrorReports.entrySet().iterator();
while (unprocessedIterator.hasNext()) {
if (shouldStopProcessingPendingErrors())
break;
Map.Entry<UUID, ErrorLogReport> unprocessedEntry = unprocessedIterator.next();
ErrorLogReport errorLogReport = unprocessedEntry.getValue();
mChannel.enqueue(errorLogReport.log, ERROR_GROUP);
Iterable<ErrorAttachmentLog> attachments = mCrashesListener.getErrorAttachments(errorLogReport.report);
handleErrorAttachmentLogs(attachments, errorLogReport);
/* Clean up an error log file and map entry. */
unprocessedIterator.remove();
ErrorLogHelper.removeStoredErrorLogFile(unprocessedEntry.getKey());
}
}
/* Processed crash report for the last session. */
if (isInstanceEnabled())
mHandler.getLooper().quit();
}
};
/* Run on background thread if current thread is UI thread. */
if (Looper.myLooper() == Looper.getMainLooper())
mHandler.post(runnable);
else
runnable.run();
}
use of com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog in project mobile-center-sdk-android by Microsoft.
the class Crashes method getChannelListener.
@Override
protected Channel.GroupListener getChannelListener() {
return new Channel.GroupListener() {
/** Process callback (template method) */
private void processCallback(Log log, final CallbackProcessor callbackProcessor) {
if (log instanceof ManagedErrorLog) {
ManagedErrorLog errorLog = (ManagedErrorLog) log;
if (errorLog.getFatal()) {
final ErrorReport report = buildErrorReport(errorLog);
UUID id = errorLog.getId();
if (report != null) {
/* Clean up before calling callbacks if requested. */
if (callbackProcessor.shouldDeleteThrowable()) {
removeStoredThrowable(id);
}
/* Call back. */
HandlerUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
callbackProcessor.onCallBack(report);
}
});
} else
MobileCenterLog.warn(LOG_TAG, "Cannot find crash report for the error log: " + id);
}
} else {
if (!(log instanceof ErrorAttachmentLog))
MobileCenterLog.warn(LOG_TAG, "A different type of log comes to crashes: " + log.getClass().getName());
}
}
@Override
public void onBeforeSending(Log log) {
processCallback(log, new CallbackProcessor() {
@Override
public boolean shouldDeleteThrowable() {
return false;
}
@Override
public void onCallBack(ErrorReport report) {
mCrashesListener.onBeforeSending(report);
}
});
}
@Override
public void onSuccess(Log log) {
processCallback(log, new CallbackProcessor() {
@Override
public boolean shouldDeleteThrowable() {
return true;
}
@Override
public void onCallBack(ErrorReport report) {
mCrashesListener.onSendingSucceeded(report);
}
});
}
@Override
public void onFailure(Log log, final Exception e) {
processCallback(log, new CallbackProcessor() {
@Override
public boolean shouldDeleteThrowable() {
return true;
}
@Override
public void onCallBack(ErrorReport report) {
mCrashesListener.onSendingFailed(report, e);
}
});
}
};
}
use of com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog in project mobile-center-sdk-android by Microsoft.
the class Crashes method handleErrorAttachmentLogs.
private void handleErrorAttachmentLogs(Iterable<ErrorAttachmentLog> attachments, ErrorLogReport errorLogReport) {
if (attachments == null) {
MobileCenterLog.debug(LOG_TAG, "CrashesListener.getErrorAttachments returned null, no additional information will be attached to log: " + errorLogReport.log.getId().toString());
} else {
for (ErrorAttachmentLog attachment : attachments) {
if (attachment != null) {
attachment.setId(UUID.randomUUID());
attachment.setErrorId(errorLogReport.log.getId());
if (attachment.isValid()) {
mChannel.enqueue(attachment, ERROR_GROUP);
} else {
MobileCenterLog.error(LOG_TAG, "Not all required fields are present in ErrorAttachmentLog.");
}
} else {
MobileCenterLog.warn(LOG_TAG, "Skipping null ErrorAttachmentLog in CrashesListener.getErrorAttachments.");
}
}
}
}
Aggregations