com.ybo.trackingplugin.tracerlib.TracePerformer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of traceplugin Show documentation
Show all versions of traceplugin Show documentation
gradle plugin allowing to add automatic logs (or other process) at the start of each traced method
package com.ybo.trackingplugin.tracerlib
import com.ybo.trackingplugin.decodedFromB64
object TracePerformer {
fun trace(
tracerFactory: Tracer.Factory,
method: String,
java: Boolean,
annotationName: String,
paramz: Array,
alterationOffset: Int,
) {
val tracer = tracerFactory.create()
val throwable = Throwable()
val stackElement = throwable.stackTrace.run {
if (size >= 2) {
this[1]
} else {
null
}
}
val methodNameToTheBestOfKnowledge = if (method.isNotBlank()) {
method.decodedFromB64()
} else {
stackElement?.methodName ?: ""
}
val fullMethodName = (stackElement?.className ?: "") +
"." + methodNameToTheBestOfKnowledge
val fullMethodNamePossiblyObfuscated = (
(stackElement?.className ?: "") +
"." + stackElement?.methodName
)
val line = stackElement?.lineNumber?.let {
(it - alterationOffset).coerceAtLeast(0)
} ?: 0
val stringLink = stackElement?.fileName?.let {
"$it:$line"
} ?: ""
val currentMethod = Tracer.Method(
originalName = fullMethodName,
possiblyObfuscatedMethod = fullMethodNamePossiblyObfuscated,
link = stringLink,
)
historyOfMethods.add(currentMethod)
tracer.trace(
defaultMessage = makeMessage(java, fullMethodName, paramz),
java = java,
annotationName = Tracer.TraceAnnotationName(annotationName.decodedFromB64()),
method = currentMethod,
history = historyOfMethods,
parameterValues = paramz,
).also {
when (it) {
Tracer.TraceHistoryManagementAction.Clear -> historyOfMethods.clear()
Tracer.TraceHistoryManagementAction.DeleteOldest -> {
if (historyOfMethods.size >= 1) {
historyOfMethods[0] = null
}
}
Tracer.TraceHistoryManagementAction.None -> {}
is Tracer.TraceHistoryManagementAction.NullAtIndex -> {
if (it.index < historyOfMethods.size) {
historyOfMethods[it.index] = null
}
}
}
}
}
private fun makeMessage(
java: Boolean,
methodName: String,
parameterValues: Array,
): String {
val params = parameterValues.joinToString(", ") { param ->
param?.let {
it.toString() + " : " + it.javaClass.name
} ?: "null"
}
return " $methodName($params) "
}
private val historyOfMethods: MutableList = mutableListOf()
}