两者的严重性不同;
Log.e()
仅会使用优先级ERROR将错误记录到日志中。
Log.wtf()
将使用优先级ASSERT记录错误,并且(根据系统配置)可能会发送错误报告并立即终止程序。
TL;DR
Log.wtf()
的优先级高于Log.e()
,但它可能会调用onTerribleFailure()
并导致应用程序终止。
官方文档说:
Log.e()
使用优先级ERROR进行日志记录。然而,Log.wtf()
使用优先级ASSERT进行日志记录。
ASSERT的优先级常量=7
ERROR的优先级常量=6
因此,Log.wtf()
相对于Log.e()
具有更高的优先级。
但是源代码与上述信息矛盾。
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);
...
}
官方文档中似乎有一个错误。因为Log.wtf()
和Log.e()
都使用ERROR优先级进行日志记录。
Log.e()
的源代码:
public static int e(@Nullable String tag, @Nullable String msg,@Nullable Throwable tr) {
return printlns(LOG_ID_MAIN, ERROR, tag, msg, tr);
}
不同之处在于Log.wtf()可能会调用onTerribleFailure()回调函数。
onTerribleFailure()可能会造成进程终止,也可能不会(取决于系统设置)。
Log.e()
是将错误信息记录在日志中,并将其优先级设置为 ERROR。
Log.wtf()
(表示“真是一个可怕的失败”)比错误日志更严重。它是一种从未发生过的错误,可能会强制设备在终止程序之前保留写入日志。
实际上,这可能是Android SDK中的文档错误,真是惊人...文档说:
错误将始终以调用堆栈为ASSERT级别记录。
但源代码却是这样的:
static int wtf(int logId, String tag, String msg, Throwable tr, boolean localStack, boolean system) {
...
int bytes = printlns(logId, ERROR, tag, msg, localStack ? what : tr);
...
}
因此,Log.wtf()和Log.e()都具有相同的优先级,即ERROR。
区别在于Log.wtf()调用onTerribleFailure()回调函数,“报告当前进程中的严重错误。可能会导致进程终止(取决于系统设置)。”
换句话说,Log.wtf()可能会使您的应用程序崩溃。
下面是代码片段:
if (ActivityManager.getService().handleApplicationWtf(
mApplicationObject, tag, system,
new ApplicationErrorReport.ParcelableCrashInfo(t))) {
// The Activity Manager has already written us off -- now exit.
Process.killProcess(Process.myPid());
System.exit(10);
}
我认为wtf(what a terrible failure)用于报告应用程序的严重异常/问题(例如,在调试控制台中报告它们)。
log.e用于报告错误,但不是那么严重。
在我开始从事ROM层工作之前,我并不知道这一点。
如果设置了某些条件,Log.wtf()将终止您的进程。我曾经非常困惑为什么系统服务总是崩溃。原来是因为我使用了Log.wtf(),它会因为某些“永远不应该发生”的事情而被触发。