Launch Logcat programmatically in Android

Today I wanted to write logcat output to a file while my application is running. To do so I executed the following code:

public void executeLogcat(){
	File logFile = new File("/mnt/sdcard/arxeion.log"); // log file name
	int sizePerFile = 60; // size in kilobytes
	int rotationCount = 10; // file rotation count
	String filter = "D"; // Debug priority

	String[] args = new String[] { "logcat",
					"-v", "time",
					"-f",logFile.getAbsolutePath(),
					"-r", Integer.toString(sizePerFile),
					"-n", Integer.toString(rotationCount),
					"*:" + filter };

	try {
		Runtime.getRuntime().exec(args);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

To execute logcat process we are obliged to add

uses permission in AndroidManifest.xml file.

WARNING!!
1. This code should NOT be used in a release version of your application.
2. You should control when to kill logcat process, as it will run forever

Advertisements

About cmanios

programming
This entry was posted in Android, Java and tagged , , , . Bookmark the permalink.

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