7

Is there a way to redirect the logcat's output towards an InputStream or something like that? I'm thinking of something in the lines of how you can redirect the stderr in C. I want to do it so its redirected when my app starts and everything that happends get dumped to a file.

Moises Jimenez
  • 1,962
  • 3
  • 21
  • 43

5 Answers5

7

If you can connect the device for debugging, you can this from the command line

$ adb logcat > textfile.txt
spatulamania
  • 6,613
  • 2
  • 30
  • 26
3

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

Example:

System.setErr(new PrintStream(new File("<file_path>"))
Moises Jimenez
  • 1,962
  • 3
  • 21
  • 43
  • 2
    This will only capture output printed to `System.err` (and potentially `System.out` if you redirect it in the same manner). However, the output will not include the prefix Android ads to each line (timestamp, PID, TID etc.), and anything logged through the `Log` class will also not be redirected here. – user149408 Feb 28 '19 at 15:22
  • how do you redirect the Log to a file, run some crashes with Log.e(), and then read the file? – Phlip Feb 17 '22 at 21:59
1

The only redirecting possibilites of LogCat output are documentend here and here.

It is not possible to reuse the LogCat output in your app itself. You can however, export it to a file like you ask.

Community
  • 1
  • 1
MarchingHome
  • 1,184
  • 9
  • 15
0

to filter logcat just from your app try this:

        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) {

        }
SpyZip
  • 5,511
  • 4
  • 32
  • 53
  • 1
    There may be an easier way: run `logcat` without `su` and without granting the `READ_LOGS` permission to the app, which constrains the entries returned to the caller’s UID (i.e. your app, including earlier instances and subprocesses). This seems to vary between Android distributions, though. Otherwise, `logcat` also has the `-e` option to filter by regular expression. – user149408 Mar 04 '19 at 10:17
0

In Kotlin, this is what I used and it seems to create and log to the logcat.txt file (located in the app directory) whether the app is being debugged or run directly from the tablet. (Sidenote: For some reason the directory doesn't refresh or show up in file explorer until I stop the app and reconnect the device.) (thanks for the help from the previous contributors)

   located in utilities...
     val publicDirectory = globalContext.getExternalFilesDir(null)
                publicDirectoryName = publicDirectory?.toString() + "/"


        fun redirectLogcatToFile() {
        try {
            val filename =
                File(Utilities.publicDirectoryName + "logcat.txt")
            filename.createNewFile()
            val cmd = "logcat -v time -f " + filename.getAbsolutePath() +" -s " + logcatTag
            Runtime.getRuntime().exec(cmd)
        } catch (ex: Exception ) {
            Utilities.toastOnAnyThread("Utilities.redirectLogcatToFile: " + ex.message)
        }
    }
    
    fun Log( message : String ){
        Log.i(logcatTag, message )
    }