从 PJSIP 示例 (PJSUA2) 中调用 LogWriter 的位置打印

Print from where LogWriter was called in PJSIP example (PJSUA2)

对于 android 的 PJSUA2 示例,MyApp.java class 有一个日志打印机:

class MyLogWriter extends LogWriter
{
    public String appLabel = "PJSUA (MyApp - MyLogWriter)";

    @Override
    public void write(LogEntry entry) {
        Log.i(appLabel, "LW("+entry.getLevel()+") Msg: " + entry.getMsg());
    }
}

扩展自 "LogWriter" class:

package org.pjsip.pjsua2;

public class LogWriter {
  private long swigCPtr;
  protected boolean swigCMemOwn;

  protected LogWriter(long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;
  }

  protected static long getCPtr(LogWriter obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        pjsua2JNI.delete_LogWriter(swigCPtr);
      }
      swigCPtr = 0;
    }
  }

  protected void swigDirectorDisconnect() {
    swigCMemOwn = false;
    delete();
  }

  public void swigReleaseOwnership() {
    swigCMemOwn = false;
    pjsua2JNI.LogWriter_change_ownership(this, swigCPtr, false);
  }

  public void swigTakeOwnership() {
    swigCMemOwn = true;
    pjsua2JNI.LogWriter_change_ownership(this, swigCPtr, true);
  }

  public void write(LogEntry entry) {
    pjsua2JNI.LogWriter_write(swigCPtr, this, LogEntry.getCPtr(entry), entry);
  }

  public LogWriter() {
    this(pjsua2JNI.new_LogWriter(), true);
    pjsua2JNI.LogWriter_director_connect(this, swigCPtr, swigCMemOwn, true);
  }

}

它确实打印了一个非常完整的日志,

07-02 09:22:13.791: I/PJSUA (MyApp - MyLogWriter)(21597): LW(1) Msg: 09:22:13.788 sip_transport.  Error processing 575 bytes packet from UDP 107.170.134.248:5060 : PJSIP syntax error exception when parsing 'To' header on line 5 col 38:

据此我知道我的问题出在解析中,问题出在哪个文件中? PJSUA 有 203 个 .java 文件,其中 none 个名为 'sip_transport'.

有没有办法让这个日志显示调用位置?或者任何其他方式来找到这个解析问题的根源?

您可以使用线程的stacktrace获取调用方法的详细信息。

StackTraceElement caller = Thread.currentThread().getStackTrace()[1];

caller.getClassName();
caller.getMethodName();
caller.getFileName();
caller.getLineNumber();

请注意,选择 1st 索引是因为 0 是当前方法(堆栈顶部),并且随着每个调用方法。所以你可以选择你想要的堆栈中的任何调用者。