How to display long messages in logcat

后端 未结 10 1007
轻奢々
轻奢々 2020-11-29 01:08

I am trying to display long message on logcat. If the length of message is more than 1000 characters, it gets broken.

What is the mechanism to show all characters o

相关标签:
10条回答
  • 2020-11-29 01:29

    Here is a Kotlin version for the @spatulamania answer (especially for lazy/smart peooples):

    val maxLogSize = 1000
    val stringLength = yourString.length
    for (i in 0..stringLength / maxLogSize) {
        val start = i * maxLogSize
        var end = (i + 1) * maxLogSize
        end = if (end > yourString.length) yourString.length else end
        Log.v("YOURTAG", yourString.substring(start, end))
    }
    
    0 讨论(0)
  • 2020-11-29 01:29

    For an easy solution, use Use soft wrap option in below attach point no 4 options might help you.

    0 讨论(0)
  • 2020-11-29 01:35

    In order not to minimize splitting lines across log messages, I take the large string and log each line separately.

    void logMultilineString(String data) {
        for (String line : data.split("\n")) {
            logLargeString(line);
        }
    }
    
    void logLargeString(String data) {
        final int CHUNK_SIZE = 4076;  // Typical max logcat payload.
        int offset = 0;
        while (offset + CHUNK_SIZE <= data.length()) {
            Log.d(TAG, data.substring(offset, offset += CHUNK_SIZE));
        }
        if (offset < data.length()) {
            Log.d(TAG, data.substring(offset));
        }
    }
    
    0 讨论(0)
  • 2020-11-29 01:45

    If logcat is capping the length at 1000 then you can split the string you want to log with String.subString() and log it in pieces. For example:

    int maxLogSize = 1000;
    for(int i = 0; i <= veryLongString.length() / maxLogSize; i++) {
        int start = i * maxLogSize;
        int end = (i+1) * maxLogSize;
        end = end > veryLongString.length() ? veryLongString.length() : end;
        Log.v(TAG, veryLongString.substring(start, end));
    }
    
    0 讨论(0)
提交回复
热议问题