How does Log.wtf() differ from Log.e()?

前端 未结 7 614
臣服心动
臣服心动 2020-12-13 12:18

I have looked at the documentation for android.util.Log and I\'m not sure exactly what the difference between Log.e() and Log.wtf() is. Is one pref

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 12:22

    COMMON MISTAKE

    Official docs say:

    Log.e() logs with priority ERROR. However, Log.wtf() logs with priority ASSERT.

    ASSERT has priority constant = 7
    ERROR has priority constant = 6

    So Log.wtf() has higher priority with respect to Log.e()

    However source code conflicts with above information.

    static int wtf(int logId, String tag, String msg, Throwable tr,boolean localStack, boolean system) {
            
        TerribleFailure what = new TerribleFailure(msg, tr);
        // Only mark this as ERROR, do not use ASSERT since that should be
        // reserved for cases where the system is guaranteed to abort.
        // The onTerribleFailure call does not always cause a crash.
        int bytes = printlns(logId, ERROR, tag, msg, localStack ? what : tr);
        ...
    }
    

    It looks like there is a mistake in Official docs. Because both Log.wtf() and Log.e() logs with priority ERROR.

    REAL DIFFERENCE

    Source Code for Log.e():

    public static int e(@Nullable String tag, @Nullable String msg,@Nullable Throwable tr) {
        return printlns(LOG_ID_MAIN, ERROR, tag, msg, tr);
    }
    

    The difference is that Log.wtf() might call onTerribleFailure() call back.

    onTerribleFailure() may or may not cause the process to terminate (depends on system settings).

    TL;DR

    Log.wtf() might call onTerribleFailure() and can cause termination of your application.

提交回复
热议问题