Save app event (logcat) in file on sd

▼魔方 西西 提交于 2019-12-10 11:45:36

问题


I want to save my app logcat events in a text file on sd card. my alarming app work properly in my and my friends devices, but other have error on my app. for example they say alarms in app are in wrong time, but i dont see this error in my and my friends devices. Because of this issue and other issues, i want save all events logcat related my app, atomatically. so they send log file to me to solve issues. how can i do this? thanks sorry for my bad english


回答1:


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.




回答2:


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.




回答3:


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

try

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



回答4:


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.




回答5:


Execute within a thread to avoid ANRs



来源:https://stackoverflow.com/questions/14419191/save-app-event-logcat-in-file-on-sd

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