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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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

xml

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
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy