logging: Add functions that accept custom class/method/linenum values

These are meant for integrating other loggers into AAPSLogger.

Signed-off-by: Carlos Rafael Giani <crg7475@mailbox.org>
This commit is contained in:
Carlos Rafael Giani 2021-12-20 17:02:58 +01:00
parent 0c486e01cf
commit d8842340b3
3 changed files with 46 additions and 2 deletions

View file

@ -5,7 +5,6 @@ package info.nightscout.shared.logging
*/ */
interface AAPSLogger { interface AAPSLogger {
fun debug(message: String) fun debug(message: String)
fun debug(enable: Boolean, tag: LTag, message: String) fun debug(enable: Boolean, tag: LTag, message: String)
fun debug(tag: LTag, message: String) fun debug(tag: LTag, message: String)
@ -21,4 +20,13 @@ interface AAPSLogger {
fun error(message: String) fun error(message: String)
fun error(message: String, throwable: Throwable) fun error(message: String, throwable: Throwable)
fun error(format: String, vararg arguments: Any?) fun error(format: String, vararg arguments: Any?)
// These are variants of the calls above that allow for explicitly
// specifying the exact logging location. They are primarily meant
// as a way to integrate other logging infrastructures into AndroidAPS,
// and typically aren't practical to use directly for logging in code.
fun debug(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String)
fun info(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String)
fun warn(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String)
fun error(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String)
} }

View file

@ -74,9 +74,29 @@ class AAPSLoggerProduction constructor(val l: L) : AAPSLogger {
override fun error(tag: LTag, format: String, vararg arguments: Any?) { override fun error(tag: LTag, format: String, vararg arguments: Any?) {
LoggerFactory.getLogger(tag.tag).error(stackLogMarker() + format, arguments) LoggerFactory.getLogger(tag.tag).error(stackLogMarker() + format, arguments)
} }
override fun debug(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String) {
LoggerFactory.getLogger(tag.tag).debug(logLocationPrefix(className, methodName, lineNumber) + message)
}
override fun info(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String) {
LoggerFactory.getLogger(tag.tag).info(logLocationPrefix(className, methodName, lineNumber) + message)
}
override fun warn(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String) {
LoggerFactory.getLogger(tag.tag).warn(logLocationPrefix(className, methodName, lineNumber) + message)
}
override fun error(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String) {
LoggerFactory.getLogger(tag.tag).error(logLocationPrefix(className, methodName, lineNumber) + message)
}
} }
fun StackTraceElement.toLogString(): String = "[${this.className.substringAfterLast(".")}.${this.methodName}():${this.lineNumber}]: " private fun logLocationPrefix(className: String, methodName: String, lineNumber: Int) =
"[$className.$methodName():$lineNumber]: "
fun StackTraceElement.toLogString(): String =
logLocationPrefix(this.className.substringAfterLast("."), this.methodName, this.lineNumber)
/* Needs to be inline. Don't remove even if IDE suggests it. */ /* Needs to be inline. Don't remove even if IDE suggests it. */
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")

View file

@ -65,4 +65,20 @@ class AAPSLoggerTest : AAPSLogger {
override fun error(tag: LTag, format: String, vararg arguments: Any?) { override fun error(tag: LTag, format: String, vararg arguments: Any?) {
println("ERROR: : " + tag.tag + " " + String.format(format, arguments)) println("ERROR: : " + tag.tag + " " + String.format(format, arguments))
} }
override fun debug(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String) {
println("DEBUG: : ${tag.tag} $className.$methodName():$lineNumber $message")
}
override fun info(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String) {
println("INFO: : ${tag.tag} $className.$methodName():$lineNumber $message")
}
override fun warn(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String) {
println("WARN: : ${tag.tag} $className.$methodName():$lineNumber $message")
}
override fun error(className: String, methodName: String, lineNumber: Int, tag: LTag, message: String) {
println("ERROR: : ${tag.tag} $className.$methodName():$lineNumber $message")
}
} }