0

I'm trying to find a good way to display log messages in a TextView in an Activity. I tried to display logcat messages in the TextView, but the results vary every time I open and close the Fragment and not all messages are shown. I'm logging in multiple Activities and classes, so I'm trying to find a way to log without coupling it to every class. Is there an alternative to logcat or something, some type of system logging that I could access and display? Or any useful libraries or anything?

Thanks

Brendan
  • 1,403
  • 4
  • 18
  • 37

3 Answers3

2

When it comes to logging on Android I'd definitely recommend: https://github.com/JakeWharton/timber

For example you don't need to do that annoying TAG all the time as Timber does this for you.

Regarding your question I think what you can do it to plant your own tree in Timber. This will make sure all logging goes through there, and from there you can catch and log everything you want. See his example of where the Tree is planted in the Application: https://github.com/JakeWharton/timber/blob/master/timber-sample/src/main/java/com/example/timber/ExampleApp.java Now this is in the Application layer so you would need to fetch them into your activities in some way. A quick way, not very beautiful way, I think you can do is to store the logs in a Collection and have this statically accessible to fetch from the Application.

On a side note, I'm curious of why you want this, why isn't Android Studio's logging tool good enough? :)

Brendan
  • 1,403
  • 4
  • 18
  • 37
  • Thank you I'll check that out. Android Studio's logging tool works great, but I'd also like to be able to view the logs on the device when I'm running the app without the computer. It's a network testing app, so I log messages when certain things happen in the background, and I'd like to be able to view a list of those messages. – Brendan Apr 24 '16 at 20:56
  • 1
    Hey Brendan, sure, makes sense. I think Timber will suit your needs. If not another option is to write your own "customised" logger where you create a Singleton that you can fetch from anywhere and log what you want, store the values in a Collection and fetch them from there. – Henrik Gyllensvärd Apr 25 '16 at 06:34
1

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();
    }
}
Brendan
  • 1,403
  • 4
  • 18
  • 37
0

A toast provides simple feedback about an operation in a small popup:

Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();

enter image description here

phnmnn
  • 12,813
  • 11
  • 47
  • 64