How to save LogCat contents to file?

前端 未结 10 1285
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 06:57

I\'ve added debug strings (using Log.d()) and want to see them in context from the contents of logCat. The \"save\" icon for LogCat has a \"Save selected items\" hint, but t

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 07:32

    Additional tip for the programmatic approach:

    String filePath = Environment.getExternalStorageDirectory() + "/logcat.txt";
    Runtime.getRuntime().exec(new String[]{"logcat", "-f", filepath, "MyAppTAG:V", "*:S"});
    

    This opens a continuous output stream between logcat and the file provided. This can result in a deadlock if you then waitFor the Process returned by exec, or an exception if the provided file is prematurely disposed.

    I found that including the "-d" flag simply dumps logcat and closes the connection, which prevents the above behavior.

提交回复
热议问题