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

时间:2011-07-19 19:04:10

标签: android

我想将我的应用的错误报告记录到Android Market错误控制台;看起来我可以使用Log.wtf

Log.wtf的文档说:

  

多么可怕的失败:报告一个永远不会发生的情况。该错误将始终记录在具有调用堆栈的ASSERT级别。根据系统配置,可以将报告添加到DropBoxManager和/或可以使用错误对话框立即终止进程。

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

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

2 个答案:

答案 0 :(得分:12)

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

我建议使用带有前缀而不是Log.wtf()的Log.e()来避免任何问题,例如: WTF: Something terrible happened

以下是调用Log.wtf()

时会发生的情况

- > Log.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;
}

- > Log.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;
} 

答案 1 :(得分:0)

关注nebkat信息。注意使用WTF:设备的API级别必须为8或更高。