Omid Omidi
Omid Omidi

Reputation: 1670

Save app event (logcat) in file on sd

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

Upvotes: 1

Views: 2620

Answers (5)

user3913384
user3913384

Reputation: 159

Execute within a thread to avoid ANRs

Upvotes: 1

Raghav Sood
Raghav Sood

Reputation: 82563

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.

Upvotes: 1

kane
kane

Reputation: 11

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

try

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

Upvotes: 1

FireDeer
FireDeer

Reputation: 51

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.

Upvotes: 1

Dororo
Dororo

Reputation: 3440

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.

Upvotes: 3

Related Questions