How to get Logcat details runtime (programmatically) in android


/**
 * @return <b>Logcat</b> data at the time of debugging as String<br/>
 * <b>Note:</b> This method requires permissions <b> "android.permission.READ_LOGS" </b>
 */
 public static String getLogCatDetails()
 {
 String logData = "";
 try
 {
 Process process = Runtime.getRuntime().exec("logcat -d");

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

StringBuilder log = new StringBuilder();

String line = "";

while ((line = bufferedReader.readLine()) != null)
 {
 log.append(line);
 }

 logData = log.toString();

}
 catch (Exception e)
 {
 if(e!=null)
 e.printStackTrace();
 }

 return logData;
 }

To clear logcat use the below method


/**
 * <b>Note:</b> This method requires permissions <b> "android.permission.READ_LOGS" </b>
 */
 public static void clearLog()
 {
 try
 {
 Runtime.getRuntime().exec("logcat -c");
 }
 catch (Exception e)
 {
 if(e!=null)
 e.printStackTrace();
 }
 }

Advertisements

Tags: ,

About krishjlk

http://www.facebook.com/krishna.kanth.121

One response to “How to get Logcat details runtime (programmatically) in android”

  1. Perry says :

    Stepping through the code for logging, shows each time that the log is empty? any thoughts would be greatly appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: