1

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

Omid Omidi
  • 1,670
  • 2
  • 16
  • 23
  • possible duplicate of [How to redirect my log output from logcat to the SD-Card on an android device?](http://stackoverflow.com/questions/3359692/how-to-redirect-my-log-output-from-logcat-to-the-sd-card-on-an-android-device) – Narkha Mar 25 '14 at 11:50

5 Answers5

3

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.

Dororo
  • 3,420
  • 2
  • 30
  • 46
  • how i can filter logcat to my app? – Omid Omidi Jan 19 '13 at 23:28
  • 2
    Add the tags as an additional arg to logcatArgs e.g. {"logcat", "-v", "time", "YourTag:V AnotherTag:V MoreTag:V *:S"}; You indicate the level you want after the : (in this case Verbose hence Tag:V) and the *:S makes everything else silent. – Dororo Jan 20 '13 at 10:58
  • how can i do this dynamically? – Omid Omidi Jan 20 '13 at 14:08
  • 1
    This -is- dynamic. You just need to call the function at a sensible time, e.g. when they leave the app, when they click 'report bug'. If you mean how can you do the tags dynamically, you shouldn't need to. The tags you are logging on will be constant e.g. Log.e("AppTag", "This is an error"); you would add "AppTag". You shouldn't really be using tags which change. – Dororo Jan 20 '13 at 15:33
  • I believe you need android.permission.READ_LOGS in your AndroidManifest.xml – Dororo Jan 21 '13 at 22:24
1

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.

pevik
  • 4,523
  • 3
  • 33
  • 44
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • i can't go to ACRA link, because google interdict it for my ip. and my app isnt crash, my app work in alarm wrong. – Omid Omidi Jan 19 '13 at 23:26
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.

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.

FireDeer
  • 51
  • 3
1

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

try

   String[] logcatArgs = new String[] {"logcat", "-d", "-v", "time"};
Renjith
  • 5,783
  • 9
  • 31
  • 42
kane
  • 11
  • 1
1

Execute within a thread to avoid ANRs

user3913384
  • 119
  • 1
  • 2