Search in sources :

Example 11 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class ErrorLogHelperTest method createErrorLogFailOver.

@Test
public void createErrorLogFailOver() throws DeviceInfoHelper.DeviceInfoException {
    /* Mock base. */
    Context mockContext = mock(Context.class);
    when(SystemClock.elapsedRealtime()).thenReturn(1000L);
    when(Process.myPid()).thenReturn(123);
    /* Mock device. */
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("mock", new PackageManager.NameNotFoundException()));
    /* Mock architecture. */
    Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", 15);
    Whitebox.setInternalState(Build.class, "CPU_ABI", "armeabi-v7a");
    /* Test. */
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mockContext, java.lang.Thread.currentThread(), new java.lang.Exception(), java.lang.Thread.getAllStackTraces(), 900, true);
    assertNotNull(errorLog);
    assertNotNull(errorLog.getId());
    assertTrue(System.currentTimeMillis() - errorLog.getToffset() <= 1000);
    assertNull(errorLog.getDevice());
    assertEquals(Integer.valueOf(123), errorLog.getProcessId());
    assertNull(errorLog.getProcessName());
    assertNull(errorLog.getParentProcessId());
    assertNull(errorLog.getParentProcessName());
    assertEquals("armeabi-v7a", errorLog.getArchitecture());
    assertEquals((Long) java.lang.Thread.currentThread().getId(), errorLog.getErrorThreadId());
    assertEquals(java.lang.Thread.currentThread().getName(), errorLog.getErrorThreadName());
    assertEquals(Boolean.TRUE, errorLog.getFatal());
    assertEquals(Long.valueOf(100), errorLog.getAppLaunchTOffset());
}
Also used : Context(android.content.Context) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Build(android.os.Build) DeviceInfoHelper(com.microsoft.azure.mobile.utils.DeviceInfoHelper) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class Crashes method trackException.

/**
     * Track an exception.
     * TODO the backend does not support that service yet, will be public method later.
     *
     * @param exception An exception.
     */
synchronized void trackException(@NonNull com.microsoft.azure.mobile.crashes.ingestion.models.Exception exception) {
    if (isInactive())
        return;
    ManagedErrorLog errorLog = ErrorLogHelper.createErrorLog(mContext, Thread.currentThread(), exception, Thread.getAllStackTraces(), getInitializeTimestamp(), false);
    mChannel.enqueue(errorLog, ERROR_GROUP);
}
Also used : ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)

Example 13 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog 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 14 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class Crashes method processPendingErrors.

private void processPendingErrors() {
    mHandler.post(new Runnable() {

        @Override
        public void run() {
            for (File logFile : ErrorLogHelper.getStoredErrorLogFiles()) {
                if (shouldStopProcessingPendingErrors())
                    return;
                MobileCenterLog.debug(LOG_TAG, "Process pending error file: " + logFile);
                String logfileContents = StorageHelper.InternalStorage.read(logFile);
                if (logfileContents != null)
                    try {
                        ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logfileContents);
                        UUID id = log.getId();
                        ErrorReport report = buildErrorReport(log);
                        if (report == null) {
                            removeAllStoredErrorLogFiles(id);
                        } else if (mCrashesListener.shouldProcess(report)) {
                            MobileCenterLog.debug(LOG_TAG, "CrashesListener.shouldProcess returned true, continue processing log: " + id.toString());
                            mUnprocessedErrorReports.put(id, mErrorReportCache.get(id));
                        } else {
                            MobileCenterLog.debug(LOG_TAG, "CrashesListener.shouldProcess returned false, clean up and ignore log: " + id.toString());
                            removeAllStoredErrorLogFiles(id);
                        }
                    } catch (JSONException e) {
                        MobileCenterLog.error(LOG_TAG, "Error parsing error log", e);
                    }
            }
            if (shouldStopProcessingPendingErrors())
                return;
            processUserConfirmation();
        }
    });
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) JSONException(org.json.JSONException) UUID(java.util.UUID) File(java.io.File)

Example 15 with ManagedErrorLog

use of com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog in project mobile-center-sdk-android by Microsoft.

the class Crashes method initialize.

private void initialize() {
    boolean enabled = isInstanceEnabled();
    mInitializeTimestamp = enabled ? SystemClock.elapsedRealtime() : -1;
    if (!enabled) {
        if (mUncaughtExceptionHandler != null) {
            mUncaughtExceptionHandler.unregister();
            mUncaughtExceptionHandler = null;
        }
    } else if (mContext != null && mUncaughtExceptionHandler == null) {
        mUncaughtExceptionHandler = new UncaughtExceptionHandler();
        mUncaughtExceptionHandler.register();
        final File logFile = ErrorLogHelper.getLastErrorLogFile();
        if (logFile != null) {
            MobileCenterLog.debug(LOG_TAG, "Processing crash report for the last session.");
            mCountDownLatch = new CountDownLatch(1);
            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    String logFileContents = StorageHelper.InternalStorage.read(logFile);
                    if (logFileContents == null)
                        MobileCenterLog.error(LOG_TAG, "Error reading last session error log.");
                    else {
                        try {
                            ManagedErrorLog log = (ManagedErrorLog) mLogSerializer.deserializeLog(logFileContents);
                            mLastSessionErrorReport = buildErrorReport(log);
                            MobileCenterLog.debug(LOG_TAG, "Processed crash report for the last session.");
                        } catch (JSONException e) {
                            MobileCenterLog.error(LOG_TAG, "Error parsing last session error log.", e);
                        }
                    }
                    mCountDownLatch.countDown();
                    HandlerUtils.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            /* Call callbacks for getInstanceLastSessionCrashReport(ResultCallback) . */
                            for (Iterator<ResultCallback<ErrorReport>> iterator = mLastCrashErrorReportCallbacks.iterator(); iterator.hasNext(); ) {
                                ResultCallback<ErrorReport> callback = iterator.next();
                                iterator.remove();
                                callback.onResult(mLastSessionErrorReport);
                            }
                        }
                    });
                }
            });
        }
    }
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ResultCallback(com.microsoft.azure.mobile.ResultCallback) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Iterator(java.util.Iterator) JSONException(org.json.JSONException) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File)

Aggregations

ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)20 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 Context (android.content.Context)9 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)9 File (java.io.File)7 Channel (com.microsoft.azure.mobile.channel.Channel)6 JSONException (org.json.JSONException)6 TestCrashException (com.microsoft.azure.mobile.crashes.model.TestCrashException)5 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)5 IOException (java.io.IOException)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)4 Log (com.microsoft.azure.mobile.ingestion.models.Log)4 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)4 UUID (java.util.UUID)4 ActivityManager (android.app.ActivityManager)3 Build (android.os.Build)3 Device (com.microsoft.azure.mobile.ingestion.models.Device)3 Answer (org.mockito.stubbing.Answer)3