Android Log.X not printing stacktrace

三世轮回 提交于 2019-12-14 03:43:22

问题


e.printStackTrace() works fine (i.e. prints my stacktrace to stderr) but Log.X fails to print a stacktrace at all.

For example:

} catch (IOException e) {
    Log.e("Network", "Exception", e);
    e.printStackTrace();
}

Output:

08-31 03:46:21.992: W/Network(13238): Exception
08-31 03:46:22.092: W/System.err(13238): java.net.UnknownHostException: Unable to resolve host "...": No address associated with hostname
08-31 03:46:22.204: W/System.err(13238):    at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
08-31 03:46:22.222: W/System.err(13238):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-31 03:46:22.222: W/System.err(13238):    at java.net.InetAddress.getAllByName(InetAddress.java:214)

回答1:


Turns out Android's Log.getStackTraceString which is used by Log.X swallows UnknownHostException. :(

From: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/util/Log.java#Log.getStackTraceString%28java.lang.Throwable%29

public static String getStackTraceString(Throwable tr) {
    if (tr == null) {
        return "";
    }

    // This is to reduce the amount of log spew that apps do in the non-error
    // condition of the network being unavailable.
    Throwable t = tr;
    while (t != null) {
        if (t instanceof UnknownHostException) {
            return "";
        }
        t = t.getCause();
    }

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    tr.printStackTrace(pw);
    return sw.toString();
}

It's all very well reducing log spew but to not even tell me what my exception was is bad joojoo!




回答2:


I had also this kind of issue and created an Xposed module to overcome this problem. See Xposed for Android 8+ or the Original Xposed for installation instruction. Later I stumbled over this thread.

Here is the project: FullStackTrace

There is also an apk for the download unser the releases.

You device must be rooted to be able to use Xposed. I used Magisk here.



来源:https://stackoverflow.com/questions/18544539/android-log-x-not-printing-stacktrace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!