-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLogUtils.java
194 lines (177 loc) · 5.96 KB
/
LogUtils.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.icodeinjector;
import android.os.SystemClock;
import android.util.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Stack;
public class LogUtils {
public static final String TAG = "codeInjector";
public static final String VER = "v1.1";
public static final int sDefaultMethodPosInThread = 5;
public static int sCurrentMethodPosInThread = 0;
public static String[] sLogWhiteListPackage = {"com.antfortune", "com.tencent"};
public static String[] sLogBlackListPackage = {"com.tencent.gmtrace"};
public static long fileModifyTime = System.currentTimeMillis();
public static String logSwitch = "1";
public static final String sConfigFilePath = "/data/local/tmp/log.txt";
public static File file = new File(sConfigFilePath);
public static String cmdLineStr = null;
public static Stack<Long> sMethodStartTimeRecordStack = new Stack<Long>();
static {
Log.i(TAG, "LogUtils Ver. " + VER);
cmdLineStr = getCmdArgs();
parseCmdLineArgs(cmdLineStr);
fileModifyTime = file.lastModified();
}
public static void preLog() {
if (!isShowLog()) {
return;
}
StackTraceElement element = getCurrentMethodName("preLog");
if (shouldPrintLog(element.toString())) {
Log.i(TAG, element + " IN");
sMethodStartTimeRecordStack.push(SystemClock.elapsedRealtime());
}
}
public static void postLog() {
if (!isShowLog()) {
return;
}
StackTraceElement element = getCurrentMethodName("postLog");
if (shouldPrintLog(element.toString())) {
long methodTime = getMethodTime();
Log.i(TAG, element + " OUT cost:" + methodTime + "ms");
}
}
public static void postLog(int outPos) {
if (!isShowLog()) {
return;
}
StackTraceElement element = getCurrentMethodName("postLog");
if (shouldPrintLog(element.toString())) {
long methodTime = getMethodTime();
Log.i(TAG, element + " OUT #" + outPos + " cost:" + methodTime + "ms");
}
}
public static long getMethodTime() {
if (sMethodStartTimeRecordStack.size() > 0) {
long methodStartTime = sMethodStartTimeRecordStack.pop();
return SystemClock.elapsedRealtime() - methodStartTime;
}
return 0;
}
public static boolean shouldPrintLog(String threadStackElement) {
if (!isShowLog()) {
return false;
}
if (sLogWhiteListPackage.length > 0) {
for (String blackListPackage : sLogBlackListPackage) {
if (threadStackElement.contains(blackListPackage)) {
return false;
}
}
for (String whileListPackage : sLogWhiteListPackage) {
if (threadStackElement.contains(whileListPackage)) {
return true;
}
}
return false;
} else {
for (String blackListPackage : sLogBlackListPackage) {
if (threadStackElement.contains(blackListPackage)) {
return false;
}
}
return true;
}
}
public static StackTraceElement getCurrentMethodName(String preMethodName) {
boolean needFindMethodPos = true;
boolean foundMethodPos = false;
StackTraceElement[] stackElements = Thread.currentThread().getStackTrace();
if (sCurrentMethodPosInThread != 0) {
if (stackElements[sCurrentMethodPosInThread - 2].toString().contains(preMethodName)) {
needFindMethodPos = false;
}
}
if (needFindMethodPos) {
for (int i = 0; i < stackElements.length; i++) {
StackTraceElement element = stackElements[i];
if (element.toString().contains(preMethodName)) {
sCurrentMethodPosInThread = (i + 2);
foundMethodPos = true;
break;
}
}
if (!foundMethodPos) {
sCurrentMethodPosInThread = sDefaultMethodPosInThread;
}
}
if (stackElements.length >= sCurrentMethodPosInThread) {
return stackElements[sCurrentMethodPosInThread - 1];
} else {
return stackElements[stackElements.length - 1];
}
}
/**
* @return
*/
private static boolean isShowLog() {
if (fileModifyTime != file.lastModified()) {
fileModifyTime = file.lastModified();
cmdLineStr = getCmdArgs();
parseCmdLineArgs(cmdLineStr);
}
if ("1".equals(logSwitch)) {
return true;
}
return false;
}
/**
* 获取命令行参数信息
*
* @return
*/
public static String getCmdArgs() {
File file = new File(sConfigFilePath);
BufferedReader bf = null;
try {
bf = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bf.readLine()) != null) {
if (line != null) {
return line;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bf != null)
bf.close();
} catch (Exception e) {
}
}
return null;
}
/**
* 解析命令行参数信息
* -s 1
*
* @param cmdLine
*/
private static void parseCmdLineArgs(String cmdLine) {
try {
String[] strAry = cmdLine.split(" ");
for (int i = 0; i < strAry.length; i += 2) {
switch (strAry[i]) {
case "-s":
logSwitch = strAry[i + 1];
break;
}
}
} catch (Exception e) {
}
}
}