How can I print the full logcat history in a TextView in Android?

喜你入骨 提交于 2019-12-08 12:00:32

logcat buffer for messages isn't very big, so old messages just get erased from logcat, if you need full log - save logcat messages to file on sdcard and then read info from it. This method will save all current messages to file:

public static void saveLogcatToFile(Context context) {
        String fileName = "yourlogname.log";
        File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/YourAppName", fileName);
        try {
            @SuppressWarnings("unused")
            Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

The code above is just a snapshot of the log at a point in time. If you want to write logs continuously use Java's Logging w/ a FileLogger and write to that.

Please add permission in manifest for logcat <uses-permission android:name="android.permission.READ_LOGS" />

I ended up saving the history to a member variable and only appending the new log lines each time I open the dialog.

public class LogsDialogFragment extends DialogFragment {

    private StringBuilder log = new StringBuilder();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Sets the Layout for the UI
        LayoutInflater i = getActivity().getLayoutInflater();
        View rootView = i.inflate(R.layout.fragment_logs_dialog, null);

        TextView logTextView = (TextView) rootView.findViewById(R.id.logTextView);
        logTextView.setMovementMethod(new ScrollingMovementMethod());

        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(WifiDirectHandler.LOG_TAG)){
                    // Removes log tag and PID from the log line
                    log.append(line.substring(line.indexOf(": ") + 2)).append("\n");
                }
            }

            this.log.append(log.toString().replace(this.log.toString(), ""));
            logTextView.setText(this.log.toString());
        } catch (IOException e) {
            Log.e("wifiDirectHandler", "Failure reading logcat");
        }

        // Creates and returns the AlertDialog for the logs
        AlertDialog.Builder dialogBuilder =  new  AlertDialog.Builder(getActivity())
            .setTitle(getString(R.string.title_logs))
            .setNegativeButton(getString(R.string.action_close),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                }
            ).setView(rootView);
        return dialogBuilder.create();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!