Save app event (logcat) in file on sd

六月ゝ 毕业季﹏ 提交于 2019-12-07 04:13:24

You can get logcat via the following:

static final int BUFFER_SIZE = 1024;

public String getLogCat() {
    String[] logcatArgs = new String[] {"logcat", "-v", "time"};

    Process logcatProc = null;
    try {
        logcatProc = Runtime.getRuntime().exec(logcatArgs);
    }
    catch (IOException e) {
        return null;
    }

    BufferedReader reader = null;
    String response = null;
    try {
        String separator = System.getProperty("line.separator");
        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), BUFFER_SIZE);
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            sb.append(separator);
        }
        response = sb.toString();
    }
    catch (IOException e) {
    }
    finally {
        if (reader != null) {
            try {
                reader.close();
            }
            catch (IOException e) {}
        }
    }

    return response;
}

You can then save this String to the sdcard.

You can get logcat via the following:

static final int BUFFER_SIZE = 1024;

public String getLogCat() {
    String[] logcatArgs = new String[] {"logcat", "-v", "time"};

    Process logcatProc = null;
    try {
        logcatProc = Runtime.getRuntime().exec(logcatArgs);
    }
    catch (IOException e) {
        return null;
    }

    BufferedReader reader = null;
    String response = null;
    try {
        String separator = System.getProperty("line.separator");
        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), BUFFER_SIZE);
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            sb.append(separator);
        }
        response = sb.toString();
    }
    catch (IOException e) {
    }
    finally {
        if (reader != null) {
            try {
                reader.close();
            }
            catch (IOException e) {}
        }
    }

    return response;
}

You can then save this String to the sdcard.

This answer from "Dororo" didn't work for me since it always got stuck in the while due to to many lines, but i have no idea how to fix that.

kane

the logcat will block for reading new logs unless you specify the '-d' arg.

try

   String[] logcatArgs = new String[] {"logcat", "-d", "-v", "time"};
Raghav Sood

This type of functionality is already implemented by the ACRA Android library. The library detects crashes, and send the crash information to either a Google Docs spreadsheet, or your own destination.

Execute within a thread to avoid ANRs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!