Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 36102d31 authored by Ashley's avatar Ashley
Browse files

Add Mail sync debug logging

parent 616705c1
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
package net.thunderbird.core.logging.file

import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import net.thunderbird.core.logging.LogEvent
import net.thunderbird.core.logging.LogLevel
import net.thunderbird.core.logging.LogSink

private const val ANDROID_LOG_TIME_FORMAT = "MM-dd-yy hh:mm:ss.SSS"

open class AndroidFileLogSink(
    override val level: LogLevel,
    private val tagFilters: Array<String>?,
    private val messageFilter: String?,
    fileName: String,
    fileLocation: String,
    coroutineContext: CoroutineContext = Dispatchers.IO,
) : LogSink {

    private val coroutineScope = CoroutineScope(coroutineContext + SupervisorJob())
    private val writeFile = File(fileLocation, "$fileName.txt")
    private val accumulatedLogs = ConcurrentHashMap<String, String>()

    override fun log(event: LogEvent) {
        try {
            if (tagFilters.isNullOrEmpty() || tagFilters.contains(event.tag) || messageFilter?.let {event.message.contains(it)} == true){
                accumulatedLogs[convertLongToTime(event.timestamp)] = "priority = ${event.level}, ${event.message}"
                createLogFile()
            }
        } catch (e: FileSystemException) {
           //do Something
        }
    }
    private fun createLogFile() =
        coroutineScope.launch {
            writeToLogFile()
        }
    private fun convertLongToTime(long: Long): String {
        val date = Date(long)
        val format = SimpleDateFormat(ANDROID_LOG_TIME_FORMAT, Locale.US)
        return format.format(date)
    }

    private fun writeToLogFile() {
        val result = runCatching {
            writeFile.bufferedWriter().use {
                it.write(accumulatedLogs.entries.joinToString("\n") { it2 -> it2.key + " " + it2.value })
            }
        }
        if (result.isFailure) {
            result.exceptionOrNull()?.printStackTrace()
        }
    }
}
+8 −0
Original line number Diff line number Diff line
package net.thunderbird.core.logging.file

import net.thunderbird.core.logging.LogLevel
import net.thunderbird.core.logging.LogSink

internal actual fun platformFileLogSink(level: LogLevel, tagFilters: Array<String>?, messageFilter: String?, fileName: String, fileLocation: String): LogSink {
    return AndroidFileLogSink(level, tagFilters, messageFilter, fileName, fileLocation)
}
+27 −0
Original line number Diff line number Diff line
package net.thunderbird.core.logging.file

import net.thunderbird.core.logging.LogEvent
import net.thunderbird.core.logging.LogLevel
import net.thunderbird.core.logging.LogSink

/**
 * A [LogSink] implementation that logs messages to a specified internal file.
 *
 * This sink uses the platform-specific implementations to handle logging.
 *
 * @param level The minimum [LogLevel] for messages to be logged.
 * @param fileName The [String] fileName to log to
 */
class FileLogSink(
    override val level: LogLevel,
    tagFilters: Array<String>?,
    private val messageFilter: String?,
    private val fileName: String,
    fileLocation: String,
) : LogSink {
    private val platformSink = platformFileLogSink(level, tagFilters, messageFilter, fileName, fileLocation)

    override fun log(event: LogEvent) {
        platformSink.log(event)
    }
}
+6 −0
Original line number Diff line number Diff line
package net.thunderbird.core.logging.file

import net.thunderbird.core.logging.LogLevel
import net.thunderbird.core.logging.LogSink

internal expect fun platformFileLogSink(level: LogLevel,tagFilters: Array<String>?, messageFilter: String?, fileName: String, fileLocation: String): LogSink