All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.amplitude.core.utilities.Diagnostics.kt Maven / Gradle / Ivy

The newest version!
package com.amplitude.core.utilities

import java.util.Collections

class Diagnostics() {
    private var malformedEvents: MutableList? = null
    private var errorLogs: MutableSet = Collections.synchronizedSet(mutableSetOf())

    companion object {
        private const val MAX_ERROR_LOGS = 10
    }

    fun addMalformedEvent(event: String) {
        if (malformedEvents == null) {
            malformedEvents = Collections.synchronizedList(mutableListOf())
        }
        malformedEvents?.add(event)
    }

    fun addErrorLog(log: String) {
        errorLogs.add(log)
        while (errorLogs.size > MAX_ERROR_LOGS) {
            errorLogs.remove(errorLogs.first())
        }
    }

    fun hasDiagnostics(): Boolean {
        return (malformedEvents != null && malformedEvents!!.isNotEmpty()) || errorLogs.isNotEmpty()
    }

    /**
     * Extracts the diagnostics as a JSON string.
     * @return JSON string of diagnostics or empty if no diagnostics are present.
     */
    fun extractDiagnostics(): String? {
        if (!hasDiagnostics()) {
            return null
        }
        val diagnostics = mutableMapOf>()
        if (malformedEvents != null && malformedEvents!!.isNotEmpty()) {
            diagnostics["malformed_events"] = malformedEvents!!
        }
        if (errorLogs.isNotEmpty()) {
            diagnostics["error_logs"] = errorLogs.toList()
        }
        val result = diagnostics.toJSONObject().toString()
        malformedEvents?.clear()
        errorLogs.clear()
        return result
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy