views:

120

answers:

3

I'm trying to redirect the log of my app to the sdcard file. But i failed to do so. I'm trying something like this.

String cmd= "logcat -v time   ActivityManager:W  myapp:D  *:* >\""+file.getAbsolutePath()+"\"";
Runtime.getRuntime().exec(cmd);

I tried the -f option also but it is not working either.

A: 

use logcat -f <filename> in order to dump it to a file in the filesystem.
Make sure the filename you're using is in the sdcard, i.e. starts with /sdcard/....

Also, in order to pass arguments to the logcat program, you should pass the exec method an array of strings (and not one string):

String[] cmd = new String[] { "logcat", "-v" "time", "ActivityManager:W", "myapp:D" };

Finally, if all else fails, use the full path for logcat: /system/bin/logcat instead of just logcat.

adamk
thanx for reply..I tried the same but i am getting empty file
Santhosh
please note that it may take some time for the logfile to show contents, as it might be buffering writes to the file (and writing a few KB at a time)
adamk
And another important thing - make sure your app has the WRITE_EXTERNAL_STORAGE and READ_LOGS permissions - or it won't allow it to read logs and write them to the sdcard .
adamk
A: 

Hi adamk

Could you please write the complete command. As it is confusing to me... Thanks in advance.

sai
A: 

Hi Again,

I got it. Please use space after every element in the String. Otherwise it will consider as a single line command without spaces and do nothing.

sai
hi, I tried,its giving me empty file...This is wat i tried,File filename = new File("/sdcard/logfile.log");filename.createNewFile();String[] cmd = new String[] { "logcat ", "-v ","time ", "ActivityManager:W ", "myApp:D ","*:* " ,"\""+filename.getAbsolutePath()+"\""};Runtime.getRuntime().exec(cmd);
Santhosh