1

在一个非常简短的 LVL 回调函数中,我有:

public void applicationError(ApplicationErrorCode errorCode) {
  Log.v(getMethodName(2), "TID: " + android.os.Process.myTid());
  Log.v("applicationError()", "TID: " + android.os.Process.myTid());
  if (!isFinishing()) { // Don't update UI if Activity is finishing.
    String result = String.format(getString(R.string.application_error), errorCode);
    displayResult(result);
  }

这个回调是一个方法成员

 private class MyLicenseCheckerCallback implements LicenseCheckerCallback 

并将 getMethodName() 定义为应用程序的活动方法:

 public static String getMethodName(final int depth)
  {
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();

    String retStr = ste[depth+1].getClassName() + "." + ste[depth+1].getMethodName();

    if (retStr == null)
      retStr = "This is weird.";
    else if (retStr.length() < 1)
      retStr = "This is even weirder.";

    return retStr;
  }

现在,奇怪的部分:

LogCat总是显示第二个Log.v()但从显示第一个Log.v()

起初,我认为 retStr 可能为 null 或为空,但日志显然没有显示类似的内容。

在其他方法中调用Log.v(getMethodName(2), "TID: " + android.os.Process.myTid());一切正常。

什么可以解释这种奇怪和不一致的Log.v()行为?

更新:我在 CheGuaVerra 的回答中实现了第一部分,这是我得到的有趣日志:

06-16 01:02:57.080: INFO/First(835): Log.v
06-16 01:02:57.080: INFO/First(835): [ 06-16 01:02:57.080   835:0x34b V/com.example.baseapp.googlepaid.MyActivityGooglePaid$MyLicenseCheckerCallback.applTID: 843
06-16 01:02:57.080: INFO/Second(835): Log.v
06-16 01:02:57.080: VERBOSE/applicationError()(835): TID: 843

更新 2:我在 CheGuaVerra 的回答中实现了这两个部分,这是我得到的有趣日志:

06-16 10:13:22.588: INFO/First(1122): Log.v
06-16 10:13:22.588: INFO/getMethodName(1122): Start
06-16 10:13:22.588: INFO/getMethodName(1122): [ 06-16 10:13:22.588  1122:0x46a V/com.example.baseapp.googlepaid.MyActivityGooglePaid$MyLicenseCheckerCallback.applTID: 1130
06-16 10:13:22.588: INFO/Second(1122): Log.v
06-16 10:13:22.588: VERBOSE/applicationError()(1122): TID: 1130

是否有可能Thread.currentThread().getStackTrace()[depth+1].getMethodName()根本不“喜欢”从 UI 线程调用?

此外,它似乎Log.v()不喜欢像[ 06-16 10:13:22.588 1122:0x46a V/com.example.baseapp.googlepaid.MyActivityGooglePaid$MyLicenseCheckerCallback.appl.

4

1 回答 1

1

你有没有尝试做这样的事情来追踪它?

public void applicationError(ApplicationErrorCode errorCode) {     
  Log.i("First", "Log.v");

  Log.v(getMethodName(2), "TID: " + android.os.Process.myTid());

  Log.i("Second", "Log.v");

  Log.v("applicationError()", "TID: " + android.os.Process.myTid());

  if (!isFinishing()) { // Don't update UI if Activity is finishing.
    String result = String.format(getString(R.string.application_error), errorCode);

  displayResult(result);
}


 public static String getMethodName(final int depth)
  {
    Log.i("getMethodName", "Start");
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();

    String retStr = ste[depth+1].getClassName() + "." + ste[depth+1].getMethodName();

    if (retStr == null)
      retStr = "This is weird.";
    else if (retStr.length() < 1)
      retStr = "This is even weirder.";

    Log.i("getMethodeName", retStr);

    return retStr;
  }
于 2011-06-16T02:25:21.837 回答