android中关于logcat的管理

1.一般来说,专业的程序开发,在developer 阶段,让logcat 在控制台显示,在发布阶段,就屏蔽logcat的打印:

具体做法:

import android.util.Log;


/**
 * 开发阶段,让所有的log 都可见,发布时,改变 state的值,达到屏蔽程序的logcat
 *
 */
public class Logger {


public static int state = 0;

public static void v (String tag , String msg) {
if(state <=5){
Log.v(tag, msg);
}
}

public static void d (String tag , String msg) {
if(state <=4){
Log.d(tag, msg);
}
}
public static void i (String tag , String msg) {
if(state <=3){
Log.i(tag, msg);
}
}

public static void w (String tag , String msg) {
if(state <=2){
Log.w(tag, msg);
}
}

public static void e (String tag , String msg) {
if(state <=1){
Log.e(tag, msg);
}
}

}


2.如果想得到 其他程序在控制台打印的 logcat ,如下:

2.1 首先介绍  logcat的分类:

Main  ----> 打印的是 主节点 信息,即 默认状态下 系统打印子在控制台的  信息

Events -----》打印 系统 事件的信息  click  move  touch 等等

Radio ------> 打印的是电话状态的信息,录音等等

System-----》系统信息

main - the main application log
events - for system event information
radio - for radio and phone-related informatio
system - a log for low-level system messages and debugging

注意:the logging system does store messages in kernel buffers.

详情(http://elinux.org/Android_Logging_Syste

2.2 可以通过对文件的读写操作 将 logcat 的信息 获取出来:

try {
Process process = Runtime.getRuntime().exec("adb -b Radio");
InputStream in = process.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in));

FileOutputStream out = openFileOutput("log", Context.MODE_PRIVATE);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
String str = null;
while ((str=read.readLine()) != null){
writer.write(str);
}

} catch (Exception e) {
e.printStackTrace();
}

然后  通过 正则表到式( regexpress  ) 将需要的信息给获取到




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章