2022-02-09 22:20:24 +01:00
|
|
|
package io.heckel.ntfy.util
|
2022-01-17 06:19:05 +01:00
|
|
|
|
|
|
|
import android.content.Context
|
2022-01-19 04:07:49 +01:00
|
|
|
import android.os.Build
|
|
|
|
import io.heckel.ntfy.BuildConfig
|
2022-01-18 20:28:48 +01:00
|
|
|
import io.heckel.ntfy.db.Database
|
|
|
|
import io.heckel.ntfy.db.LogDao
|
|
|
|
import io.heckel.ntfy.db.LogEntry
|
2022-01-17 06:19:05 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.launch
|
2022-01-19 04:07:49 +01:00
|
|
|
import java.text.SimpleDateFormat
|
2022-01-18 00:05:59 +01:00
|
|
|
import java.util.*
|
2022-01-17 06:19:05 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
2022-01-18 00:05:59 +01:00
|
|
|
class Log(private val logsDao: LogDao) {
|
|
|
|
private val record: AtomicBoolean = AtomicBoolean(false)
|
|
|
|
private val count: AtomicInteger = AtomicInteger(0)
|
|
|
|
private val scrubNum: AtomicInteger = AtomicInteger(-1)
|
|
|
|
private val scrubTerms = Collections.synchronizedMap(mutableMapOf<String, String>())
|
2022-01-17 06:19:05 +01:00
|
|
|
|
|
|
|
private fun log(level: Int, tag: String, message: String, exception: Throwable?) {
|
|
|
|
if (!record.get()) return
|
2022-01-18 00:05:59 +01:00
|
|
|
GlobalScope.launch(Dispatchers.IO) { // FIXME This does not guarantee the log order
|
|
|
|
logsDao.insert(LogEntry(System.currentTimeMillis(), tag, level, message, exception?.stackTraceToString()))
|
2022-01-17 06:19:05 +01:00
|
|
|
val current = count.incrementAndGet()
|
|
|
|
if (current >= PRUNE_EVERY) {
|
|
|
|
logsDao.prune(ENTRIES_MAX)
|
|
|
|
count.set(0) // I know there is a race here, but this is good enough
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:52:34 +01:00
|
|
|
fun getFormatted(scrub: Boolean): String {
|
|
|
|
return if (scrub) {
|
|
|
|
prependDeviceInfo(formatEntries(scrubEntries(logsDao.getAll())), scrubLine = true)
|
|
|
|
} else {
|
|
|
|
prependDeviceInfo(formatEntries(logsDao.getAll()), scrubLine = false)
|
|
|
|
}
|
2022-01-18 00:05:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 01:52:34 +01:00
|
|
|
private fun prependDeviceInfo(s: String, scrubLine: Boolean): String {
|
|
|
|
val maybeScrubLine = if (scrubLine) "Server URLs (aside from ntfy.sh) and topics have been replaced with fruits 🍌🥝🍋🥥🥑🍊🍎🍑.\n" else ""
|
2022-01-19 04:07:49 +01:00
|
|
|
return """
|
2022-02-05 01:52:34 +01:00
|
|
|
This is a log of the ntfy Android app. The log shows up to 1,000 entries.
|
|
|
|
$maybeScrubLine
|
2022-01-19 04:07:49 +01:00
|
|
|
Device info:
|
|
|
|
--
|
|
|
|
ntfy: ${BuildConfig.VERSION_NAME} (${BuildConfig.FLAVOR})
|
|
|
|
OS: ${System.getProperty("os.version")}
|
|
|
|
Android: ${Build.VERSION.RELEASE} (SDK ${Build.VERSION.SDK_INT})
|
|
|
|
Model: ${Build.DEVICE}
|
2022-01-19 04:22:00 +01:00
|
|
|
Product: ${Build.PRODUCT}
|
2022-01-19 04:24:30 +01:00
|
|
|
--
|
2022-01-19 04:07:49 +01:00
|
|
|
""".trimIndent() + "\n\n$s"
|
2022-01-18 00:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun addScrubTerm(term: String, type: TermType = TermType.Term) {
|
|
|
|
if (scrubTerms[term] != null || IGNORE_TERMS.contains(term)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val replaceTermIndex = scrubNum.incrementAndGet()
|
2022-01-19 04:07:49 +01:00
|
|
|
val replaceTerm = REPLACE_TERMS.getOrNull(replaceTermIndex) ?: "fruit${replaceTermIndex}"
|
2022-01-18 00:05:59 +01:00
|
|
|
scrubTerms[term] = when (type) {
|
|
|
|
TermType.Domain -> "$replaceTerm.example.com"
|
|
|
|
else -> replaceTerm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 04:07:49 +01:00
|
|
|
private fun scrubEntries(entries: List<LogEntry>): List<LogEntry> {
|
|
|
|
return entries
|
|
|
|
.map { e ->
|
|
|
|
e.copy(
|
|
|
|
message = scrub(e.message)!!,
|
|
|
|
exception = scrub(e.exception)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 00:05:59 +01:00
|
|
|
private fun scrub(line: String?): String? {
|
|
|
|
var newLine = line ?: return null
|
|
|
|
scrubTerms.forEach { (scrubTerm, replaceTerm) ->
|
|
|
|
newLine = newLine.replace(scrubTerm, replaceTerm)
|
|
|
|
}
|
|
|
|
return newLine
|
|
|
|
}
|
|
|
|
|
2022-01-19 04:07:49 +01:00
|
|
|
private fun formatEntries(entries: List<LogEntry>): String {
|
|
|
|
return entries.joinToString(separator = "\n") { e ->
|
|
|
|
val date = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(Date(e.timestamp))
|
|
|
|
val level = when (e.level) {
|
|
|
|
android.util.Log.DEBUG -> "D"
|
|
|
|
android.util.Log.INFO -> "I"
|
|
|
|
android.util.Log.WARN -> "W"
|
|
|
|
android.util.Log.ERROR -> "E"
|
|
|
|
else -> "?"
|
|
|
|
}
|
2022-01-21 03:23:24 +01:00
|
|
|
val tag = e.tag.format("%23s")
|
2022-01-19 04:07:49 +01:00
|
|
|
val prefix = "${e.timestamp} $date $level $tag"
|
|
|
|
val message = if (e.exception != null) {
|
|
|
|
"${e.message}\nException:\n${e.exception}"
|
|
|
|
} else {
|
|
|
|
e.message
|
|
|
|
}
|
|
|
|
"$prefix $message"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun deleteAll() {
|
|
|
|
return logsDao.deleteAll()
|
|
|
|
}
|
|
|
|
|
2022-01-18 00:05:59 +01:00
|
|
|
enum class TermType {
|
|
|
|
Domain, Term
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:19:05 +01:00
|
|
|
companion object {
|
2022-01-18 00:05:59 +01:00
|
|
|
private const val TAG = "NtfyLog"
|
|
|
|
private const val PRUNE_EVERY = 100
|
2022-02-05 01:52:34 +01:00
|
|
|
private const val ENTRIES_MAX = 1000
|
2022-01-18 00:05:59 +01:00
|
|
|
private val IGNORE_TERMS = listOf("ntfy.sh")
|
|
|
|
private val REPLACE_TERMS = listOf(
|
2022-01-19 04:07:49 +01:00
|
|
|
"banana", "kiwi", "lemon", "coconut", "avocado", "orange", "apple", "peach"
|
2022-01-18 00:05:59 +01:00
|
|
|
)
|
|
|
|
private var instance: Log? = null
|
|
|
|
|
2022-01-17 06:19:05 +01:00
|
|
|
fun d(tag: String, message: String, exception: Throwable? = null) {
|
|
|
|
if (exception == null) android.util.Log.d(tag, message) else android.util.Log.d(tag, message, exception)
|
|
|
|
getInstance()?.log(android.util.Log.DEBUG, tag, message, exception)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun i(tag: String, message: String, exception: Throwable? = null) {
|
|
|
|
if (exception == null) android.util.Log.i(tag, message) else android.util.Log.i(tag, message, exception)
|
|
|
|
getInstance()?.log(android.util.Log.INFO, tag, message, exception)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun w(tag: String, message: String, exception: Throwable? = null) {
|
|
|
|
if (exception == null) android.util.Log.w(tag, message) else android.util.Log.w(tag, message, exception)
|
|
|
|
getInstance()?.log(android.util.Log.WARN, tag, message, exception)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun e(tag: String, message: String, exception: Throwable? = null) {
|
|
|
|
if (exception == null) android.util.Log.e(tag, message) else android.util.Log.e(tag, message, exception)
|
|
|
|
getInstance()?.log(android.util.Log.ERROR, tag, message, exception)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setRecord(enable: Boolean) {
|
|
|
|
if (!enable) d(TAG, "Disabled log recording")
|
|
|
|
getInstance()?.record?.set(enable)
|
|
|
|
if (enable) d(TAG, "Enabled log recording")
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getRecord(): Boolean {
|
|
|
|
return getInstance()?.record?.get() ?: false
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:52:34 +01:00
|
|
|
fun getFormatted(scrub: Boolean): String {
|
|
|
|
return getInstance()?.getFormatted(scrub) ?: "(no logs)"
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getScrubTerms(): Map<String, String> {
|
|
|
|
return getInstance()?.scrubTerms!!.toMap()
|
2022-01-18 00:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteAll() {
|
|
|
|
getInstance()?.deleteAll()
|
2022-01-19 04:07:49 +01:00
|
|
|
d(TAG, "Log was truncated")
|
2022-01-18 00:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun addScrubTerm(term: String, type: TermType = TermType.Term) {
|
|
|
|
getInstance()?.addScrubTerm(term, type)
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:59 +01:00
|
|
|
fun init(context: Context) {
|
2022-01-17 06:19:05 +01:00
|
|
|
return synchronized(Log::class) {
|
|
|
|
if (instance == null) {
|
|
|
|
val database = Database.getInstance(context.applicationContext)
|
2022-01-18 00:05:59 +01:00
|
|
|
instance = Log(database.logDao())
|
2022-01-17 06:19:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getInstance(): Log? {
|
|
|
|
return synchronized(Log::class) {
|
|
|
|
instance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|