-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMLog.java
More file actions
executable file
·239 lines (199 loc) · 6.93 KB
/
MLog.java
File metadata and controls
executable file
·239 lines (199 loc) · 6.93 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* Create by Allen
*/
package util;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MLog {
public static Boolean LOG_OUT = true;
private static final int LENGTH = 5;
private static final int V = 0x1;
private static final int D = 0x2;
private static final int I = 0x3;
private static final int W = 0x4;
private static final int E = 0x5;
private static final int A = 0x6;
private static final int JSON = 0x7;
private static final int JSON_INDENT = 4;
/**
* 行号分隔符
*/
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String DEFAULT_MESSAGE = "execute";
public static void v() {
printLog(V, null, DEFAULT_MESSAGE);
}
public static void v(Object msg) {
printLog(V, null, msg);
}
public static void v(String tag, String msg) {
printLog(V, tag, msg);
}
public static void d() {
printLog(D, null, DEFAULT_MESSAGE);
}
public static void d(Object msg) {
printLog(D, null, msg);
}
public static void d(String tag, Object msg) {
printLog(D, tag, msg);
}
public static void i() {
printLog(I, null, DEFAULT_MESSAGE);
}
public static void i(Object msg) {
printLog(I, null, msg);
}
public static void i(String tag, Object msg) {
printLog(I, tag, msg);
}
public static void w() {
printLog(W, null, DEFAULT_MESSAGE);
}
public static void w(Object msg) {
printLog(W, null, msg);
}
public static void w(String tag, Object msg) {
printLog(W, tag, msg);
}
public static void e() {
printLog(E, null, DEFAULT_MESSAGE);
}
public static void e(Object msg) {
printLog(E, null, msg);
}
public static void e(String tag, Object msg) {
printLog(E, tag, msg);
}
public static void a() {
printLog(A, null, DEFAULT_MESSAGE);
}
public static void a(Object msg) {
printLog(A, null, msg);
}
public static void a(String tag, Object msg) {
printLog(A, tag, msg);
}
public static void json(String jsonFormat) {
printLog(JSON, null, jsonFormat);
}
public static void json(String tag, String jsonFormat) {
printLog(JSON, tag, jsonFormat);
}
public static int printMethodName(String TAG) {
if (!LOG_OUT) {
return -1;
}
String msg = "";
StackTraceElement info = LogInfo.getInfoInternal(LENGTH);
if (info != null) {
msg = info.getMethodName() + " # Line " + info.getLineNumber();
}
return Log.i(TAG, msg);
}
public static int printStackTrace(String TAG) {
if (!LOG_OUT) {
return -1;
}
StackTraceElement[] stackTraceElements = new Exception()
.getStackTrace();
if (stackTraceElements != null) {
Log.d(TAG, "printStackTrace:");
for (int i = 1; i < stackTraceElements.length; i++) {
Log.d(TAG, stackTraceElements[i].toString());
}
}
return 0;
}
private static void printLog(int type, String tagStr, Object objectMsg) {
String msg;
if (!LOG_OUT) {
return;
}
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
int index = 4;
String className = stackTraceElements[index].getFileName();
String methodName = stackTraceElements[index].getMethodName();
int lineNumber = stackTraceElements[index].getLineNumber();
String tag = (tagStr == null ? className : tagStr);
methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
StringBuilder builder = new StringBuilder();
builder.append("[ (").append(className).append(":").append(lineNumber).append(")#").append(methodName).append(" ]");
if (objectMsg == null) {
msg = "log with null object";
} else {
msg = objectMsg.toString();
}
if (msg != null && type != JSON) {
builder.append(msg);
}
String logStr = builder.toString();
switch (type) {
case V:
Log.v(tag, logStr);
break;
case D:
Log.d(tag, logStr);
break;
case I:
Log.i(tag, logStr);
break;
case W:
Log.w(tag, logStr);
break;
case E:
Log.e(tag, logStr);
break;
case A:
Log.d(tag, logStr);
break;
case JSON: {
if (TextUtils.isEmpty(msg)) {
Log.d(tag, "Empty or null json content");
return;
}
String message = null;
try {
if (msg.startsWith("{")) {
JSONObject jsonObject = new JSONObject(msg);
message = jsonObject.toString(JSON_INDENT);
} else if (msg.startsWith("[")) {
JSONArray jsonArray = new JSONArray(msg);
message = jsonArray.toString(JSON_INDENT);
}
} catch (JSONException e) {
e(tag, e.getCause().getMessage() + "\n" + msg);
return;
}
printLine(tag, true);
message = logStr + LINE_SEPARATOR + message;
String[] lines = message.split(LINE_SEPARATOR);
StringBuilder jsonContent = new StringBuilder();
for (String line : lines) {
jsonContent.append("|| ").append(line).append(LINE_SEPARATOR);
}
Log.d(tag, jsonContent.toString());
printLine(tag, false);
}
break;
default:
break;
}
}
private static void printLine(String tag, boolean isTop) {
if (isTop) {
Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
} else {
Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════════════════");
}
}
public static void errorLog(Exception e) {
if (e == null) {
return;
}
printLog(E, null, e.getLocalizedMessage());
}
}