From 5fe520e5753bd559133b0b593ae24dd1abeae688 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Mon, 12 Feb 2024 23:52:04 +0800 Subject: [PATCH] Cleanup `Log` usage and make logging configurable --- .../kotlin/com/hippo/ehviewer/EhApplication.kt | 4 +++- .../ehviewer/client/parser/ProfileParser.kt | 5 ++--- .../hippo/ehviewer/download/DownloadManager.kt | 18 +++++++++--------- .../hippo/ehviewer/download/DownloadService.kt | 6 +++--- .../ehviewer/gallery/ArchivePageLoader.kt | 5 ++--- .../com/hippo/ehviewer/spider/SpiderQueen.kt | 9 ++++----- 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt b/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt index 644071e66..d3b327af8 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/EhApplication.kt @@ -89,6 +89,9 @@ class EhApplication : Application(), SingletonImageLoader.Factory { AppCompatDelegate.setDefaultNightMode(mode) } } + if (!LogcatLogger.isInstalled && Settings.saveCrashLog) { + LogcatLogger.install(AndroidLogcatLogger(LogPriority.VERBOSE)) + } } lifecycle.addObserver(lockObserver) val handler = Thread.getDefaultUncaughtExceptionHandler() @@ -105,7 +108,6 @@ class EhApplication : Application(), SingletonImageLoader.Factory { System.loadLibrary("ehviewer") System.loadLibrary("ehviewer_rust") ReadableTime.initialize(this) - LogcatLogger.install(AndroidLogcatLogger(LogPriority.VERBOSE)) lifecycleScope.launchIO { launchIO { EhTagDatabase diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/ProfileParser.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/ProfileParser.kt index 277436f9a..037c0ef30 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/ProfileParser.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/ProfileParser.kt @@ -15,14 +15,13 @@ */ package com.hippo.ehviewer.client.parser -import android.util.Log import com.hippo.ehviewer.client.EhUrl import com.hippo.ehviewer.client.exception.ParseException import com.hippo.ehviewer.util.ExceptionUtils +import eu.kanade.tachiyomi.util.system.logcat import org.jsoup.Jsoup object ProfileParser { - private val TAG = ProfileParser::class.java.simpleName fun parse(body: String): Result { return runCatching { val d = Jsoup.parse(body) @@ -40,7 +39,7 @@ object ProfileParser { } }.getOrElse { ExceptionUtils.throwIfFatal(it) - Log.i(TAG, "No avatar") + logcat { "No avatar" } null } Result(displayName, avatar) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/download/DownloadManager.kt b/app/src/main/kotlin/com/hippo/ehviewer/download/DownloadManager.kt index 9e4486a17..bbe695380 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/download/DownloadManager.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/download/DownloadManager.kt @@ -16,7 +16,6 @@ package com.hippo.ehviewer.download import android.net.Uri -import android.util.Log import android.util.SparseLongArray import androidx.compose.runtime.Composable import androidx.compose.runtime.DisallowComposableCalls @@ -53,6 +52,7 @@ import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.transform import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock +import logcat.LogPriority import splitties.preferences.edit object DownloadManager : OnSpiderListener { @@ -200,7 +200,7 @@ object DownloadManager : OnSpiderListener { // Add to label download list val list = getInfoListForLabel(info.label) if (list == null) { - Log.e(TAG, "Can't find download info list with label: $label") + logcat(TAG, LogPriority.ERROR) { "Can't find download info list with label: $label" } return } list.add(0, info) @@ -548,12 +548,12 @@ object DownloadManager : OnSpiderListener { */ suspend fun changeLabel(list: Collection, label: String?) { if (null != label && !containLabel(label)) { - Log.e(TAG, "Not exits label: $label") + logcat(TAG, LogPriority.ERROR) { "Not exits label: $label" } return } val dstList = getInfoListForLabel(label) if (dstList == null) { - Log.e(TAG, "Can't find label with label: $label") + logcat(TAG, LogPriority.ERROR) { "Can't find label with label: $label" } return } for (info in list) { @@ -562,7 +562,7 @@ object DownloadManager : OnSpiderListener { } val srcList = getInfoListForLabel(info.label) if (srcList == null) { - Log.e(TAG, "Can't find label with label: " + info.label) + logcat(TAG, LogPriority.ERROR) { "Can't find label with label: " + info.label } continue } srcList.remove(info) @@ -800,7 +800,7 @@ object DownloadManager : OnSpiderListener { TYPE_ON_GET_PAGES -> { val info = mCurrentTask if (info == null) { - Log.e(TAG, "Current task is null, but it should not be") + logcat(TAG, LogPriority.ERROR) { "Current task is null, but it should not be" } } else { info.total = mPages launchIO { @@ -826,7 +826,7 @@ object DownloadManager : OnSpiderListener { mSpeedReminder.onDone(mIndex) val info = mCurrentTask if (info == null) { - Log.e(TAG, "Current task is null, but it should not be") + logcat(TAG, LogPriority.ERROR) { "Current task is null, but it should not be" } } else { info.finished = mFinished info.downloaded = mDownloaded @@ -844,7 +844,7 @@ object DownloadManager : OnSpiderListener { mSpeedReminder.onDone(mIndex) val info = mCurrentTask if (info == null) { - Log.e(TAG, "Current task is null, but it should not be") + logcat(TAG, LogPriority.ERROR) { "Current task is null, but it should not be" } } else { info.finished = mFinished info.downloaded = mDownloaded @@ -869,7 +869,7 @@ object DownloadManager : OnSpiderListener { } // Check null if (info == null || spider == null) { - Log.e(TAG, "Current stuff is null, but it should not be") + logcat(TAG, LogPriority.ERROR) { "Current stuff is null, but it should not be" } } else { // Stop speed count mSpeedReminder.stop() diff --git a/app/src/main/kotlin/com/hippo/ehviewer/download/DownloadService.kt b/app/src/main/kotlin/com/hippo/ehviewer/download/DownloadService.kt index e3528300d..a7c899fc4 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/download/DownloadService.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/download/DownloadService.kt @@ -22,7 +22,6 @@ import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle import android.os.SystemClock -import android.util.Log import androidx.annotation.IntDef import androidx.collection.LongSparseArray import androidx.core.app.ActivityCompat @@ -40,6 +39,7 @@ import com.hippo.ehviewer.util.FileUtils import com.hippo.ehviewer.util.ReadableTime import com.hippo.ehviewer.util.SimpleHandler import com.hippo.ehviewer.util.getParcelableExtraCompat +import eu.kanade.tachiyomi.util.system.logcat import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob @@ -339,7 +339,7 @@ class DownloadService : Service(), DownloadManager.DownloadListener, CoroutineSc sItemTitleArray.valueAt(0), ) } else { - Log.d("TAG", "WTF, sItemTitleArray is null") + logcat { "WTF, sItemTitleArray is null" } getString(R.string.error_unknown) } needStyle = false @@ -355,7 +355,7 @@ class DownloadService : Service(), DownloadManager.DownloadListener, CoroutineSc sItemTitleArray.valueAt(0), ) } else { - Log.d("TAG", "WTF, sItemTitleArray is null") + logcat { "WTF, sItemTitleArray is null" } getString(R.string.error_unknown) } needStyle = false diff --git a/app/src/main/kotlin/com/hippo/ehviewer/gallery/ArchivePageLoader.kt b/app/src/main/kotlin/com/hippo/ehviewer/gallery/ArchivePageLoader.kt index 901acbdaf..e52b57811 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/gallery/ArchivePageLoader.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/gallery/ArchivePageLoader.kt @@ -16,7 +16,6 @@ package com.hippo.ehviewer.gallery import android.os.ParcelFileDescriptor -import android.util.Log import com.hippo.ehviewer.Settings.archivePasswds import com.hippo.ehviewer.image.ByteBufferSource import com.hippo.ehviewer.image.Image @@ -54,7 +53,7 @@ class ArchivePageLoader( ) : PageLoader2(gid, startPage) { private lateinit var pfd: ParcelFileDescriptor private val hostJob = launch(start = CoroutineStart.LAZY) { - Log.d(DEBUG_TAG, "Open archive ${file.uri.displayPath}") + logcat(DEBUG_TAG) { "Open archive ${file.uri.displayPath}" } pfd = file.openFileDescriptor("r") size = openArchive(pfd.fd, pfd.statSize) if (size == 0) { @@ -82,7 +81,7 @@ class ArchivePageLoader( super.stop() closeArchive() pfd.close() - Log.d(DEBUG_TAG, "Close archive ${file.uri.displayPath} successfully!") + logcat(DEBUG_TAG) { "Close archive ${file.uri.displayPath} successfully!" } } private val mJobMap = hashMapOf() diff --git a/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderQueen.kt b/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderQueen.kt index 4d59c3dc7..6d510777e 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderQueen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/spider/SpiderQueen.kt @@ -16,7 +16,6 @@ */ package com.hippo.ehviewer.spider -import android.util.Log import androidx.annotation.IntDef import androidx.collection.LongSparseArray import androidx.collection.set @@ -551,7 +550,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS }.onFailure { if (it is CancellationException) { if (mReadReference > 0) { - Log.d(WORKER_DEBUG_TAG, "Download image $index cancelled") + logcat(WORKER_DEBUG_TAG) { "Download image $index cancelled" } if (it.message != FORCE_RETRY) { updatePageState(index, STATE_FAILED, "Cancelled") } @@ -677,7 +676,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS repeat(2) { times -> runCatching { - Log.d(WORKER_DEBUG_TAG, "Start download image $index attempt #$times") + logcat(WORKER_DEBUG_TAG) { "Start download image $index attempt #$times" } val success = withTimeout(downloadTimeout) { mSpiderDen.makeHttpCallAndSaveImage( index, @@ -688,12 +687,12 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS } check(success) - Log.d(WORKER_DEBUG_TAG, "Download image $index succeed") + logcat(WORKER_DEBUG_TAG) { "Download image $index succeed" } updatePageState(index, STATE_FINISHED) return }.onFailure { mSpiderDen.remove(index) - Log.d(WORKER_DEBUG_TAG, "Download image $index attempt #$times failed") + logcat(WORKER_DEBUG_TAG) { "Download image $index attempt #$times failed" } error = when (it) { is TimeoutCancellationException -> ERROR_TIMEOUT is CancellationException -> throw it