gpt4 book ai didi

android - Android 的 Log.wtf 在什么情况下会终止我的应用程序?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:38 34 4
gpt4 key购买 nike

我想将我的应用程序的错误报告记录到 Android 电子市场错误控制台;看起来我可以使用 Log.wtf为此。

Log.wtf 的文档说:

What a Terrible Failure: Report a condition that should never happen. The error will always be logged at level ASSERT with the call stack. Depending on system configuration, a report may be added to the DropBoxManager and/or the process may be terminated immediately with an error dialog.

在我的例子中,我可以捕获这些异常并通过显示错误消息从中恢复;我不希望我的应用程序崩溃,但我确实希望将报告发送到错误控制台。

在什么情况下 Log.wtf 会终止我的应用程序?是否有可能在不导致应用程序崩溃的情况下获取错误报告?

最佳答案

这取决于您的系统设置(某些选项可以启用调试但在普通设备上禁用)。它们是在为设备和可能的内核编译 android 时启用的设置。

我建议使用带前缀的 Log.e() 而不是 Log.wtf() 以避免任何问题,例如WTF:发生了可怕的事情

这是调用 Log.wtf() 时发生的情况

-> 日志.java

/**
* What a Terrible Failure: Report an exception that should never happen.
* Similar to {@link #wtf(String, Throwable)}, with a message as well.
* @param tag Used to identify the source of a log message.
* @param msg The message you would like logged.
* @param tr An exception to log. May be null.
*/
public static int wtf(String tag, String msg, Throwable tr) {
TerribleFailure what = new TerribleFailure(msg, tr);
int bytes = println_native(LOG_ID_MAIN, ASSERT, tag, getStackTraceString(tr));
sWtfHandler.onTerribleFailure(tag, what);
return bytes;
}

-> 日志.java

private static TerribleFailureHandler sWtfHandler = new TerribleFailureHandler() {
public void onTerribleFailure(String tag, TerribleFailure what) {
RuntimeInit.wtf(tag, what);
}
};

->RuntimeInit.java

/**
* Report a serious error in the current process. May or may not cause
* the process to terminate (depends on system settings).
*
* @param tag to record with the error
* @param t exception describing the error site and conditions
*/
public static void wtf(String tag, Throwable t) {
try {
if (ActivityManagerNative.getDefault()
.handleApplicationWtf(mApplicationObject, tag,
new ApplicationErrorReport.CrashInfo(t))) {
// The Activity Manager has already written us off -- now exit.
Process.killProcess(Process.myPid());
System.exit(10);
}
} catch (Throwable t2) {
Slog.e(TAG, "Error reporting WTF", t2);
}
}

->ActivityManagerNative.java

public boolean handleApplicationWtf(IBinder app, String tag,
ApplicationErrorReport.CrashInfo crashInfo)
throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IActivityManager.descriptor);
data.writeStrongBinder(app);
data.writeString(tag);
crashInfo.writeToParcel(data, 0);
mRemote.transact(HANDLE_APPLICATION_WTF_TRANSACTION, data,
reply, 0);
reply.readException();
boolean res = reply.readInt() != 0;
reply.recycle();
data.recycle();
return res;
}

关于android - Android 的 Log.wtf 在什么情况下会终止我的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6752642/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com