Search in sources :

Example 1 with ErrorAttachmentLog

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);
}
Also used : Bitmap(android.graphics.Bitmap) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with ErrorAttachmentLog

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());
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ErrorAttachmentLog

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();
}
Also used : ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 4 with ErrorAttachmentLog

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);
                }
            });
        }
    };
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 5 with ErrorAttachmentLog

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.");
            }
        }
    }
}
Also used : ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)

Aggregations

ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)7 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)4 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)4 UUID (java.util.UUID)4 Context (android.content.Context)3 Channel (com.microsoft.azure.mobile.channel.Channel)3 Test (org.junit.Test)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)2 File (java.io.File)2 Bitmap (android.graphics.Bitmap)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 TestCrashException (com.microsoft.azure.mobile.crashes.model.TestCrashException)1 Log (com.microsoft.azure.mobile.ingestion.models.Log)1 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1