diff --git a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt index 9a71568ef..d24faa7d7 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -7,6 +7,7 @@ import android.view.KeyEvent import io.legado.app.constant.Bus import io.legado.app.help.ActivityHelp import io.legado.app.ui.book.read.ReadBookActivity +import io.legado.app.utils.LogUtils import io.legado.app.utils.postEvent @@ -21,6 +22,7 @@ class MediaButtonReceiver : BroadcastReceiver() { fun handleIntent(context: Context, intent: Intent): Boolean { val intentAction = intent.action + LogUtils.log("耳机按键:$intentAction") if (Intent.ACTION_MEDIA_BUTTON == intentAction) { val event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) ?: return false diff --git a/app/src/main/java/io/legado/app/utils/LogUtils.kt b/app/src/main/java/io/legado/app/utils/LogUtils.kt new file mode 100644 index 000000000..a0ddd9b77 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/LogUtils.kt @@ -0,0 +1,110 @@ +package io.legado.app.utils + +import android.annotation.SuppressLint +import io.legado.app.App +import io.legado.app.help.FileHelp +import java.io.File +import java.io.IOException +import java.text.SimpleDateFormat +import java.util.* +import java.util.logging.* +import java.util.logging.Formatter + + +object LogUtils { + + private val logger: Logger by lazy { + Logger.getGlobal().apply { + addFileHandler( + this, + Level.INFO, + FileHelp.getCachePath() + File.separator + System.currentTimeMillis() + ".log" + ) + } + } + + fun log(msg: String) { + if (App.INSTANCE.getPrefBoolean("recordLog")) { + logger.log(Level.INFO, msg) + } + } + + // 正常的日期格式 + const val DATE_PATTERN_FULL = "yyyy-MM-dd HH:mm:ss" + + /** + * 为log设置等级 + * + * @param log + * @param level + */ + fun setLogLevel(log: Logger, level: Level) { + log.level = level + } + + /** + * 为log添加控制台handler + * + * @param log + * 要添加handler的log + * @param level + * 控制台的输出等级 + */ + fun addConsoleHandler(log: Logger, level: Level) { + // 控制台输出的handler + val consoleHandler = ConsoleHandler() + // 设置控制台输出的等级(如果ConsoleHandler的等级高于或者等于log的level,则按照FileHandler的level输出到控制台,如果低于,则按照Log等级输出) + consoleHandler.level = level + + // 添加控制台的handler + log.addHandler(consoleHandler) + } + + /** + * 为log添加文件输出Handler + * + * @param log + * 要添加文件输出handler的log + * @param level + * log输出等级 + * @param filePath + * 指定文件全路径 + */ + fun addFileHandler(log: Logger, level: Level, filePath: String) { + var fileHandler: FileHandler? = null + try { + fileHandler = FileHandler(filePath) + // 设置输出文件的等级(如果FileHandler的等级高于或者等于log的level,则按照FileHandler的level输出到文件,如果低于,则按照Log等级输出) + fileHandler.level = level + fileHandler.formatter = object : Formatter() { + override fun format(record: LogRecord): String { + + // 设置文件输出格式 + return (getCurrentDateStr(DATE_PATTERN_FULL) + + " - Level:" + record.level.name.substring(0, 1) + + " " + record.message + "\n") + } + } + + } catch (e: SecurityException) { + e.printStackTrace() + } catch (e: IOException) { + e.printStackTrace() + } + + // 添加输出文件handler + log.addHandler(fileHandler) + } + + /** + * 获取当前时间 + * + * @return + */ + @SuppressLint("SimpleDateFormat") + fun getCurrentDateStr(pattern: String): String { + val date = Date() + val sdf = SimpleDateFormat(pattern) + return sdf.format(date) + } +} \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config.xml b/app/src/main/res/xml/pref_config.xml index a682044d0..a58063dd4 100644 --- a/app/src/main/res/xml/pref_config.xml +++ b/app/src/main/res/xml/pref_config.xml @@ -29,4 +29,9 @@ android:title="@string/download_path" app:iconSpaceReserved="false" /> + \ No newline at end of file