Search in sources :

Example 16 with Log

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

the class DefaultLogSerializer method deserializeContainer.

@NonNull
@Override
public LogContainer deserializeContainer(@NonNull String json) throws JSONException {
    JSONObject jContainer = new JSONObject(json);
    LogContainer container = new LogContainer();
    JSONArray jLogs = jContainer.getJSONArray(LOGS);
    List<Log> logs = new ArrayList<>();
    for (int i = 0; i < jLogs.length(); i++) {
        JSONObject jLog = jLogs.getJSONObject(i);
        Log log = readLog(jLog);
        logs.add(log);
    }
    container.setLogs(logs);
    return container;
}
Also used : JSONObject(org.json.JSONObject) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) NonNull(android.support.annotation.NonNull)

Example 17 with Log

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

the class DefaultLogSerializer method serializeContainer.

@NonNull
@Override
public String serializeContainer(@NonNull LogContainer logContainer) throws JSONException {
    /* Init JSON serializer, in verbose: try to make it pretty. */
    JSONStringer writer = null;
    if (MobileCenterLog.getLogLevel() <= android.util.Log.VERBOSE) {
        try {
            Constructor<JSONStringer> constructor = JSONStringer.class.getDeclaredConstructor(int.class);
            constructor.setAccessible(true);
            writer = constructor.newInstance(2);
        } catch (Exception e) {
            MobileCenterLog.error(MobileCenter.LOG_TAG, "Failed to setup pretty json, falling back to default one", e);
        }
    }
    if (writer == null)
        writer = new JSONStringer();
    /* Start writing JSON. */
    writer.object();
    writer.key(LOGS).array();
    for (Log log : logContainer.getLogs()) writeLog(writer, log);
    writer.endArray();
    writer.endObject();
    return writer.toString();
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) JSONStringer(org.json.JSONStringer) JSONException(org.json.JSONException) NonNull(android.support.annotation.NonNull)

Example 18 with Log

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

the class DefaultChannel method triggerIngestion.

private synchronized void triggerIngestion(final String batchId, final GroupState groupState, final int stateSnapshot, final List<Log> batch) {
    if (batchId != null && checkStateDidNotChange(groupState, stateSnapshot)) {
        /* Call group listener before sending logs to ingestion service. */
        if (groupState.mListener != null) {
            for (Log log : batch) {
                groupState.mListener.onBeforeSending(log);
            }
        }
        /* Decrement counter. */
        groupState.mPendingLogCount -= batch.size();
        MobileCenterLog.debug(LOG_TAG, "ingestLogs(" + groupState.mName + "," + batchId + ") pendingLogCount=" + groupState.mPendingLogCount);
        /* Remember this batch. */
        groupState.mSendingBatches.put(batchId, batch);
        /*
             * Due to bug on old Android versions (verified on 4.0.4),
             * if we start an async task from here, i.e. the async persistence handler thread,
             * we end up with AsyncTask configured with the wrong Handler to use for onPostExecute
             * instead of using main thread as advertised in Javadoc (and its a static field there).
             *
             * Our SDK guards against an application that would make a first async task in non UI
             * thread before SDK is initialized, but we should also avoid corrupting AsyncTask
             * with our wrong handler to avoid creating bugs in the application code since we are
             * a library.
             *
             * So make sure we execute the async task from UI thread to avoid any issue.
             */
        HandlerUtils.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                sendLogs(groupState, stateSnapshot, batch, batchId);
            }
        });
    }
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log)

Example 19 with Log

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

the class DefaultChannel method deleteLogsOnSuspended.

private synchronized void deleteLogsOnSuspended(GroupState groupState, int currentState, List<Log> logs) {
    if (checkStateDidNotChange(groupState, currentState)) {
        if (logs.size() > 0 && groupState.mListener != null) {
            for (Log log : logs) {
                groupState.mListener.onBeforeSending(log);
                groupState.mListener.onFailure(log, new CancellationException());
            }
        }
        if (logs.size() >= CLEAR_BATCH_SIZE && groupState.mListener != null) {
            deleteLogsOnSuspended(groupState);
        } else {
            mPersistence.deleteLogs(groupState.mName);
        }
    }
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) CancellationException(com.microsoft.azure.mobile.CancellationException)

Example 20 with Log

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

the class DefaultChannel method handleSendingSuccess.

/**
     * The actual implementation to react to sending a batch to the server successfully.
     *
     * @param groupState   The group state.
     * @param currentState The current state.
     * @param batchId      The batch ID.
     */
private synchronized void handleSendingSuccess(@NonNull final GroupState groupState, int currentState, @NonNull final String batchId) {
    if (checkStateDidNotChange(groupState, currentState)) {
        String groupName = groupState.mName;
        mPersistence.deleteLogs(groupName, batchId);
        List<Log> removedLogsForBatchId = groupState.mSendingBatches.remove(batchId);
        GroupListener groupListener = groupState.mListener;
        if (groupListener != null) {
            for (Log log : removedLogsForBatchId) groupListener.onSuccess(log);
        }
        checkPendingLogs(groupName);
    }
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log)

Aggregations

Log (com.microsoft.azure.mobile.ingestion.models.Log)59 Test (org.junit.Test)44 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)37 ArrayList (java.util.ArrayList)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Context (android.content.Context)18 UUID (java.util.UUID)18 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)13 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)13 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)12 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)12 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)11 Persistence (com.microsoft.azure.mobile.persistence.Persistence)11 Matchers.anyString (org.mockito.Matchers.anyString)10 IOException (java.io.IOException)9 Channel (com.microsoft.azure.mobile.channel.Channel)8 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)8 StartServiceLog (com.microsoft.azure.mobile.ingestion.models.StartServiceLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7