package com.wasu.cs.module; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import com.wasu.authsdk.AuthSDK; import com.wasu.authsdk.IAuthInterface; import com.wasu.module.log.WLog; import com.wasu.statistics.WasuStatistics; /** * 支持WR日志上传 * @author danxingxi created on 2015/12/16 * */ public class LogReaderModule { private final static String TAG = LogReaderModule.class.getSimpleName(); public static class WRLogHolder // 静态类 { private static logUpLoadListener mLogUpLoadListener; /** * 日志上传方法 * @param callBackName JS提供的回调方法名,调用webView的方法,通知日志上传成功 * @param mLogUpLoadListener 日志上传结果回调方法 */ public static void upLoadLog(logUpLoadListener mLogUpLoadListener) { try { StringBuffer logBuffer = new StringBuffer(); //日志缓存buffer ArrayList cmdLine = new ArrayList(); // 设置命令 cmdLine.add("logcat"); // logcat cmdLine.add("-d"); // -d 读取日志 cmdLine.add("-v"); //设置日志格式 cmdLine.add("time"); //显示时间 cmdLine.add("-tag"); //显示tag ArrayList clearLog = new ArrayList(); // 设置命令 clearLog.add("logcat"); // logcat clearLog.add("-c"); // -c 清除日志 Process process = Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()])); // 捕获日志 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); // 将捕获内容转换为BufferedReader // Runtime.runFinalizersOnExit(true); String str = null; int temp = 0; while (temp < 30 && (str = bufferedReader.readLine()) != null) // 开始读取日志,每次读取一行,最多读取30行 { if(str != null){ //WLog.e("TAG", str); // 输出,在logcat中查看效果,也可以是其他操作,比如发送给服务器.. logBuffer.append(str+"\r\n"); } temp++; } Runtime.getRuntime().exec(clearLog.toArray(new String[clearLog.size()])); // 清理日志,读取日志后清除日志缓存 if(logBuffer.length() > 0){ WLog.d(TAG, "上传日志内容:"+logBuffer.toString()); WasuStatistics.getInstance().uploadLog(WasuStatistics.ERROR, AuthSDK.getInstance().getValue(IAuthInterface.KEY_TVID) , logBuffer.toString()); if (mLogUpLoadListener != null) { mLogUpLoadListener.onResult(true); } }else{ WLog.e(TAG, "WRlog string is null"); if (mLogUpLoadListener != null) { mLogUpLoadListener.onResult(false); } } /**回收资源,关闭流**/ logBuffer.setLength(0); logBuffer = null; bufferedReader.close(); bufferedReader = null; cmdLine = null; process.destroy(); process = null; str = null; clearLog = null; mLogUpLoadListener = null; } catch (Exception e) { e.printStackTrace(); WLog.e(TAG, "upload WRlog error"); if (mLogUpLoadListener != null) { mLogUpLoadListener.onResult(false); } } WLog.i(TAG, "WRlog upload finish"); } /** * 日志上传结果回调 * @author danxingxi * */ public interface logUpLoadListener{ void onResult(boolean isSuccessful); } } }