Android display Logcat in TextView
Asked Answered
D

3

14

The app I'm building have root-access to Android. I wish to show Logcat log information in one of the TextView so it can show up on the screen while i'm debugging.

Can someone give me some ideas which library/function i can call to access those log?

Thanks

Discoverer answered 17/8, 2014 at 16:39 Comment(3)
is there a reason why you want to implement the logcat-viewing on your own instead of using the open source app alogcat ?Fudge
The major reason is that alogcat cannot be run in parallel with the application. I'm experiencing a problem where the application completely make the Android Shell non-responsive. So I want to see what's going on at real-time overlay on top of my application.Discoverer
Possible duplicate of Can logcat results for Log.i be viewed in our activity?Malley
D
30

Here's a blogpost that does exactly what you need it to do. It has a complete code example on how to display the contents of the Logcat log. Here's the code:

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;

  class ReadLogDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         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) {
                   log.append(line);
              }   
              TextView tv = (TextView)findViewById(R.id.textView1);
              tv.setText(log.toString());
         } catch (IOException e) {
              // Handle Exception
         }
    }

  } 

Just to clarify, this is not my answer, here is the original

Dunlop answered 17/8, 2014 at 17:38 Comment(9)
Thanks for the answer. However, one of my major concern is that how do I know there's a new line of log appeared in the system ready for display? Do I just simply put them in infinite loop and check them every now and then?Discoverer
That's one way of doing it yes, you could also add a OnClickListenear to load the new Logcat stream. You mentioned that your app make the Android Shell non-responsive. Are you using TRY/CATCH statements? they should handle that. You could also connect your phone to your workstation through a USB cable and use Eclipse and use Logcat view from there.Dunlop
Thanks. I guess I'll have it trigger on click and update them every few seconds.Discoverer
@DawidCzerwinski How can I limit the results to a specific tag?Halfpint
@SteveC. #6174485 try this.Dunlop
@DawidCzerwinski how would I define that in android since I can't use the quotation marks?Halfpint
@SteveC. have you tried single quotation marks? or other way of expressing quotes in a string? e.g. Python lets you do that by placing an "\" on the front. e.g.: print "Hello \"Steve\" " I'cant remember how to do it in Android.Dunlop
@DawidCzerwinski were you able to figure out how to limit the results to a specific tag? Using \ or single quotes isn't working for me?Paraphrastic
Nevermind. Just realized you don't need any type of quotes at all. Something like logcat -d -s TAG will workParaphrastic
Z
1

Well, there is a solution to log anything you want to screen using this lib. It didn't worked for me, so I develop my own solution you can find an example of it here. It's really simple, just add a class OnScreenLog to your project

package br.com.ideiageni.onscreenlogSample;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * Created by ariel on 07/07/2016.
 */
    public class OnScreenLog {
    private static int timeoutTime = 1000;
    private static TextView tvLog;
    private static int logCount = 0;
    private static int logCountMax = 30;
    private static String[] logs = new String[logCountMax];
    private static int cntClicks = 0;
    private static boolean visibility = false;
    private static Activity activity;
    private int maxClicks = 5;

    public OnScreenLog(){}

    public OnScreenLog(Activity activity, int ViewID){
        OnScreenLog.activity = activity;
        tvLog = new TextView(activity.getApplicationContext());
        maintainLog("Log is working");
        tvLog.setLayoutParams(new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT));
        tvLog.setTextColor(Color.BLACK);
        tvLog.setBackgroundColor(Color.LTGRAY);
        tvLog.setAlpha((float) 0.4);

        View v = null;
        LinearLayout linearLayout;
        RelativeLayout relativeLayout;
        try {
            linearLayout = (LinearLayout) activity.findViewById(ViewID);
        } catch (ClassCastException e) {linearLayout = null;};

        try {
            relativeLayout = (RelativeLayout) activity.findViewById(ViewID);
        } catch (ClassCastException e) {relativeLayout = null;};
        if(linearLayout != null) {
            linearLayout.addView(tvLog);
            v = linearLayout;
        } else if(relativeLayout != null) {
            relativeLayout.addView(tvLog);
            v = relativeLayout;
        }

        if(v != null) {
            v.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            cntClicks++;
                            timerHandler.removeCallbacks(rTimeout);
                            timerHandler.postDelayed(rTimeout, timeoutTime);

                            if (cntClicks > maxClicks-1) {
                                setLogVisible(!visibility);
                                timerHandler.removeCallbacks(rTimeout);
                                cntClicks = 0;
                            }
                            break;

                    }
                    return false;
                }
            });
        }

    }

    public void log (String text){
        String logText = text;
        maintainLog(logText);
    }

    public void log (int text){
        String logText = String.valueOf(text);
        maintainLog(logText);
    }

    public void log (int[] text){
        StringBuilder builder = new StringBuilder();
        for (int i : text) {
            builder.append(i);
            builder.append("-");
        }
        String logText = builder.toString();
        maintainLog(logText);
    }

    public void log (byte[] text){
        StringBuilder builder = new StringBuilder();
        for (int i : text) {
            builder.append(i);
            builder.append("-");
        }
        String logText = builder.toString();
        maintainLog(logText);
    }

    private void maintainLog(String newText){
        String logText = "";
        if(logCount<logCountMax) logCount++;
        for(int i=logCount-1; i>0; i--){
            logs[i] = logs[i-1];
        }
        logs[0] = newText;
        for(int i=0; i<logCount; i++){
            if(i<logCount-1) logText+=logs[i]+System.getProperty("line.separator");
            else logText+=logs[i];
        }
        tvLog.setText(logText);
    }

    public void clearLog(){
        tvLog.setText("");
    }

    public void setLogVisible(boolean visibility){
        if(visibility) tvLog.setVisibility(View.VISIBLE);
        else tvLog.setVisibility(View.INVISIBLE);
        OnScreenLog.visibility = visibility;
    }

    public static int getLogCountMax() {
        return logCountMax;
    }

    public static void setLogCountMax(int logCountMax) {
        OnScreenLog.logCountMax = logCountMax;
        logs = new String[logCountMax];
    }

    public int getMaxClicks() {
        return maxClicks;
    }

    public void setMaxClicks(int maxClicks) {
        this.maxClicks = maxClicks;
    }

    Handler timerHandler = new Handler();
    Runnable rTimeout = new Runnable() {

        @Override
        public void run() {
            cntClicks = 0;
        }
    };
}

then, for instance:

public class Activity1 extends AppCompatActivity {

private OnScreenLog log;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_1);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    log = new OnScreenLog(this, R.id.content_1);
    log.log("Started log on Activity 1");

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), Activity2.class);
            startActivity(intent);
            log.log("Starting Activity 2");
            Snackbar.make(view, "Starting Activity 2", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

Where R.id.content_1 is the name of the main LinearLayout or RelativeLayout of your activity.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.ideiageni.onscreenlogSample.Activity1"
tools:showIn="@layout/activity_1">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Activity 1!" />

</RelativeLayout>

Work not finished yet but can be used for anyone in need. Missing some directions on how to use. Suggestions are welcome.

Zeralda answered 20/10, 2016 at 3:32 Comment(0)
L
0

If you want to clean up the output of the above answer a little bit to a certain topic then here is one solution:

**The colon may not work depending on how your log is formatted**

while ((line = bufferedReader.readLine()) != null) {
    CharSequence cs = "Mqtt:";  //topic string (the colon helps cut out false positives)
    if (line.contains(cs)) {
        log.append(line.substring(line.indexOf("Mqtt:"),line.length())); // Cuts out the junk at the beginning of the log
        log.append("\n"); // makes log print line by line like normal
    }
}

Thanks @Dawid for the great link to a good answer!

Lining answered 25/5, 2016 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.