How to redirect logcat output?

Is there a way to redirect logcat output to an InputStream or something like that? I am thinking of something in the lines of how you can redirect stderr to C. I want to do this so that it is redirected when my application starts, and everything that happens is dumped to a file.

+4
source share
4 answers

The easiest way is to use System.setErr . I let you easily redirect the error output to a file.

Example:

 System.setErr(new PrintStream(new File("<file_path>")) 
+2
source

If you can connect the device to debugging, you can do it from the command line

 $ adb logcat > textfile.txt 
+2
source

The only LogCat output redirection options are documentend here and here .

It is not possible to reuse LogCat output in the application itself. However, you can export it to a file as you ask.

+1
source

to filter logcat only from your application, try the following:

  int pid = android.os.Process.myPid(); File outputFile = new File(Environment.getExternalStorageDirectory() + "/logcat.txt"); Log.d("zzz","outputFile: " + outputFile); try { String command = "logcat | grep " + pid + " > " + outputFile.getAbsolutePath(); Log.d("zzz","command: " + command); Process p = Runtime.getRuntime().exec("su"); OutputStream os = p.getOutputStream(); os.write((command + "\n").getBytes("ASCII")); } catch (IOException e) { } 
0
source

Source: https://habr.com/ru/post/1441218/


All Articles