public long putLog()

in sdk/appcenter/src/main/java/com/microsoft/appcenter/persistence/DatabasePersistence.java [264:325]


    public long putLog(@NonNull Log log, @NonNull String group, @IntRange(from = Flags.NORMAL, to = Flags.CRITICAL) int flags) throws PersistenceException {

        /* Convert log to JSON string and put in the database. */
        try {
            AppCenterLog.debug(LOG_TAG, "Storing a log to the Persistence database for log type " + log.getType() + " with flags=" + flags);
            String payload = getLogSerializer().serializeLog(log);
            ContentValues contentValues;

            //noinspection CharsetObjectCanBeUsed min API level 19 required to fix this warning.
            int payloadSize = payload.getBytes("UTF-8").length;
            boolean isLargePayload = payloadSize >= PAYLOAD_MAX_SIZE;
            String targetKey;
            String targetToken;
            if (log instanceof CommonSchemaLog) {
                if (isLargePayload) {
                    throw new PersistenceException("Log is larger than " + PAYLOAD_MAX_SIZE + " bytes, cannot send to OneCollector.");
                }
                targetToken = log.getTransmissionTargetTokens().iterator().next();
                targetKey = PartAUtils.getTargetKey(targetToken);
                targetToken = CryptoUtils.getInstance(mContext).encrypt(targetToken);
            } else {
                targetKey = null;
                targetToken = null;
            }
            long maxSize = mDatabaseManager.getMaxSize();
            if (maxSize == -1) {
                throw new PersistenceException("Failed to store a log to the Persistence database.");
            }
            if (!isLargePayload && maxSize <= payloadSize) {
                throw new PersistenceException("Log is too large (" + payloadSize + " bytes) to store in database. " +
                        "Current maximum database size is " + maxSize + " bytes.");
            }
            contentValues = getContentValues(group, isLargePayload ? null : payload, targetToken, log.getType(), targetKey, Flags.getPersistenceFlag(flags, false));
            long databaseId = mDatabaseManager.put(contentValues, COLUMN_PRIORITY);
            if (databaseId == -1) {
                throw new PersistenceException("Failed to store a log to the Persistence database for log type " + log.getType() + ".");
            }
            AppCenterLog.debug(LOG_TAG, "Stored a log to the Persistence database for log type " + log.getType() + " with databaseId=" + databaseId);
            if (isLargePayload) {
                AppCenterLog.debug(LOG_TAG, "Payload is larger than what SQLite supports, storing payload in a separate file.");
                File directory = getLargePayloadGroupDirectory(group);

                //noinspection ResultOfMethodCallIgnored we'll get an error anyway at write time.
                directory.mkdir();
                File payloadFile = getLargePayloadFile(directory, databaseId);
                try {
                    FileManager.write(payloadFile, payload);
                } catch (IOException e) {

                    /* Remove database entry if we cannot save payload as a file. */
                    mDatabaseManager.delete(databaseId);
                    throw e;
                }
                AppCenterLog.debug(LOG_TAG, "Payload written to " + payloadFile);
            }
            return databaseId;
        } catch (JSONException e) {
            throw new PersistenceException("Cannot convert to JSON string.", e);
        } catch (IOException e) {
            throw new PersistenceException("Cannot save large payload in a file.", e);
        }
    }