Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase console output at the android Log class

For default Log on android platform has limited amount of character for console output. Around equal a bit more than 3000. Therefore, if the message is longer than 3000 characters, it is not shown on screen.

I have not found a better solution than this:

public class Log {
    private static int mode = android.util.Log.INFO;

    public static void e(String tag, String msg, Throwable tr) {
        if ((mode & android.util.Log.ERROR) <= 0) return;
        android.util.Log.e(tag, msg, tr);
    }

    public static void e(String tag, String msg) {
        if ((mode & android.util.Log.ERROR) <= 0) return;
        android.util.Log.e(tag, msg);
    }

    public static void w(String tag, String msg) {
        if ((mode & android.util.Log.WARN) <= 0) return;
        android.util.Log.w(tag, msg);
    }

    public static void i(String tag, String msg) {
        if ((mode & android.util.Log.INFO) <= 0) return;
        android.util.Log.i(tag, msg);
    }

    public static void d(String tag, String msg) {
        if ((mode & android.util.Log.DEBUG) <= 0) return;

            int length = msg.length();
            int kind = 3000;
            if (length >= kind) {
                int count = length / kind;
                int u = length % kind;
                int i = 0;
                for (i = 0; i < count; ++i) {
                    int start = i * kind;
                    int end = start + kind;
                    android.util.Log.d(tag, msg.substring(start, end));
                }
                if (u != 0) {
                    int start = length - u;
                    int end = start + u;
                    android.util.Log.d(tag, msg.substring(start, end));
                }
            } else {
                android.util.Log.d(tag, msg);
            }
    }

}

Is there a better solution to this issue?

like image 350
Nikolay Moskvin Avatar asked Jan 12 '11 12:01

Nikolay Moskvin


People also ask

Where is the console in Android Studio?

in the upper menu of Android Studio. In the bottom status bar, click 5: Debug button, next to the 4: Run button. Now you should select the Logcat console. Check this article for more information.


1 Answers

A workaround for the 1024 character limit when doing large outputs (JSON etc) is to make a simple for-loop, logging them in chunks.

It may not be as elaborate as yours, but it's easy on the eyes and won't clutter the code too much.

        String json = data.toString();
        int length = json.length();

        for(int i=0; i<length; i+=1024)
        {
            if(i+1024<length)
                Log.d("JSON OUTPUT", json.substring(i, i+1024));
            else
                Log.d("JSON OUTPUT", json.substring(i, length));
        }
like image 126
Greger Avatar answered Nov 01 '22 11:11

Greger