From fe4a00ae07165c5d8806d6d814c78b3c4cb00975 Mon Sep 17 00:00:00 2001 From: Ryan Harg Date: Fri, 13 Aug 2021 10:55:26 +0200 Subject: [PATCH] 74: Update logging to gather more information --- .../funkwhale/ffa/activities/LoginActivity.kt | 25 ++++++++----------- .../funkwhale/ffa/activities/MainActivity.kt | 2 +- .../funkwhale/ffa/playback/PlayerService.kt | 6 ++++- .../java/audio/funkwhale/ffa/utils/OAuth.kt | 11 ++++---- .../java/audio/funkwhale/ffa/utils/Util.kt | 23 ++++++++++++++--- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/audio/funkwhale/ffa/activities/LoginActivity.kt b/app/src/main/java/audio/funkwhale/ffa/activities/LoginActivity.kt index 4b7c0c0..ac405c4 100644 --- a/app/src/main/java/audio/funkwhale/ffa/activities/LoginActivity.kt +++ b/app/src/main/java/audio/funkwhale/ffa/activities/LoginActivity.kt @@ -43,23 +43,20 @@ class LoginActivity : AppCompatActivity() { data?.let { when (requestCode) { 0 -> { - oAuth.exchange(this, data, - { - PowerPreference - .getFileByName(AppContext.PREFS_CREDENTIALS) - .setBoolean("anonymous", false) + oAuth.exchange(this, data) { + PowerPreference + .getFileByName(AppContext.PREFS_CREDENTIALS) + .setBoolean("anonymous", false) - lifecycleScope.launch(Main) { - Userinfo.get(this@LoginActivity, oAuth)?.let { - startActivity(Intent(this@LoginActivity, MainActivity::class.java)) + lifecycleScope.launch(Main) { + Userinfo.get(this@LoginActivity, oAuth)?.let { + startActivity(Intent(this@LoginActivity, MainActivity::class.java)) - return@launch finish() - } - throw Exception(getString(R.string.login_error_userinfo)) + return@launch finish() } - }, - { "error".log() } - ) + throw Exception(getString(R.string.login_error_userinfo)) + } + } } } } diff --git a/app/src/main/java/audio/funkwhale/ffa/activities/MainActivity.kt b/app/src/main/java/audio/funkwhale/ffa/activities/MainActivity.kt index a68ec0e..5fe32df 100644 --- a/app/src/main/java/audio/funkwhale/ffa/activities/MainActivity.kt +++ b/app/src/main/java/audio/funkwhale/ffa/activities/MainActivity.kt @@ -637,7 +637,7 @@ class MainActivity : AppCompatActivity() { .body(Gson().toJson(mapOf("track" to track.id))) .awaitStringResponse() } catch (e: Exception) { - e.log() + e.logError("incrementListenCount()") } } } diff --git a/app/src/main/java/audio/funkwhale/ffa/playback/PlayerService.kt b/app/src/main/java/audio/funkwhale/ffa/playback/PlayerService.kt index fa34bfd..310834d 100644 --- a/app/src/main/java/audio/funkwhale/ffa/playback/PlayerService.kt +++ b/app/src/main/java/audio/funkwhale/ffa/playback/PlayerService.kt @@ -12,6 +12,7 @@ import android.media.MediaMetadata import android.os.Build import android.os.IBinder import android.support.v4.media.MediaMetadataCompat +import android.util.Log import android.view.KeyEvent import androidx.core.app.NotificationManagerCompat import androidx.media.session.MediaButtonReceiver @@ -479,7 +480,10 @@ class PlayerService : Service() { super.onPositionDiscontinuity(reason) if (reason == Player.DISCONTINUITY_REASON_PERIOD_TRANSITION) { - EventBus.send(Event.TrackFinished(queue.current())) + val currentTrack = queue.current().also { + it.log("Track finished") + } + EventBus.send(Event.TrackFinished(currentTrack)) } } diff --git a/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt b/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt index cbcddfb..6576dc3 100644 --- a/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt +++ b/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt @@ -60,7 +60,7 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory } else { false }.also { - it.log("isAuthorized()") + it.logInfo("isAuthorized()") } } @@ -75,7 +75,7 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory state: AuthState, context: Context ): Boolean { - if (state.needsTokenRefresh.also { it.log("needsTokenRefresh()") } && + if (state.needsTokenRefresh.also { it.logInfo("needsTokenRefresh()") } && state.refreshToken != null) { val refreshRequest = state.createTokenRefreshRequest() val auth = ClientSecretPost(state.clientSecret) @@ -91,7 +91,7 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory } return (state.isAuthorized) .also { - it.log("tryRefreshAccessToken()") + it.logInfo("tryRefreshAccessToken()") } } @@ -171,8 +171,7 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory fun exchange( context: Context, authorization: Intent, - success: () -> Unit, - error: () -> Unit + success: () -> Unit ) { state().let { state -> state.apply { @@ -194,7 +193,7 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory } if (response != null) success() - else error() + else Log.e("FFA", "performTokenRequest() not successful") } } } diff --git a/app/src/main/java/audio/funkwhale/ffa/utils/Util.kt b/app/src/main/java/audio/funkwhale/ffa/utils/Util.kt index 88c1b15..1a7f62a 100644 --- a/app/src/main/java/audio/funkwhale/ffa/utils/Util.kt +++ b/app/src/main/java/audio/funkwhale/ffa/utils/Util.kt @@ -3,6 +3,8 @@ package audio.funkwhale.ffa.utils import android.content.Context import android.widget.Toast import com.google.android.exoplayer2.util.Log +import com.google.android.exoplayer2.util.Log.LOG_LEVEL_ERROR +import com.google.android.exoplayer2.util.Log.LOG_LEVEL_INFO import com.preference.PowerPreference import java.net.URI @@ -31,10 +33,23 @@ private fun logClassName(): String { return "UNKNOWN" } -fun Any?.log(prefix: String? = null) { - prefix?.let { - Log.d("FFA", "${logClassName()} - $prefix: $this") - } ?: Log.d("FFA", "${logClassName()} - $this") +enum class LogLevel(value: Int) { + INFO(LOG_LEVEL_INFO), + DEBUG(Log.LOG_LEVEL_ALL), + ERROR(LOG_LEVEL_ERROR) +} + +fun Any?.logError(prefix: String? = null) = this.log(prefix, LogLevel.ERROR) +fun Any?.logInfo(prefix: String? = null) = this.log(prefix, LogLevel.INFO) + +fun Any?.log(prefix: String? = null, logLevel: LogLevel = LogLevel.DEBUG) { + val tag = "FFA" + val message = "${logClassName()} - ${prefix?.let { "$it: " }}$this" + when (logLevel) { + LogLevel.DEBUG -> Log.d(tag, message) + LogLevel.INFO -> Log.i(tag, message) + LogLevel.ERROR -> Log.e(tag, message) + } } fun maybeNormalizeUrl(rawUrl: String?): String? {