由于我们在Android中使用Logcat作为控制台,有时输出的文本/消息可能很长,我无法看到完整的输出。Logcat仅显示其开头部分。是否有一种方法可以扩展它,以便我可以看到完整的消息?
由于我们在Android中使用Logcat作为控制台,有时输出的文本/消息可能很长,我无法看到完整的输出。Logcat仅显示其开头部分。是否有一种方法可以扩展它,以便我可以看到完整的消息?
这是我解决问题的方法,希望能帮到你。
在代码中使用的重要方法是splitAndLog。
public class Utils {
/**
* Divides a string into chunks of a given character size.
*
* @param text String text to be sliced
* @param sliceSize int Number of characters
* @return ArrayList<String> Chunks of strings
*/
public static ArrayList<String> splitString(String text, int sliceSize) {
ArrayList<String> textList = new ArrayList<String>();
String aux;
int left = -1, right = 0;
int charsLeft = text.length();
while (charsLeft != 0) {
left = right;
if (charsLeft >= sliceSize) {
right += sliceSize;
charsLeft -= sliceSize;
}
else {
right = text.length();
aux = text.substring(left, right);
charsLeft = 0;
}
aux = text.substring(left, right);
textList.add(aux);
}
return textList;
}
/**
* Divides a string into chunks.
*
* @param text String text to be sliced
* @return ArrayList<String>
*/
public static ArrayList<String> splitString(String text) {
return splitString(text, 80);
}
/**
* Divides the string into chunks for displaying them
* into the Eclipse's LogCat.
*
* @param text The text to be split and shown in LogCat
* @param tag The tag in which it will be shown.
*/
public static void splitAndLog(String tag, String text) {
ArrayList<String> messageList = Utils.splitString(text);
for (String message : messageList) {
Log.d(tag, message);
}
}
}
我从不使用GUI查看logcat输出,所以我不确定DDMS / Eclipse UI中是否有滚动条。
无论如何,您可以使用命令行中的logcat - 有大量选项可用。
要持续监视活动设备的日志: adb logcat
要转储整个日志: adb logcat -d
将整个日志转储到文件中: adb logcat -d > log.txt
过滤并显示特定的日志标记: adb logcat -s MyLogTag
......等等!
logcat
中查看长消息,可以编写自己的包装器来分割多行消息,这可能值得使用android.util.Log
方法。当然,你可以通过将鼠标放在行尾并拖动来更改列宽。但是对于非常长的消息来说,这样做会很麻烦。如果我有一条非常长的消息,我通常会将其复制并粘贴到文本文件中。在Windows中,使用Ctrl-C即可复制。