Monday, July 2, 2012

Read & Store Log-cat Programmatically in Android

Android provide Logcat option with ADB to Debugg the Application.

So we can test app on Emulator and with the help of Logcat we can easily trace the Error details.

But if We are testing on Real Android Device and that is not connected with system then its hard know where we are getting exception, error etc.

so for that i wrote this Tutorial

We can read the Logcat details programmatically and also at run-time we can store in Text file and later we can see the error details.

so what we will do in this code:

  • Read the logcat
  • Showing the logcat on TextView
  • Create a Text file
  • store logcat into Text file
  • and finally save file into SDCard.

see below code snap..

//to read the logcat programmatically
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);
}
}
catch (IOException e) {
}

//to create a Text file name "logcat.txt" in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");


don't forget to add permission for read log-cat in manifest file



add permission for writing text file to SDCard file



So lets create a small App to do this things

-------------------------------------------
App Name: LogCollect
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: LogTest
-------------------------------------------

LogTest.java
package com.rdc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class LogTest extends Activity {

private StringBuilder log;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));

log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) {
}


//convert log to string
final String logString = new String(log.toString());

//create text file in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");

try {
//to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);

// Write the string to the file
osw.write(logString);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

main.xml






and the manifest file: make sure you need to add permission

















you can check the stored file open DDMS in eclipse and go to this path (in my cash)

File Explorer

data/mnt/sdcard/myLogcat/logcat.txt

now you can pull out this "logfile.txt" and store in local system. see below snap shot


also if you execute this app it will show you the log-cat on TextView with Scrolling like this

You can download the complete source code zip file here : LogCollect


cheers!!

I'd love to hear your thoughts!

Labels: , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home