LogcatSharkLog.kt
TLDR
The LogcatSharkLog.kt
file in the leakcanary
package provides a class LogcatSharkLog
that implements the Logger
interface from the shark.SharkLog
package. This class is used for logging messages and stack traces to the Android logcat. It also includes a companion object
with a method install()
that can be used to install an instance of LogcatSharkLog
as the logger for shark.SharkLog
.
Classes
LogcatSharkLog
The LogcatSharkLog
class is a logger implementation that logs messages and stack traces to the Android logcat. It implements the Logger
interface from the shark.SharkLog
package. It provides the following methods:
-
d(message: String)
: Logs a debug level message to the logcat. If the message length is less than 4000 characters, it is logged as is. Otherwise, it logs each line of the message separately. -
d(throwable: Throwable, message: String)
: Logs a debug level message along with the stack trace of a throwable to the logcat. It calls thed
method with the combined message and the stack trace string obtained fromLog.getStackTraceString(throwable)
.
Companion Object
install()
The install()
method is a companion object function that can be used to install an instance of LogcatSharkLog
as the logger for shark.SharkLog
. This method sets the SharkLog.logger
property to an instance of LogcatSharkLog
.
package leakcanary
import android.util.Log
import shark.SharkLog
import shark.SharkLog.Logger
class LogcatSharkLog : Logger {
override fun d(message: String) {
if (message.length < 4000) {
Log.d("LeakCanary", message)
} else {
message.lines().forEach { line ->
Log.d("LeakCanary", line)
}
}
}
override fun d(
throwable: Throwable,
message: String
) {
d("$message\n${Log.getStackTraceString(throwable)}")
}
companion object {
fun install() {
SharkLog.logger = LogcatSharkLog()
}
}
}