欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android将应用调试log信息保存在SD卡的方法

程序员文章站 2024-02-29 13:36:46
把自己应用的调试信息写入到sd卡中。 package com.sdmc.hotel.util; import java.io.bufferedreader;...

把自己应用的调试信息写入到sd卡中。

package com.sdmc.hotel.util;
import java.io.bufferedreader; 
import java.io.file; 
import java.io.filenotfoundexception; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.inputstreamreader; 
import android.content.context; 
import android.os.environment; 
import android.util.log;
/** 
* log日志统计保存 
* 会打印i,e,w,不会打印d
* 每次开启应用,就会把上次的log信息覆盖 
* @author way 
* 
*/ 
public class logcathelper { 
private static logcathelper instance = null; 
private static string path_logcat; 
private logdumper mlogdumper = null; 
private int mpid; 
/** 
* 
* 初始化目录 
* 
* */ 
public void init(context context) { 
if (environment.getexternalstoragestate().equals( 
environment.media_mounted)) {// 优先保存到sd卡中 
path_logcat = environment.getexternalstoragedirectory() 
.getabsolutepath() + file.separator + "minigps"; 
} else {// 如果sd卡不存在,就保存到本应用的目录下 
path_logcat = context.getfilesdir().getabsolutepath() 
+ file.separator + "minigps"; 
} 
file file = new file(path_logcat); 
if (!file.exists()) { 
file.mkdirs(); 
} 
} 
public static logcathelper getinstance(context context) { 
if (instance == null) { 
instance = new logcathelper(context); 
} 
return instance; 
} 
private logcathelper(context context) { 
init(context); 
mpid = android.os.process.mypid(); 
} 
public void start() { 
if (mlogdumper == null){ 
mlogdumper = new logdumper(string.valueof(mpid), path_logcat); 
}
log.i("path", path_logcat);///storage/sdcard0/minigps
mlogdumper.start(); 
} 
public void stop() { 
if (mlogdumper != null) { 
mlogdumper.stoplogs(); 
mlogdumper = null; 
} 
} 
private class logdumper extends thread { 
private process logcatproc; 
private bufferedreader mreader = null; 
private boolean mrunning = true; 
string cmds = null; 
private string mpid; 
private fileoutputstream out = null; 
public logdumper(string pid, string dir) { 
mpid = pid; 
try { 
out = new fileoutputstream(new file(dir, "gps-" 
+ mydate.getfilename() + ".log")); 
} catch (filenotfoundexception e) { 
// todo auto-generated catch block 
e.printstacktrace(); 
} 
/** 
* 
* 日志等级:*:v , *:d , *:w , *:e , *:f , *:s 
* 
* 显示当前mpid程序的 e和w等级的日志. 
* 
* */ 
// cmds = "logcat *:e *:w | grep \"(" + mpid + ")\""; 
// cmds = "logcat | grep \"(" + mpid + ")\"";//打印所有日志信息 
// cmds = "logcat -s way";//打印标签过滤信息 
cmds = "logcat *:e *:i | grep \"(" + mpid + ")\"";//会打印i,e,w,不会打印d 
} 
public void stoplogs() { 
mrunning = false; 
} 
@override 
public void run() { 
try { 
logcatproc = runtime.getruntime().exec(cmds); 
mreader = new bufferedreader(new inputstreamreader( 
logcatproc.getinputstream()), 1024); 
string line = null; 
while (mrunning && (line = mreader.readline()) != null) { 
if (!mrunning) { 
break; 
} 
if (line.length() == 0) { 
continue; 
} 
if (out != null && line.contains(mpid)) { 
out.write((mydate.getdateen() + " " + line + "\n") 
.getbytes()); 
} 
} 
} catch (ioexception e) { 
e.printstacktrace(); 
} finally { 
if (logcatproc != null) { 
logcatproc.destroy(); 
logcatproc = null; 
} 
if (mreader != null) { 
try { 
mreader.close(); 
mreader = null; 
} catch (ioexception e) { 
e.printstacktrace(); 
} 
} 
if (out != null) { 
try { 
out.close(); 
} catch (ioexception e) { 
e.printstacktrace(); 
} 
out = null; 
} 
} 
} 
} 
} 

系统权限

<uses-permission android:name="android.permission.write_external_storage" /> 
<uses-permission android:name="android.permission.read_logs" /> 

时间工具类:

package com.sdmc.hotel.util;
import java.text.simpledateformat; 
import java.util.date; 
public class mydate { 
public static string getfilename() { 
simpledateformat format = new simpledateformat("yyyy-mm-dd"); 
string date = format.format(new date(system.currenttimemillis())); 
return date;// 2012年10月03日 23:41:31 
} 
public static string getdateen() { 
simpledateformat format1 = new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
string date1 = format1.format(new date(system.currenttimemillis())); 
return date1;// 2012-10-03 23:41:31 
} 
}

方法的调用:

public class myapplication extends application { 
@override 
public void oncreate() { 
logcathelper.getinstance(this).start(); 
} 
}