Log.java
6.72 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
package com.chudiangameplay.android.tool;
import android.text.TextUtils;
import com.chudiangameplay.android.BuildConfig;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 通用日志打印类 (安全控制)
* 只有A/B版打印 其他版本不打印
*/
public final class Log {
private static final String TAG = "test";
private static boolean logEnabled = false;
static {
// if (Config.curVersion == Config.IS_ALPHA ) {
if (BuildConfig.DEBUG) {
logEnabled = true;
}
}
public static void w(String msg) {
if (logEnabled) {
android.util.Log.w(TAG, getLocation() + msg);
}
}
public static void w(String tag, String msg) {
if (logEnabled) {
android.util.Log.w(tag, getLocation() + msg);
}
}
public static void w(String tag, String msg, Throwable e) {
if (logEnabled) {
android.util.Log.w(tag, getLocation() + msg, e);
}
}
public static void w() {
if (logEnabled) {
android.util.Log.w(TAG, getLocation());
}
}
public static void d() {
if (logEnabled) {
android.util.Log.v(TAG, getLocation());
}
}
public static void d(String msg) {
if (logEnabled) {
android.util.Log.d(TAG, getLocation() + msg);
}
}
public static void d(String tag, String msg) {
if (logEnabled) {
android.util.Log.d(tag, getLocation() + msg);
}
}
public static void d(String tag, String msg, Throwable e) {
if (logEnabled) {
android.util.Log.d(tag, getLocation() + msg, e);
}
}
public static void i(String msg) {
if (logEnabled) {
// android.util.Log.i(TAG, getLocation() + msg);
i(TAG, msg);
}
}
public static void i(String tag, String msg) {
if (logEnabled) {
// if (msg.length() > 4000) {
// for (int i = 0; i < msg.length(); i += 4000) {
// if (i + 4000 < msg.length())
// android.util.Log.i(tag , getLocation() +msg.substring(i, i + 4000));
// else
// android.util.Log.i(tag , getLocation() +msg.substring(i, msg.length()));
// }
// } else {
// android.util.Log.i(tag, getLocation() +msg);
// }
printJson(tag, msg);
}
}
public static void i() {
if (logEnabled) {
android.util.Log.i(TAG, getLocation());
}
}
public static void e(String msg) {
if (logEnabled) {
android.util.Log.e(TAG, getLocation() + msg);
}
}
public static void e(String msg, Throwable e) {
if (logEnabled) {
android.util.Log.e(TAG, getLocation() + msg, e);
}
}
public static void e(String TAG, String msg) {
if (logEnabled) {
android.util.Log.e(TAG, getLocation() + msg);
}
}
public static void e(String TAG, String msg, Throwable e) {
if (logEnabled) {
android.util.Log.e(TAG, getLocation() + msg, e);
}
}
public static void e(Throwable e) {
if (logEnabled) {
android.util.Log.e(TAG, getLocation(), e);
}
}
public static void e() {
if (logEnabled) {
android.util.Log.e(TAG, getLocation());
}
}
public static void v(String TAG, String msg, Throwable e) {
if (logEnabled) {
android.util.Log.v(TAG, getLocation(), e);
}
}
public static void v(String TAG, String msg) {
if (logEnabled) {
android.util.Log.v(TAG, getLocation());
}
}
public static void v(String msg) {
if (logEnabled) {
android.util.Log.v(TAG, msg);
}
}
private static String getLocation() {
final String className = Log.class.getName();
final StackTraceElement[] traces = Thread.currentThread()
.getStackTrace();
boolean found = false;
for (StackTraceElement trace : traces) {
try {
if (found) {
if (!trace.getClassName().startsWith(className)) {
Class<?> clazz = Class.forName(trace.getClassName());
return "[" + getClassName(clazz) + ":"
+ trace.getMethodName() + ":"
+ trace.getLineNumber() + "]: ";
}
} else if (trace.getClassName().startsWith(className)) {
found = true;
}
} catch (ClassNotFoundException ignored) {
}
}
return "[]: ";
}
private static String getClassName(Class<?> clazz) {
if (clazz != null) {
if (!TextUtils.isEmpty(clazz.getSimpleName())) {
return clazz.getSimpleName();
}
return getClassName(clazz.getEnclosingClass());
}
return "";
}
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void printLine(String tag, boolean isTop) {
if (isTop) {
Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
} else {
Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════════════════");
}
}
public static void printJson(String tag, String msg) {
String message;
try {
if (msg.startsWith("{")) {
JSONObject jsonObject = new JSONObject(msg);
message = jsonObject.toString(4);//最重要的方法,就一行,返回格式化的json字符串,其中的数字4是缩进字符数
} else if (msg.startsWith("[")) {
JSONArray jsonArray = new JSONArray(msg);
message = jsonArray.toString(4);
} else {
message = msg;
}
} catch (JSONException e) {
message = msg;
}
printLine(tag, true);
message = LINE_SEPARATOR + message;
String[] lines = message.split(LINE_SEPARATOR);
for (String line : lines) {
Log.d(tag, "║ " + line);
}
printLine(tag, false);
}
}