Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Mar 2, 2024
1 parent 483ed94 commit 6b5c32a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
20 changes: 20 additions & 0 deletions app/src/main/java/io/legado/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.res.Configuration
import android.os.Build
import com.github.liuyueyi.quick.transfer.constants.TransType
import com.jeremyliao.liveeventbus.LiveEventBus
import com.jeremyliao.liveeventbus.logger.DefaultLogger
import io.legado.app.base.AppContextWrapper
import io.legado.app.constant.AppConst.channelIdDownload
import io.legado.app.constant.AppConst.channelIdReadAloud
Expand Down Expand Up @@ -40,6 +41,7 @@ import splitties.init.appCtx
import splitties.systemservices.notificationManager
import java.net.URL
import java.util.concurrent.TimeUnit
import java.util.logging.Level

class App : Application() {

Expand All @@ -57,6 +59,7 @@ class App : Application() {
.lifecycleObserverAlwaysActive(true)
.autoClear(false)
.enableLogger(BuildConfig.DEBUG || AppConfig.recordLog)
.setLogger(EventLogger())
applyDayNight(this)
registerActivityLifecycleCallbacks(LifecycleHelp)
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig)
Expand Down Expand Up @@ -178,4 +181,21 @@ class App : Application() {
)
}

class EventLogger : DefaultLogger() {

override fun log(level: Level, msg: String) {
super.log(level, msg)
LogUtils.d(TAG, msg)
}

override fun log(level: Level, msg: String, th: Throwable?) {
super.log(level, msg, th)
LogUtils.d(TAG, "$msg\n${th?.stackTraceToString()}")
}

companion object {
private const val TAG = "[LiveEventBus]"
}
}

}
4 changes: 3 additions & 1 deletion app/src/main/java/io/legado/app/base/BaseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ abstract class BaseService : LifecycleService() {

@CallSuper
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
LogUtils.d(simpleName, "onStartCommand $intent")
LogUtils.d(simpleName) {
"onStartCommand $intent ${intent?.toUri(0)}"
}
startForegroundNotification()
return super.onStartCommand(intent, flags, startId)
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/io/legado/app/model/ReadBook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ object ReadBook : CoroutineScope by MainScope() {
* 朗读
*/
fun readAloud(play: Boolean = true, startPos: Int = 0) {
book?.let {
book ?: return
if (isLayoutAvailable) {
ReadAloud.play(appCtx, play, startPos = startPos)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ data class TextChapter(
pageSplit: Boolean,
): Int {
val paragraphs = if (pageSplit) {
pageParagraphs
if (isCompleted) pageParagraphs else pageParagraphsInternal
} else {
paragraphs
if (isCompleted) paragraphs else pageParagraphsInternal
}
paragraphs.forEach { paragraph ->
if (position in paragraph.chapterIndices) {
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/io/legado/app/utils/LogUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ object LogUtils {
logger.log(Level.INFO, "$tag $msg")
}

inline fun d(tag: String, lazyMsg: () -> String) {
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO, "$tag ${lazyMsg()}")
}
}

@JvmStatic
fun e(tag: String, msg: String) {
logger.log(Level.WARNING, "$tag $msg")
}

private val logger: Logger by lazy {
val logger: Logger by lazy {
Logger.getGlobal().apply {
fileHandler?.let {
addHandler(it)
Expand Down

0 comments on commit 6b5c32a

Please sign in to comment.