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;
}
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();
}
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);
}
});
}
}
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);
}
}
}
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);
}
}
Aggregations