ostergaard
ostergaard

Reputation: 3507

Android Log.X not printing stacktrace

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)

Upvotes: 18

Views: 2180

Answers (2)

k_o_
k_o_

Reputation: 6288

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.

Upvotes: 0

ostergaard
ostergaard

Reputation: 3507

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!

Upvotes: 45

Related Questions