ToastLogInterceptor.java
1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.studymachine.www.other;
import com.studymachine.www.action.ToastAction;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.config.IToastInterceptor;
import timber.log.Timber;
/**
* time : 2020/11/04
* desc : 自定义 Toast 拦截器(用于追踪 Toast 调用的位置)
*/
public final class ToastLogInterceptor implements IToastInterceptor {
@Override
public boolean intercept(CharSequence text) {
if (AppConfig.isLogEnable()) {
// 获取调用的堆栈信息
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
// 跳过最前面两个堆栈
for (int i = 2; stackTrace.length > 2 && i < stackTrace.length; i++) {
// 获取代码行数
int lineNumber = stackTrace[i].getLineNumber();
// 获取类的全路径
String className = stackTrace[i].getClassName();
if (lineNumber <= 0 || className.startsWith(ToastUtils.class.getName()) ||
className.startsWith(ToastAction.class.getName())) {
continue;
}
Timber.tag("ToastUtils");
Timber.i("(" + stackTrace[i].getFileName() + ":" + lineNumber + ") " + text.toString());
break;
}
}
return false;
}
}