解决Android启动过程中Log丢失问题的方案

在Android开发中,遇到需要Debug启动过程中的问题时,往往会因为在启动过程中log丢失而异常麻烦,为此我们用了下面这个简单的方案。

假设你的应用或者framework的某些服务需要在开机时越快启动越好,那么在启动过程中某些log会丢失,但是程序的逻辑不会丢失,为此我们可以把需要分析的关键部位的log缓存在内存中,等到问题出现后系统稳定后再通过某些方式把缓存的log信息读出来,因为log是存在内存中的,所以不会有任何的丢失。

为了做到程序的灵活性,我们使用了检测文件是否存在来打开和关闭缓存功能,这样我们可以缓存功能的代码嵌入到程序本身,平时它都不会运行,只有当需要的时候,在特定的目录下建立一个文件后,它才会运行。同时为了不改变应用程序本身的代码就能读取缓存的信息,我们使用接收广播的形式来打印,下面是程序的源码,欢迎提供你的宝贵意见和建议:

package ;

import java.io.File;

import java.text.SimpleDateFormat;

import java.util.Date;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Handler;

import android.os.Message;

import android.os.Process;

import android.util.Log;

public class LogAssist {

private static final String TAG = "LogAssist";

//control command

private static final String ACTION_PRINT = "action.logassist.print";

private static final String ACTION_CONTROL_PAUSE = "action.logassist.control.pause";

private static final String ACTION_CONTROL_RESUME = "action.logassist.control.resume";

private static final String ACTION_CONTROL_CLEAR = "action.logassist.control.clear";

//delay time to print the buffered log (ms)

private static final String ACTION_EXTRA_DELAY2PRINT = "delay";

//msg to print the log

private static final int MSG_PRINT = 2008;

//switch the log assist on or off by adding/removing this file

private static final String SWITCHER_FILE = "/data/data/com./logassist_enabled";

//maxium buffer used to cache the log

private static final int MAX_BUFFER_SIZE = 10241000;

//buffer size to clear when it is full

private static final int BUFFER_SIZE_TO_CLEAR = 512;

//start line of the log

private static final String STR_LOG_START_LINE = "----------------LogAssist Print------------\n";

static StringBuffer mLogBuffer;

static SimpleDateFormat mDateFormat;

static boolean mStarted = false;

static int mPid;

static Handler mPrintHandler = null;

public static void init(Context context) {

if (!needStart()) {

Log.d(TAG, "LogAssist is not enabled.");

return;

}

mLogBuffer = new StringBuffer(STR_LOG_START_LINE);

mDateFormat = new SimpleDateFormat("dd-M-yyyy hh:mm:ss.SSS");

mStarted = true;

mPid = Process.myPid();

registerControlReceiver(context);

Log.d(TAG, "LogAssist is enabled.");

}

static class PrintHandlerCallBack implements Handler.Callback {

@Override

public boolean handleMessage(Message msg) {

switch (msg.what) {

case MSG_PRINT:

print();

break;

}

return false;

}

}

private static void registerControlReceiver(Context context) {

BroadcastReceiver controlReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

Log.d(TAG, "received intent :"+intent.getAction());

if (intent == null) {

return;

}

String action = intent.getAction();

if (ACTION_PRINT.equals(action)) {

int delay = intent.getIntExtra(ACTION_EXTRA_DELAY2PRINT, 0);

Log.d(TAG, " intent :delay="+delay);

if (delay <=0) {

print();

} else {

if (mPrintHandler == null) {

mPrintHandler = new Handler(new PrintHandlerCallBack());

}

mPrintHandler.sendEmptyMessageDelayed(MSG_PRINT, delay);

}

} else if (ACTION_CONTROL_PAUSE.equals(action)) {

mStarted = false;

}else if (ACTION_CONTROL_RESUME.equals(action)) {

mStarted = true;

}else if (ACTION_CONTROL_CLEAR.equals(action)) {

mLogBuffer = new StringBuffer(STR_LOG_START_LINE);

}

}

};

IntentFilter ctrlIntentFilter = new IntentFilter();

ctrlIntentFilter.addAction(ACTION_PRINT);

ctrlIntentFilter.addAction(ACTION_CONTROL_PAUSE);

ctrlIntentFilter.addAction(ACTION_CONTROL_RESUME);

ctrlIntentFilter.addAction(ACTION_CONTROL_CLEAR);

context.registerReceiver(controlReceiver, ctrlIntentFilter);

}

//check the switch file to see we need run log assist

private static boolean needStart() {

File switcher = new File(SWITCHER_FILE);

return switcher.exists();

}

public static void log(String info) {

if (!mStarted) {

return;

}

if (mLogBuffer.length() >= MAX_BUFFER_SIZE) {

//if buffer is full, remove first part defined by BUFFER_SIZE_TO_CLEAR

if (mLogBuffer.length() > BUFFER_SIZE_TO_CLEAR) {

//if the cached size is bigger than size to be cleared

mLogBuffer.delete(0, BUFFER_SIZE_TO_CLEAR);

} else {

//if the cached size is not bigger than size to be cleared, reset the buffer

mLogBuffer = new StringBuffer(STR_LOG_START_LINE);

}

}

String date = mDateFormat.format(new Date());

int tid = Process.myTid();

mLogBuffer.append(date + "  " + mPid + "  " + tid + "    " + info + "\n");

}

private static void print() {

if (mLogBuffer != null) {

Log.i(TAG, mLogBuffer.toString());

}

}

}


源码可从此下载: https://github.com/belyxiong/LogAssist

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,730评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,422评论 19 139
  • 已记不得这是第几个失眠的夜晚了,时间过了大半年,可是突然之间的想起依旧会隐隐作痛,失眠至凌晨。我曾以为自己会很...
    忆晓阅读 1,280评论 0 0
  • 天元大地,混沌初开,便有了神和动物。神,其实算是天地之间的第一户人家。他们天生神力,便被后人称为“神”。既然天地之...
    JMWXYCS阅读 3,255评论 0 0
  • 昨日 阳光灿烂 今日 阴雨绵绵 明日 又将如何 人生 最大的悲哀 犹如这天气 面对无常 只道是平常
    翦梦阅读 1,352评论 3 2