作为开发,我们深深体谅测试的同学。这里将代码嵌入到项目里面,测试只需要两秒钟就可以将抓取系统log并发送给开发了,大大节约了人力成本。
首先创建一个类,取名CustomlLogcat,作用是抓取系统log并写入文件。
public class CustomlLogcat { public static String LOG_PATH = Environment.getExternalStorageDirectory() + File.separator + "custom_log.txt"; public interface OnLogGetListener { public void onLogGet(); } public static class MLog { public static void Log(final Context c ,final OnLogGetListener l) { final Thread th=new Thread(new Runnable() { @Override public void run() { getLog(); } }); Handler hanlder = new Handler(); hanlder.post(new Runnable() { @Override public void run() { try { th.start(); } finally { if (l != null) { l.onLogGet(); } } } }); } private static void getLog() { try { deleteLog(); ArrayList<String> cmdLine=new ArrayList<String>(); cmdLine.add("logcat"); cmdLine.add("-d"); cmdLine.add("*:W"); ArrayList<String> clearLog=new ArrayList<String>(); clearLog.add("logcat"); clearLog.add("-c"); Process process=Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()])); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream())); String str=null; while((str=bufferedReader.readLine())!=null) { Runtime.getRuntime().exec(clearLog.toArray(new String[clearLog.size()])); writeLog(str); } if(str==null) { } } catch(Exception e) { e.printStackTrace(); } } private static void deleteLog() { File f = new File(LOG_PATH); if (f.exists()) { f.delete(); } } private static void writeLog(String str) { File f = new File(LOG_PATH); try { if (!f.exists()) { f.createNewFile(); } FileWriter fw = new FileWriter(f ,true); fw.write(str); fw.write("\r\n"); fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
以上代码需要注意的是,本人只抓取了warnning(W)级别以上的代码,所以传递给系统的命令代码是:
ArrayList<String> cmdLine=new ArrayList<String>(); cmdLine.add("logcat"); cmdLine.add("-d"); cmdLine.add("*:W");
接着,就可以把系统日志作为附件发送邮件给开发了。
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "抓取日志"); menu.add(0, 1, 0, "发送邮件给小天天"); menu.add(0, 2, 0, "发送邮件给小马马"); menu.add(0, 3, 0, "发送邮件给小斌斌"); menu.add(0, 4, 0, "发送邮件给小尊尊"); menu.add(0, 5, 0, "发送邮件给小饼饼"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 0: CustomlLogcat.MLog.Log(MainActivity.this, new OnLogGetListener() { @Override public void onLogGet() { Toast.makeText(MainActivity.this, "日志抓取完毕,可以发送邮件。", Toast.LENGTH_SHORT).show(); } }); break; case 1: sendLogMail(1); break; case 2: sendLogMail(2); break; case 3: sendLogMail(3); break; case 4: sendLogMail(4); break; case 5: sendLogMail(5); break; } return super.onOptionsItemSelected(item); } public void sendLogMail(int optionIndex) { File file = new File(CustomlLogcat.LOG_PATH); //附件文件地址 Intent intent = new Intent(Intent.ACTION_SEND); if (optionIndex == 1) { intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"XXXX@XXXX.com"}); } else if (optionIndex == 2) { intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"XXXX@XXXX.com"}); } else if (optionIndex == 3) { intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"XXXXX@XXX.com"}); } else if (optionIndex == 4) { intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"XXXX@XXXX.com"}); } else if (optionIndex == 5) { intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"XXXX@XXXX.com"}); } else {} intent.putExtra("subject", "Android斗地主log--------------"); // intent.putExtra("body", "详情查看附件-------------"); //正文 intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); //添加附件,附件为file对象 if (file.getName().endsWith(".gz")) { intent.setType("application/x-gzip"); //如果是gz使用gzip的mime } else if (file.getName().endsWith(".txt")) { intent.setType("text/plain"); //纯文本则用text/plain的mime } else { intent.setType("application/octet-stream"); //其他的均使用流当做二进制数据来发送 } startActivity(intent); //调用系统的mail客户端进行发送 } }