package com.android.demo.lileidemo.utils;
import com.android.demo.lileidemo.constant.AppConstants;
import com.ford.sync.basics.utils.LogUtil;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
/**
* date: 04/23/2020.
* author: lilei.
*/
public class RuntimeUtil {
private static final String TAG = AppConstants.APP_TAG + "RuntimeUtil ";
private static final String SAVE_LOG_DIR_PATH = AppConstants.TSP_DIR + "/log/";
/**
* getRetrieveLog from cmd adb logcat.
* for log test.
*
* @param timeMillis timeMillis.
* @param line if line is 0,set recent 5 second time.
*/
public static void getRetrieveLog(long timeMillis, String line) {
String retrieveTime = DateTimeUtil.getBeforeMillisDateTime(timeMillis, 5000);
LogUtil.d(TAG + "getRetrieveLog() begin retrieveTime:" + retrieveTime
+ " line:" + line);
try {
ArrayList<String> cmdLine = new ArrayList<String>();
cmdLine.add("logcat");
cmdLine.add("-t");
if ("0".equals(line)) {
cmdLine.add(retrieveTime);
} else {
cmdLine.add(line);
}
LogUtil.d(TAG + "getRetrieveLog() cmdLine:" + cmdLine.toString());
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) {
LogUtil.d(TAG + "getRetrieveLog() str:" + str);
}
if (str == null) {
LogUtil.d(TAG + "getRetrieveLog() end!!-- is null --");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* getRetrieveLogToFile from cmd adb logcat.
*
* @param timeMillis current time.
* @param recentSeconds recent seconds.
* @param fileName file name.
* @return Full log path.
*/
public static String getRetrieveLogToFile(long timeMillis, int recentSeconds, String fileName) {
String retrieveTime = DateTimeUtil.getBeforeMillisDateTime(
timeMillis, recentSeconds * 1000);
LogUtil.d(TAG + "getRetrieveLogToFile() begin UTF-8 timeMillis:" + timeMillis
+ " recentSeconds:" + recentSeconds + " begin retrieveTime:" + retrieveTime);
BufferedWriter bufferedWriter = null;
ArrayList<String> cmdLine = new ArrayList<String>();
cmdLine.add("logcat");
cmdLine.add("-t");
cmdLine.add(retrieveTime);
LogUtil.d(TAG + "getRetrieveLog() cmdLine:" + cmdLine.toString());
Process process = null; //capture log.
String fullLogPath = null;
try {
//create a new path.
File dirFile = new File(SAVE_LOG_DIR_PATH);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
process = Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
fullLogPath = SAVE_LOG_DIR_PATH + fileName;
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fullLogPath), "UTF-8"));
int len = 0;
byte[] buf = new byte[1024];
String str = null;
while ((str = bufferedReader.readLine()) != null) {
//Start reading the log, one line at a time.
//LogUtil.d(TAG + "getRetrieveLogToFile() str:" + str);
bufferedWriter.write(str + "
");
}
} catch (IOException e1) {
LogUtil.d(TAG + "getRetrieveLogToFile() error:" + e1);
e1.printStackTrace();
} catch (Exception e) {
LogUtil.d(TAG + "getRetrieveLogToFile() error:" + e);
e.printStackTrace();
} finally {
if (null != bufferedWriter) {
try {
bufferedWriter.close();
bufferedWriter = null;
} catch (IOException e) {
LogUtil.e(TAG + "getRetrieveLogToFile() error:" + e);
}
}
}
LogUtil.d(TAG + "getRetrieveLogToFile() end!!");
return fullLogPath;
}
}