使用Android的PJSUA2示例,MyApp.java类有一个日志打印机:
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”类扩展:
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文件,没有一个叫'sip_transport'。
有没有办法让这个日志显示我的调用位置?或者找到这个解析问题源的任何其他方法?
答案 0 :(得分:1)
您可以使用线程的stacktrace
获取调用方法详细信息。
StackTraceElement caller = Thread.currentThread().getStackTrace()[1];
caller.getClassName();
caller.getMethodName();
caller.getFileName();
caller.getLineNumber();
请注意,选择第一索引是因为 0 是当前方法(堆栈顶部),并且随着每个调用方法而下降。因此,您可以选择所需的堆栈中的任何调用方。