MGK
MGK

Reputation: 7098

How to redirect my log output from logcat to the SD-Card on an android device?

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.

Upvotes: 19

Views: 28177

Answers (6)

bear
bear

Reputation: 99

   /**
     * Launches a logcat process.
     * To stop the logcat preserve the use:
     * process.destroy();
     * in the onBackPressed()
     *
     * @param filename destination of logcat output
     * @return Process logcaat is running on
     */

    public Process launchLogcat(String filename) {
        Process process = null;
        String cmd = "logcat -f " + filename + "\n";
        try {
            process = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            process = null;
        }
        return process;
    }

Upvotes: 4

Krishna
Krishna

Reputation: 1634

public static void saveLogInSDCard(Context context){
    String filename = Environment.getExternalStorageDirectory() + File.separator + "project_app.log";
    String command = "logcat -d *:V";

    try{
        Process process = Runtime.getRuntime().exec(command);

        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        try{
            File file = new File(filename);
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
            while((line = in.readLine()) != null){
                writer.write(line + "\n");
            }
            writer.flush();
            writer.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

Upvotes: 0

Green goblin
Green goblin

Reputation: 9996

If you are getting an empty log file, try change the extension of file from .log to .txt.

Upvotes: 0

adamk
adamk

Reputation: 46804

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", "-f", "/sdcard/myfilename", "-v", "time", "ActivityManager:W", "myapp:D" };

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

Upvotes: 22

sai
sai

Reputation: 1

Try using a space after every element in the string. Otherwise, it will consider as a single line command without spaces and do nothing.

Upvotes: 0

broderix
broderix

Reputation: 389

This is my worked version:

try {
    File filename = new File(Environment.getExternalStorageDirectory()+"/logfile.log"); 
    filename.createNewFile(); 
    String cmd = "logcat -d -f "+filename.getAbsolutePath();
    Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

find your logfile /sdcard/logfile.log

Upvotes: 26

Related Questions