zoukankan      html  css  js  c++  java
  • RuntimeUtil

    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;
    }
    }
  • 相关阅读:
    尬聊攻略丨过年回家,你最怕被亲戚问什么?
    有人被盗刷900次? 支付宝发布年终“神反转”盘点
    470余万条疑似12306用户数据遭贩卖 嫌疑人被刑拘
    利用Python实现对Web服务器的目录探测
    栈空间溢出
    KB奇遇记(6):搞笑的ERP项目团队
    探寻不同版本号的SDK对iOS程序的影响
    阅读《Android 从入门到精通》(24)——切换图片
    leetcode
    Grunt的配置及使用(压缩合并js/css)
  • 原文地址:https://www.cnblogs.com/adamli/p/13139722.html
Copyright © 2011-2022 走看看