add new func of logcatch

This commit is contained in:
zhongchao
2021-12-16 19:20:47 +08:00
parent e8840a45fa
commit 311e5e0af1
32 changed files with 360 additions and 545 deletions

View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,69 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'com.alibaba.arouter'
}
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
// buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode Integer.valueOf(VERSION_CODE)
versionName getValueFromRootProperties("${project.name.replace("-", "_").toUpperCase()}_VERSION")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
//ARouter apt 参数
kapt {
useBuildCache = false
arguments {
arg("AROUTER_MODULE_NAME", project.getName())
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation rootProject.ext.dependencies.kotlinstdlibjdk7
implementation rootProject.ext.dependencies.coroutinescore
implementation rootProject.ext.dependencies.arouter
kapt rootProject.ext.dependencies.aroutercompiler
implementation rootProject.ext.dependencies.mogologlib
if (Boolean.valueOf(USE_MAVEN_PACKAGE)) {
implementation rootProject.ext.dependencies.mogoserviceapi
implementation rootProject.ext.dependencies.modulecommon
implementation rootProject.ext.dependencies.mogo_core_utils
implementation rootProject.ext.dependencies.mogo_core_function_api
implementation rootProject.ext.dependencies.mogo_core_function_call
implementation rootProject.ext.dependencies.mogo_core_data
}else {
implementation project(':services:mogo-service-api')
implementation project(':modules:mogo-module-common')
implementation project(':core:mogo-core-utils')
implementation project(':core:mogo-core-function-api')
implementation project(':core:mogo-core-function-call')
implementation project(':core:mogo-core-data')
}
}

View File

@@ -0,0 +1,3 @@
GROUP=com.mogo.eagle.core.function.impl
POM_ARTIFACT_ID=devatools
VERSION_CODE=1

View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhjt.mogo_core_function_devatools">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Launcher" />
</manifest>

View File

@@ -0,0 +1,31 @@
package com.zhjt.mogo_core_function_devatools
import android.content.Context
import com.alibaba.android.arouter.facade.annotation.Route
import com.mogo.eagle.core.data.constants.MogoServicePaths
import com.mogo.eagle.core.function.api.devatools.IDevaToolsProvider
import com.zhjt.mogo_core_function_devatools.logcatch.MogoLogCatchManager
@Route(path = MogoServicePaths.PATH_DEVA_TOOLS)
class DevaToolsProvider : IDevaToolsProvider {
override val functionName: String
get() = "DevaToolsProvider"
override fun init(context: Context) {
MogoLogCatchManager.init(context)
}
override fun startLogCatch() {
MogoLogCatchManager.startCatchLog()
}
override fun stopLogCatch() {
MogoLogCatchManager.stopCatchLog()
}
override fun onDestroy() {
MogoLogCatchManager.onDestroy()
}
}

View File

@@ -0,0 +1,28 @@
package com.zhjt.mogo_core_function_devatools.logcatch
class MogoLogCatchConst {
companion object{
const val LOG_PUSH_TYPE = 500000
/**
* 开始抓日志
*/
const val START_CATCH_LOG = 1
/**
* 结束抓日志
*/
const val STOP_CATCH_LOG = 2
/**
* 本应用设置,打开日志
*/
const val LOCAL_CONFIG_OPEN_LOG = 3
/**
* 本应用设置,关闭日志
*/
const val LOCAL_CONFIG_CLOSE_LOG = 4
}
}

View File

@@ -0,0 +1,162 @@
package com.zhjt.mogo_core_function_devatools.logcatch
import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Message
import com.mogo.commons.AbsMogoApplication
import com.mogo.commons.debug.DebugConfig
import com.mogo.eagle.core.function.call.devatools.CallDevaToolsListenerManager
import com.mogo.eagle.core.utilcode.mogo.toast.TipToast
import com.mogo.eagle.core.utilcode.util.ThreadUtils
import com.mogo.module.common.MogoApisHandler
import com.mogo.service.cloud.socket.IMogoOnMessageListener
import com.mogo.utils.logger.LogLevel
import com.mogo.utils.logger.Logger
import com.mogo.utils.network.NetConfig
import com.zhidao.loglib.bean.RemoteLogPushContent
import com.zhidao.loglib.call.LogInfoManagerFactory
import com.zhidao.loglib.core.ILogListener
import com.zhidao.loglib.core.LogInfoManager
import com.zhjt.mogo_core_function_devatools.logcatch.MogoLogCatchConst.Companion.LOCAL_CONFIG_CLOSE_LOG
import com.zhjt.mogo_core_function_devatools.logcatch.MogoLogCatchConst.Companion.LOCAL_CONFIG_OPEN_LOG
import com.zhjt.mogo_core_function_devatools.logcatch.MogoLogCatchConst.Companion.LOG_PUSH_TYPE
import com.zhjt.mogo_core_function_devatools.logcatch.MogoLogCatchConst.Companion.START_CATCH_LOG
import com.zhjt.mogo_core_function_devatools.logcatch.MogoLogCatchConst.Companion.STOP_CATCH_LOG
@SuppressLint("StaticFieldLeak")
object MogoLogCatchManager : IMogoOnMessageListener<RemoteLogPushContent>, Handler.Callback,
ILogListener {
private const val TAG = "MogoLogCatchManager"
private const val MANUAL_CATCH_PKG_NAME = "manual-catch-log"
private const val MSG_TRY_CLOSE_LOG = 1001
private var mContext: Context? = null
private val handler = Handler(this)
private val manualContent = RemoteLogPushContent()
private val catchingList: MutableList<String> = mutableListOf()
private var logInfoManager: LogInfoManager? = null
fun init(context: Context) {
mContext = context
MogoApisHandler.getInstance().apis
.getSocketManagerApi(AbsMogoApplication.getApp().applicationContext)
.registerOnMessageListener(LOG_PUSH_TYPE, this)
manualContent.duration = 60
manualContent.pkgName = MANUAL_CATCH_PKG_NAME
}
override fun target(): Class<RemoteLogPushContent> {
return RemoteLogPushContent::class.java
}
override fun onMsgReceived(obj: RemoteLogPushContent?) {
obj?.let {
Logger.d(TAG, "收到push消息: $obj")
when (obj.type) {
START_CATCH_LOG -> if (!catchingList.contains(obj.pkgName)) {
startCatchLog(obj)
}
STOP_CATCH_LOG -> stopCatchLog(obj)
LOCAL_CONFIG_OPEN_LOG -> openLoggerLevel()
LOCAL_CONFIG_CLOSE_LOG -> closeLoggerLevel()
else -> {
}
}
}
}
override fun handleMessage(msg: Message): Boolean {
if (msg.what == MSG_TRY_CLOSE_LOG) {
CallDevaToolsListenerManager.invokeDevaToolsLogCatch()
closeLoggerLevel()
return true
}
return false
}
fun startCatchLog() {
if (catchingList.contains(MANUAL_CATCH_PKG_NAME)) {
TipToast.shortTip("已经在抓取日志了,请稍后再试")
} else {
Logger.d(TAG, "开始抓取日志====")
manualContent.type = START_CATCH_LOG
startCatchLog(manualContent)
}
}
fun stopCatchLog() {
Logger.d(TAG, "结束抓取日志====")
manualContent.type = STOP_CATCH_LOG
stopCatchLog(manualContent)
}
private fun startCatchLog(content: RemoteLogPushContent) {
catchingList.add(content.pkgName)
var delay = (content.duration * 60 * 1000).toLong()
handler.removeMessages(MSG_TRY_CLOSE_LOG)
if (delay <= 0) {
// 如果push 下来的delay小于等于0那就给个默认最大值一小时
delay = 60 * 60 * 1000L
}
handler.sendEmptyMessageDelayed(MSG_TRY_CLOSE_LOG, delay)
openLoggerLevel()
logInfoManager = LogInfoManagerFactory.createPushLogInfoManager(mContext, content, this)
logInfoManager?.start()
}
private fun stopCatchLog(content: RemoteLogPushContent) {
catchingList.remove(content.pkgName)
if (catchingList.isEmpty()) {
handler.removeMessages(MSG_TRY_CLOSE_LOG)
}
logInfoManager?.stop()
logInfoManager = null
closeLoggerLevel()
}
/**
* 放开Logger的限制
*/
private fun openLoggerLevel() {
Logger.init(LogLevel.DEBUG)
}
/**
* 根据状态收紧Logger的限制
*/
private fun closeLoggerLevel() {
if (catchingList.isNotEmpty()) {
Logger.init(if (DebugConfig.isDebug()) LogLevel.DEBUG else LogLevel.OFF)
NetConfig.instance().isLoggable = DebugConfig.isDebug()
}
}
override fun onError(errorCount: Int) {
ThreadUtils.runOnUiThread {
TipToast.shortTip("日志抓取出现错误,出错数量:$errorCount")
}
}
override fun onClose(pkgName: String?) {
ThreadUtils.runOnUiThread {
CallDevaToolsListenerManager.invokeDevaToolsLogCatch()
TipToast.shortTip("日志抓取默认计时结束")
}
}
fun onDestroy() {
MogoApisHandler.getInstance().apis
.getSocketManagerApi(AbsMogoApplication.getApp().applicationContext)
.unregisterLifecycleListener(LOG_PUSH_TYPE)
if (handler.hasMessages(MSG_TRY_CLOSE_LOG)) {
handler.removeMessages(MSG_TRY_CLOSE_LOG)
}
manualContent.type = 0
logInfoManager = null
mContext = null
}
}