什么可以解释这个奇怪的Log.v()行为?

时间:2011-06-16 02:09:45

标签: android logcat android-logcat

在一个非常简短的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

1 个答案:

答案 0 :(得分: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;
  }