bydziek
bydziek

Reputation: 43

How to programmatically stop saving logcat to file on Android device?

Background: I'm working on application that will be used by limited users group and I will never put it on Store. App sometimes is having some troubles on clients devices and it seems like it would be very useful to me to have full access to logcat since I don't have access to devices. And I know that I have to give users "debug" app version - it's not a problem.

So I want to implement feature that allows user (eg. on my request) to redirect logcat to file, which can be sent to me. I already implemented redirecting to file, but I want to allow user for disabling saving to file since it gets big quite fast.

For redirecting to file I'm using

String cmd = "logcat -f " + file.getAbsolutePath();
Runtime.getRuntime().exec(cmd);

But once it's enabled I can't disable it. I mean stop saving to file. I tried with -c option with no luck. What can I do?

Upvotes: 4

Views: 2183

Answers (5)

Dan Harms
Dan Harms

Reputation: 4840

You can switch it back to printing to stdout (default).

String cmd = "logcat -f stdout";
Runtime.getRuntime().exec(cmd);

Upvotes: 1

Huby
Huby

Reputation: 871

Runtime.getRuntime().exec() returns a process that you can kill, which will stop the logging.

// Start writing file
String cmd = "logcat -f " + file.getAbsolutePath();
Process logcatProcess = Runtime.getRuntime().exec(cmd);

// Stop writing file
logcatProcess.destroy();

Upvotes: 1

Al Cabone
Al Cabone

Reputation: 413

Was asking myself the same thing...

This did the trick for me if anybody is still interested

Runtime.getRuntime().exec("logcat -f " + outputFile.getAbsolutePath() + " -d");

key is the "-d" here in the end.

You could also replace "-d" with "-t 1000" to print only the last 1000 lines to file and then stop.

Be aware that a new call to the same logfile will append logs. So you could delete that file if it already exists.

Upvotes: 1

Santiago Villafuerte
Santiago Villafuerte

Reputation: 783

You may want to call "ps | grep logcat" and kill it with its PID: "kill pid".

Upvotes: 0

bydziek
bydziek

Reputation: 43

I did not found solution to my problem, so it remains open. I just want to leave here my workaround as it might be useful.

So I decided to leave logcat printing to stdout and just dump it to file and then clear on button click. This solution have some advantages since logcat is separated in files with timestamps in names so I can look for problem depending on time when something happend.

Upvotes: 0

Related Questions