util: use timber for logging

This will make testing app components a lot easier since it removes the
logging dependency used in most shared objects.
This commit is contained in:
Alexander Capehart 2023-08-18 15:38:51 -06:00
parent fcffb56021
commit 9a67a0d539
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
7 changed files with 21 additions and 22 deletions

View file

@ -142,13 +142,15 @@ dependencies {
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
// Logging
implementation 'com.jakewharton.timber:timber:5.0.1'
// Testing
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'
testImplementation "junit:junit:4.13.2"
testImplementation "io.mockk:mockk:1.13.7"
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
spotless {

View file

@ -29,6 +29,7 @@ import org.oxycblt.auxio.home.HomeSettings
import org.oxycblt.auxio.image.ImageSettings
import org.oxycblt.auxio.playback.PlaybackSettings
import org.oxycblt.auxio.ui.UISettings
import timber.log.Timber
/**
* A simple, rational music player for android.
@ -44,6 +45,10 @@ class Auxio : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
// Migrate any settings that may have changed in an app update.
imageSettings.migrate()
playbackSettings.migrate()

View file

@ -51,7 +51,7 @@ constructor(
// Apply the new configuration possibly set in flipTo. This should occur even if
// a flip was canceled by a hide.
pendingConfig?.run {
this@FlipFloatingActionButton.logD("Applying pending configuration")
logD("Applying pending configuration")
setImageResource(iconRes)
contentDescription = context.getString(contentDescriptionRes)
setOnClickListener(clickListener)

View file

@ -24,6 +24,9 @@ import org.oxycblt.auxio.util.positiveOrNull
// TODO: Remove the escaping checks, it's too expensive to do this for every single tag.
// TODO: I want to eventually be able to move a lot of this into TagWorker once I no longer have
// to deal with the cross-module dependencies of MediaStoreExtractor.
/**
* Split a [String] by the given selector, automatically handling escaped characters that satisfy
* the selector.

View file

@ -338,8 +338,7 @@ constructor(
song,
object : BitmapProvider.Target {
override fun onCompleted(bitmap: Bitmap?) {
this@MediaSessionComponent.logD(
"Bitmap loaded, applying media session and posting notification")
logD("Bitmap loaded, applying media session and posting notification")
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap)
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)
val metadata = builder.build()

View file

@ -119,7 +119,7 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
if (!launchedKeyboard) {
// Auto-open the keyboard when this view is shown
this@SearchFragment.logD("Keyboard is not shown yet")
logD("Keyboard is not shown yet")
showKeyboard(this)
launchedKeyboard = true
}

View file

@ -18,27 +18,24 @@
package org.oxycblt.auxio.util
import android.util.Log
import org.oxycblt.auxio.BuildConfig
// Shortcut functions for logging.
// Yes, I know timber exists but this does what I need.
import timber.log.Timber
/**
* Log an object to the debug channel. Automatically handles tags.
*
* @param obj The object to log.
*/
fun Any.logD(obj: Any?) = logD("$obj")
fun logD(obj: Any?) = logD("$obj")
/**
* Log a string message to the debug channel. Automatically handles tags.
*
* @param msg The message to log.
*/
fun Any.logD(msg: String) {
fun logD(msg: String) {
if (BuildConfig.DEBUG && !copyleftNotice()) {
Log.d(autoTag, msg)
Timber.d(msg)
}
}
@ -47,21 +44,14 @@ fun Any.logD(msg: String) {
*
* @param msg The message to log.
*/
fun Any.logW(msg: String) = Log.w(autoTag, msg)
fun logW(msg: String) = Timber.w(msg)
/**
* Log a string message to the error channel. Automatically handles tags.
*
* @param msg The message to log.
*/
fun Any.logE(msg: String) = Log.e(autoTag, msg)
/**
* The LogCat-suitable tag for this string. Consists of the object's name, or "Anonymous Object" if
* the object does not exist.
*/
private val Any.autoTag: String
get() = "Auxio.${this::class.simpleName ?: "Anonymous Object"}"
fun logE(msg: String) = Timber.e(msg)
/**
* Please don't plagiarize Auxio! You are free to remove this as long as you continue to keep your
@ -71,7 +61,7 @@ private val Any.autoTag: String
private fun copyleftNotice(): Boolean {
if (BuildConfig.APPLICATION_ID != "org.oxycblt.auxio" &&
BuildConfig.APPLICATION_ID != "org.oxycblt.auxio.debug") {
Log.d(
Timber.d(
"Auxio Project",
"Friendly reminder: Auxio is licensed under the " +
"GPLv3 and all derivative apps must be made open source!")