增加日志抓取功能
This commit is contained in:
@@ -17,9 +17,5 @@ class MogoMonitorConst {
|
||||
*/
|
||||
public static final int LOCAL_CONFIG_CLOSE_LOG = 4;
|
||||
|
||||
|
||||
public static final String BROADCAST_START_CATCH_LOG = "com.mogo.controller.action" +
|
||||
".START_CATCH_LOG";
|
||||
public static final String BROADCAST_STOP_CATCH_LOG = "com.mogo.controller.action" +
|
||||
".STOP_CATCH_LOG";
|
||||
public static final String BROADCAST_LOG_CTRL = "com.mogo.control.action.LOG_CTRL";
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.zhidao.mogo.module.monitor;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.ArrayMap;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.alibaba.android.arouter.facade.annotation.Route;
|
||||
import com.alibaba.android.arouter.launcher.ARouter;
|
||||
@@ -14,11 +17,13 @@ import com.mogo.service.monitor.IMogoMonitorProvider;
|
||||
import com.mogo.utils.logger.LogLevel;
|
||||
import com.mogo.utils.logger.Logger;
|
||||
import com.mogo.utils.network.NetConfig;
|
||||
import com.mogo.utils.network.utils.GsonUtil;
|
||||
import com.zhidao.mogo.module.monitor.bean.RemoteLogPushContent;
|
||||
import com.zhidao.mogo.module.monitor.dialog.ILogDialogListener;
|
||||
import com.zhidao.mogo.module.monitor.dialog.LogDebugDialog;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 应用监控模块provider
|
||||
@@ -27,10 +32,15 @@ import java.util.Map;
|
||||
*/
|
||||
@Route(path = MogoMonitorConst.MODULE_PATH)
|
||||
public class MogoMonitorProvider implements IMogoMonitorProvider,
|
||||
IMogoOnMessageListener<RemoteLogPushContent>, ILogDialogListener {
|
||||
IMogoOnMessageListener<RemoteLogPushContent>, ILogDialogListener, Handler.Callback {
|
||||
private static final String TAG = MogoMonitorConst.MODULE_NAME;
|
||||
private static final int MSG_TRY_CLOSE_LOG = 1001;
|
||||
private static final String MANUAL_CATCH_PKG_NAME = "manual-catch-log";
|
||||
private Context context;
|
||||
private LogDebugDialog logDebugDialog;
|
||||
private RemoteLogPushContent manualContent = new RemoteLogPushContent(60,
|
||||
MANUAL_CATCH_PKG_NAME);
|
||||
private Handler handler = new Handler(this);
|
||||
@Override
|
||||
public void showLogDebugDialog() {
|
||||
logDebugDialog.show();
|
||||
@@ -43,11 +53,22 @@ public class MogoMonitorProvider implements IMogoMonitorProvider,
|
||||
apis.getSocketManagerApi(context).registerOnMessageListener(MogoMonitorConst.LOG_PUSH_TYPE, this);
|
||||
}
|
||||
|
||||
private void startRemoteCtrl(){
|
||||
Intent intent = new Intent("com.mogo.remotecontrol.action");
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
ComponentName comp = new ComponentName("com.mogo.remotecontrol",
|
||||
"com.mogo.remotecontrol.RemoteCtrlService");
|
||||
intent.setComponent(comp);
|
||||
context.startService(intent);
|
||||
Logger.d(TAG, "startRemoteCtrl");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetActivityContext(Context context) {
|
||||
this.context = context;
|
||||
logDebugDialog = new LogDebugDialog(context);
|
||||
logDebugDialog.setDialogListener(this);
|
||||
startRemoteCtrl();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,10 +81,12 @@ public class MogoMonitorProvider implements IMogoMonitorProvider,
|
||||
Logger.d(TAG, "收到push消息: " + obj);
|
||||
switch (obj.getType()) {
|
||||
case MogoMonitorConst.START_CATCH_LOG:
|
||||
onLogStart();
|
||||
if(!catchingList.contains(obj.getPkgName())){
|
||||
startCatchLog(obj);
|
||||
}
|
||||
break;
|
||||
case MogoMonitorConst.STOP_CATCH_LOG:
|
||||
onLogStop();
|
||||
stopCatchLog(obj);
|
||||
break;
|
||||
case MogoMonitorConst.LOCAL_CONFIG_OPEN_LOG:
|
||||
openLoggerLevel();
|
||||
@@ -76,20 +99,37 @@ public class MogoMonitorProvider implements IMogoMonitorProvider,
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> catchingList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onLogStart() {
|
||||
// 这个是通过对话框点击开始的回调
|
||||
Logger.d(TAG,"开始抓取日志====");
|
||||
context.sendBroadcast(new Intent(MogoMonitorConst.BROADCAST_START_CATCH_LOG));
|
||||
openLoggerLevel();
|
||||
if(catchingList.contains(MANUAL_CATCH_PKG_NAME)){
|
||||
Toast.makeText(context, "已经在抓日志了", Toast.LENGTH_LONG).show();
|
||||
}else {
|
||||
Logger.d(TAG, "开始抓取日志====");
|
||||
manualContent.setType(MogoMonitorConst.START_CATCH_LOG);
|
||||
startCatchLog(manualContent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLogStop() {
|
||||
// 这个是通过对话框点击结束的回调
|
||||
Logger.d(TAG,"结束抓取日志====");
|
||||
context.sendBroadcast(new Intent(MogoMonitorConst.BROADCAST_STOP_CATCH_LOG));
|
||||
closeLoggerLevel();
|
||||
manualContent.setType(MogoMonitorConst.STOP_CATCH_LOG);
|
||||
stopCatchLog(manualContent);
|
||||
}
|
||||
|
||||
public void sendCtrlBroadcast(RemoteLogPushContent content) {
|
||||
startRemoteCtrl();
|
||||
|
||||
Intent intent = new Intent(MogoMonitorConst.BROADCAST_LOG_CTRL);
|
||||
intent.putExtra("content", GsonUtil.jsonFromObject(content));
|
||||
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
|
||||
intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
Logger.d(TAG, "sendCtrlBroadcast: " + content);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,7 +143,41 @@ public class MogoMonitorProvider implements IMogoMonitorProvider,
|
||||
* 根据状态收紧Logger的限制
|
||||
*/
|
||||
private void closeLoggerLevel() {
|
||||
Logger.init(DebugConfig.isDebug() ? LogLevel.DEBUG : LogLevel.OFF);
|
||||
NetConfig.instance().setLoggable(DebugConfig.isDebug());
|
||||
if(!catchingList.isEmpty()) {
|
||||
Logger.init(DebugConfig.isDebug() ? LogLevel.DEBUG : LogLevel.OFF);
|
||||
NetConfig.instance().setLoggable(DebugConfig.isDebug());
|
||||
}
|
||||
}
|
||||
|
||||
private void startCatchLog(RemoteLogPushContent content){
|
||||
catchingList.add(content.getPkgName());
|
||||
|
||||
long delay = content.getDuration() * 60 * 1000;
|
||||
handler.removeMessages(MSG_TRY_CLOSE_LOG);
|
||||
if (delay <= 0) {
|
||||
// 如果push 下来的delay小于等于0,那就给个默认最大值一小时
|
||||
delay = 60 * 60 * 1000;
|
||||
}
|
||||
handler.sendEmptyMessageDelayed(MSG_TRY_CLOSE_LOG, delay);
|
||||
openLoggerLevel();
|
||||
sendCtrlBroadcast(content);
|
||||
}
|
||||
|
||||
private void stopCatchLog(RemoteLogPushContent content) {
|
||||
catchingList.remove(content.getPkgName());
|
||||
if (catchingList.isEmpty()) {
|
||||
handler.removeMessages(MSG_TRY_CLOSE_LOG);
|
||||
}
|
||||
sendCtrlBroadcast(content);
|
||||
closeLoggerLevel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleMessage(Message msg) {
|
||||
if (msg.what == MSG_TRY_CLOSE_LOG) {
|
||||
closeLoggerLevel();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,15 @@ public class RemoteLogPushContent {
|
||||
private String cmd;
|
||||
private String pkgName;
|
||||
|
||||
public RemoteLogPushContent(){
|
||||
|
||||
}
|
||||
|
||||
public RemoteLogPushContent(int duration, String pkgName) {
|
||||
this.duration = duration;
|
||||
this.pkgName = pkgName;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user