|
|
1
|
+package com.gimi.common.cinema.utils;
|
|
|
2
|
+
|
|
|
3
|
+import android.os.Environment;
|
|
|
4
|
+import android.support.annotation.IntDef;
|
|
|
5
|
+import android.util.Log;
|
|
|
6
|
+import org.json.JSONArray;
|
|
|
7
|
+import org.json.JSONException;
|
|
|
8
|
+import org.json.JSONObject;
|
|
|
9
|
+
|
|
|
10
|
+import javax.xml.transform.OutputKeys;
|
|
|
11
|
+import javax.xml.transform.Source;
|
|
|
12
|
+import javax.xml.transform.Transformer;
|
|
|
13
|
+import javax.xml.transform.TransformerFactory;
|
|
|
14
|
+import javax.xml.transform.stream.StreamResult;
|
|
|
15
|
+import javax.xml.transform.stream.StreamSource;
|
|
|
16
|
+import java.io.BufferedWriter;
|
|
|
17
|
+import java.io.File;
|
|
|
18
|
+import java.io.FileWriter;
|
|
|
19
|
+import java.io.IOException;
|
|
|
20
|
+import java.io.StringReader;
|
|
|
21
|
+import java.io.StringWriter;
|
|
|
22
|
+import java.lang.annotation.Retention;
|
|
|
23
|
+import java.lang.annotation.RetentionPolicy;
|
|
|
24
|
+import java.text.SimpleDateFormat;
|
|
|
25
|
+import java.util.Date;
|
|
|
26
|
+import java.util.Formatter;
|
|
|
27
|
+import java.util.Locale;
|
|
|
28
|
+
|
|
|
29
|
+/**
|
|
|
30
|
+ * <pre>
|
|
|
31
|
+ * author: Blankj
|
|
|
32
|
+ * blog : http://blankj.com
|
|
|
33
|
+ * time : 2016/9/21
|
|
|
34
|
+ * desc : 日志相关工具类
|
|
|
35
|
+ * </pre>
|
|
|
36
|
+ */
|
|
|
37
|
+public final class LogUtils {
|
|
|
38
|
+
|
|
|
39
|
+ private LogUtils() {
|
|
|
40
|
+ throw new UnsupportedOperationException("u can't instantiate me...");
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+ public static final int V = 0x01;
|
|
|
44
|
+ public static final int D = 0x02;
|
|
|
45
|
+ public static final int I = 0x04;
|
|
|
46
|
+ public static final int W = 0x08;
|
|
|
47
|
+ public static final int E = 0x10;
|
|
|
48
|
+ public static final int A = 0x20;
|
|
|
49
|
+
|
|
|
50
|
+ @IntDef({V, D, I, W, E, A})
|
|
|
51
|
+ @Retention(RetentionPolicy.SOURCE)
|
|
|
52
|
+ public @interface TYPE {
|
|
|
53
|
+ }
|
|
|
54
|
+
|
|
|
55
|
+ private static final int FILE = 0xF1;
|
|
|
56
|
+ private static final int JSON = 0xF2;
|
|
|
57
|
+ private static final int XML = 0xF4;
|
|
|
58
|
+
|
|
|
59
|
+ private static String dir; // log存储目录
|
|
|
60
|
+ private static boolean sLogSwitch = true; // log总开关
|
|
|
61
|
+ private static String sGlobalTag = null; // log标签
|
|
|
62
|
+ private static boolean sTagIsSpace = true; // log标签是否为空白
|
|
|
63
|
+ private static boolean sLog2FileSwitch = false;// log写入文件开关
|
|
|
64
|
+ private static boolean sLogBorderSwitch = true; // log边框开关
|
|
|
65
|
+ private static int sLogFilter = V; // log过滤器
|
|
|
66
|
+
|
|
|
67
|
+ private static final String TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════";
|
|
|
68
|
+ private static final String LEFT_BORDER = "║ ";
|
|
|
69
|
+ private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════";
|
|
|
70
|
+ private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
|
|
71
|
+
|
|
|
72
|
+ private static final int MAX_LEN = 4000;
|
|
|
73
|
+ private static final String NULL_TIPS = "Log with null object.";
|
|
|
74
|
+ private static final String NULL = "null";
|
|
|
75
|
+ private static final String ARGS = "args";
|
|
|
76
|
+
|
|
|
77
|
+ public static class Builder {
|
|
|
78
|
+
|
|
|
79
|
+ public Builder() {
|
|
|
80
|
+ if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
|
|
81
|
+// dir = Utils.getContext().getExternalCacheDir() + File.separator + "log" + File.separator;
|
|
|
82
|
+ } else {
|
|
|
83
|
+// dir = Utils.getContext().getCacheDir() + File.separator + "log" + File.separator;
|
|
|
84
|
+ }
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ public Builder setGlobalTag(String tag) {
|
|
|
88
|
+ if (!isSpace(tag)) {
|
|
|
89
|
+ LogUtils.sGlobalTag = tag;
|
|
|
90
|
+ sTagIsSpace = false;
|
|
|
91
|
+ } else {
|
|
|
92
|
+ LogUtils.sGlobalTag = "";
|
|
|
93
|
+ sTagIsSpace = true;
|
|
|
94
|
+ }
|
|
|
95
|
+ return this;
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ public Builder setLogSwitch(boolean logSwitch) {
|
|
|
99
|
+ LogUtils.sLogSwitch = logSwitch;
|
|
|
100
|
+ return this;
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ public Builder setLog2FileSwitch(boolean log2FileSwitch) {
|
|
|
104
|
+ LogUtils.sLog2FileSwitch = log2FileSwitch;
|
|
|
105
|
+ return this;
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
|
108
|
+ public Builder setBorderSwitch(boolean borderSwitch) {
|
|
|
109
|
+ LogUtils.sLogBorderSwitch = borderSwitch;
|
|
|
110
|
+ return this;
|
|
|
111
|
+ }
|
|
|
112
|
+
|
|
|
113
|
+ public Builder setLogFilter(@TYPE int logFilter) {
|
|
|
114
|
+ LogUtils.sLogFilter = logFilter;
|
|
|
115
|
+ return this;
|
|
|
116
|
+ }
|
|
|
117
|
+ }
|
|
|
118
|
+
|
|
|
119
|
+ public static void v(Object contents) {
|
|
|
120
|
+ log(V, sGlobalTag, contents);
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ public static void v(String tag, Object... contents) {
|
|
|
124
|
+ log(V, tag, contents);
|
|
|
125
|
+ }
|
|
|
126
|
+
|
|
|
127
|
+ public static void d(Object contents) {
|
|
|
128
|
+ log(D, sGlobalTag, contents);
|
|
|
129
|
+ }
|
|
|
130
|
+
|
|
|
131
|
+ public static void d(String tag, Object... contents) {
|
|
|
132
|
+ log(D, tag, contents);
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ public static void i(Object contents) {
|
|
|
136
|
+ log(I, sGlobalTag, contents);
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ public static void i(String tag, Object... contents) {
|
|
|
140
|
+ log(I, tag, contents);
|
|
|
141
|
+ }
|
|
|
142
|
+
|
|
|
143
|
+ public static void w(Object contents) {
|
|
|
144
|
+ log(W, sGlobalTag, contents);
|
|
|
145
|
+ }
|
|
|
146
|
+
|
|
|
147
|
+ public static void w(String tag, Object... contents) {
|
|
|
148
|
+ log(W, tag, contents);
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ public static void e(Object contents) {
|
|
|
152
|
+ log(E, sGlobalTag, contents);
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ public static void e(String tag, Object... contents) {
|
|
|
156
|
+ log(E, tag, contents);
|
|
|
157
|
+ }
|
|
|
158
|
+
|
|
|
159
|
+ public static void a(Object contents) {
|
|
|
160
|
+ log(A, sGlobalTag, contents);
|
|
|
161
|
+ }
|
|
|
162
|
+
|
|
|
163
|
+ public static void a(String tag, Object... contents) {
|
|
|
164
|
+ log(A, tag, contents);
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ public static void file(Object contents) {
|
|
|
168
|
+ log(FILE, sGlobalTag, contents);
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ public static void file(String tag, Object contents) {
|
|
|
172
|
+ log(FILE, tag, contents);
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ public static void json(String contents) {
|
|
|
176
|
+ log(JSON, sGlobalTag, contents);
|
|
|
177
|
+ }
|
|
|
178
|
+
|
|
|
179
|
+ public static void json(String tag, String contents) {
|
|
|
180
|
+ log(JSON, tag, contents);
|
|
|
181
|
+ }
|
|
|
182
|
+
|
|
|
183
|
+ public static void xml(String contents) {
|
|
|
184
|
+ log(XML, sGlobalTag, contents);
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+ public static void xml(String tag, String contents) {
|
|
|
188
|
+ log(XML, tag, contents);
|
|
|
189
|
+ }
|
|
|
190
|
+
|
|
|
191
|
+ private static void log(int type, String tag, Object... contents) {
|
|
|
192
|
+ if (!sLogSwitch) return;
|
|
|
193
|
+ final String[] processContents = processContents(type, tag, contents);
|
|
|
194
|
+ tag = processContents[0];
|
|
|
195
|
+ String msg = processContents[1];
|
|
|
196
|
+ switch (type) {
|
|
|
197
|
+ case V:
|
|
|
198
|
+ case D:
|
|
|
199
|
+ case I:
|
|
|
200
|
+ case W:
|
|
|
201
|
+ case E:
|
|
|
202
|
+ case A:
|
|
|
203
|
+ if (V == sLogFilter || type >= sLogFilter) {
|
|
|
204
|
+ printLog(type, tag, msg);
|
|
|
205
|
+ }
|
|
|
206
|
+ if (sLog2FileSwitch) {
|
|
|
207
|
+ print2File(tag, msg);
|
|
|
208
|
+ }
|
|
|
209
|
+ break;
|
|
|
210
|
+ case FILE:
|
|
|
211
|
+ print2File(tag, msg);
|
|
|
212
|
+ break;
|
|
|
213
|
+ case JSON:
|
|
|
214
|
+ printLog(D, tag, msg);
|
|
|
215
|
+ break;
|
|
|
216
|
+ case XML:
|
|
|
217
|
+ printLog(D, tag, msg);
|
|
|
218
|
+ break;
|
|
|
219
|
+ }
|
|
|
220
|
+
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+ private static String[] processContents(int type, String tag, Object... contents) {
|
|
|
224
|
+ StackTraceElement targetElement = Thread.currentThread().getStackTrace()[5];
|
|
|
225
|
+ String className = targetElement.getClassName();
|
|
|
226
|
+ String[] classNameInfo = className.split("\\.");
|
|
|
227
|
+ if (classNameInfo.length > 0) {
|
|
|
228
|
+ className = classNameInfo[classNameInfo.length - 1];
|
|
|
229
|
+ }
|
|
|
230
|
+ if (className.contains("$")) {
|
|
|
231
|
+ className = className.split("\\$")[0];
|
|
|
232
|
+ }
|
|
|
233
|
+ if (!sTagIsSpace) {// 如果全局tag不为空,那就用全局tag
|
|
|
234
|
+ tag = sGlobalTag;
|
|
|
235
|
+ } else {// 全局tag为空时,如果传入的tag为空那就显示类名,否则显示tag
|
|
|
236
|
+ tag = isSpace(tag) ? className : tag;
|
|
|
237
|
+ }
|
|
|
238
|
+
|
|
|
239
|
+ String head = new Formatter()
|
|
|
240
|
+ .format("Thread: %s, %s(%s.java:%d)" + LINE_SEPARATOR,
|
|
|
241
|
+ Thread.currentThread().getName(),
|
|
|
242
|
+ targetElement.getMethodName(),
|
|
|
243
|
+ className,
|
|
|
244
|
+ targetElement.getLineNumber())
|
|
|
245
|
+ .toString();
|
|
|
246
|
+ String msg = NULL_TIPS;
|
|
|
247
|
+ if (contents != null) {
|
|
|
248
|
+ if (contents.length == 1) {
|
|
|
249
|
+ Object object = contents[0];
|
|
|
250
|
+ msg = object == null ? NULL : object.toString();
|
|
|
251
|
+ if (type == JSON) {
|
|
|
252
|
+ msg = formatJson(msg);
|
|
|
253
|
+ } else if (type == XML) {
|
|
|
254
|
+ msg = formatXml(msg);
|
|
|
255
|
+ }
|
|
|
256
|
+ } else {
|
|
|
257
|
+ StringBuilder sb = new StringBuilder();
|
|
|
258
|
+ for (int i = 0, len = contents.length; i < len; ++i) {
|
|
|
259
|
+ Object content = contents[i];
|
|
|
260
|
+ sb.append(ARGS)
|
|
|
261
|
+ .append("[")
|
|
|
262
|
+ .append(i)
|
|
|
263
|
+ .append("]")
|
|
|
264
|
+ .append(" = ")
|
|
|
265
|
+ .append(content == null ? NULL : content.toString())
|
|
|
266
|
+ .append(LINE_SEPARATOR);
|
|
|
267
|
+ }
|
|
|
268
|
+ msg = sb.toString();
|
|
|
269
|
+ }
|
|
|
270
|
+ }
|
|
|
271
|
+ if (sLogBorderSwitch) {
|
|
|
272
|
+ StringBuilder sb = new StringBuilder();
|
|
|
273
|
+ String[] lines = msg.split(LINE_SEPARATOR);
|
|
|
274
|
+ for (String line : lines) {
|
|
|
275
|
+ sb.append(LEFT_BORDER).append(line).append(LINE_SEPARATOR);
|
|
|
276
|
+ }
|
|
|
277
|
+ msg = sb.toString();
|
|
|
278
|
+ }
|
|
|
279
|
+ return new String[]{tag, head + msg};
|
|
|
280
|
+ }
|
|
|
281
|
+
|
|
|
282
|
+ private static String formatJson(String json) {
|
|
|
283
|
+ try {
|
|
|
284
|
+ if (json.startsWith("{")) {
|
|
|
285
|
+ json = new JSONObject(json).toString(4);
|
|
|
286
|
+ } else if (json.startsWith("[")) {
|
|
|
287
|
+ json = new JSONArray(json).toString(4);
|
|
|
288
|
+ }
|
|
|
289
|
+ } catch (JSONException e) {
|
|
|
290
|
+ e.printStackTrace();
|
|
|
291
|
+ }
|
|
|
292
|
+ return json;
|
|
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+ private static String formatXml(String xml) {
|
|
|
296
|
+ try {
|
|
|
297
|
+ Source xmlInput = new StreamSource(new StringReader(xml));
|
|
|
298
|
+ StreamResult xmlOutput = new StreamResult(new StringWriter());
|
|
|
299
|
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
|
|
300
|
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
|
301
|
+ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
|
|
|
302
|
+ transformer.transform(xmlInput, xmlOutput);
|
|
|
303
|
+ xml = xmlOutput.getWriter().toString().replaceFirst(">", ">" + LINE_SEPARATOR);
|
|
|
304
|
+ } catch (Exception e) {
|
|
|
305
|
+ e.printStackTrace();
|
|
|
306
|
+ }
|
|
|
307
|
+ return xml;
|
|
|
308
|
+ }
|
|
|
309
|
+
|
|
|
310
|
+ private static void printLog(int type, String tag, String msg) {
|
|
|
311
|
+ if (sLogBorderSwitch) printBorder(type, tag, true);
|
|
|
312
|
+ int len = msg.length();
|
|
|
313
|
+ int countOfSub = len / MAX_LEN;
|
|
|
314
|
+ if (countOfSub > 0) {
|
|
|
315
|
+ int index = 0;
|
|
|
316
|
+ String sub;
|
|
|
317
|
+ for (int i = 0; i < countOfSub; i++) {
|
|
|
318
|
+ sub = msg.substring(index, index + MAX_LEN);
|
|
|
319
|
+ printSubLog(type, tag, sub);
|
|
|
320
|
+ index += MAX_LEN;
|
|
|
321
|
+ }
|
|
|
322
|
+ printSubLog(type, tag, msg.substring(index, len));
|
|
|
323
|
+ } else {
|
|
|
324
|
+ printSubLog(type, tag, msg);
|
|
|
325
|
+ }
|
|
|
326
|
+ if (sLogBorderSwitch) printBorder(type, tag, false);
|
|
|
327
|
+ }
|
|
|
328
|
+
|
|
|
329
|
+ private static void printSubLog(final int type, final String tag, String msg) {
|
|
|
330
|
+ if (sLogBorderSwitch) msg = LEFT_BORDER + msg;
|
|
|
331
|
+ switch (type) {
|
|
|
332
|
+ case V:
|
|
|
333
|
+ Log.v(tag, msg);
|
|
|
334
|
+ break;
|
|
|
335
|
+ case D:
|
|
|
336
|
+ Log.d(tag, msg);
|
|
|
337
|
+ break;
|
|
|
338
|
+ case I:
|
|
|
339
|
+ Log.i(tag, msg);
|
|
|
340
|
+ break;
|
|
|
341
|
+ case W:
|
|
|
342
|
+ Log.w(tag, msg);
|
|
|
343
|
+ break;
|
|
|
344
|
+ case E:
|
|
|
345
|
+ Log.e(tag, msg);
|
|
|
346
|
+ break;
|
|
|
347
|
+ case A:
|
|
|
348
|
+ Log.wtf(tag, msg);
|
|
|
349
|
+ break;
|
|
|
350
|
+ }
|
|
|
351
|
+ }
|
|
|
352
|
+
|
|
|
353
|
+ private static void printBorder(int type, String tag, boolean isTop) {
|
|
|
354
|
+ String border = isTop ? TOP_BORDER : BOTTOM_BORDER;
|
|
|
355
|
+ switch (type) {
|
|
|
356
|
+ case V:
|
|
|
357
|
+ Log.v(tag, border);
|
|
|
358
|
+ break;
|
|
|
359
|
+ case D:
|
|
|
360
|
+ Log.d(tag, border);
|
|
|
361
|
+ break;
|
|
|
362
|
+ case I:
|
|
|
363
|
+ Log.i(tag, border);
|
|
|
364
|
+ break;
|
|
|
365
|
+ case W:
|
|
|
366
|
+ Log.w(tag, border);
|
|
|
367
|
+ break;
|
|
|
368
|
+ case E:
|
|
|
369
|
+ Log.e(tag, border);
|
|
|
370
|
+ break;
|
|
|
371
|
+ case A:
|
|
|
372
|
+ Log.wtf(tag, border);
|
|
|
373
|
+ break;
|
|
|
374
|
+ }
|
|
|
375
|
+ }
|
|
|
376
|
+
|
|
|
377
|
+ private synchronized static void print2File(final String tag, final String msg) {
|
|
|
378
|
+ Date now = new Date();
|
|
|
379
|
+ String date = new SimpleDateFormat("MM-dd", Locale.getDefault()).format(now);
|
|
|
380
|
+ final String fullPath = dir + date + ".txt";
|
|
|
381
|
+ if (!createOrExistsFile(fullPath)) {
|
|
|
382
|
+ Log.e(tag, "log to " + fullPath + " failed!");
|
|
|
383
|
+ return;
|
|
|
384
|
+ }
|
|
|
385
|
+ String time = new SimpleDateFormat("MM-dd HH:mm:ss.SSS ", Locale.getDefault()).format(now);
|
|
|
386
|
+ StringBuilder sb = new StringBuilder();
|
|
|
387
|
+ if (sLogBorderSwitch) sb.append(TOP_BORDER).append(LINE_SEPARATOR);
|
|
|
388
|
+ sb.append(time)
|
|
|
389
|
+ .append(tag)
|
|
|
390
|
+ .append(": ")
|
|
|
391
|
+ .append(msg)
|
|
|
392
|
+ .append(LINE_SEPARATOR);
|
|
|
393
|
+ if (sLogBorderSwitch) sb.append(BOTTOM_BORDER).append(LINE_SEPARATOR);
|
|
|
394
|
+ final String dateLogContent = sb.toString();
|
|
|
395
|
+ new Thread(new Runnable() {
|
|
|
396
|
+ @Override
|
|
|
397
|
+ public void run() {
|
|
|
398
|
+ BufferedWriter bw = null;
|
|
|
399
|
+ try {
|
|
|
400
|
+ bw = new BufferedWriter(new FileWriter(fullPath, true));
|
|
|
401
|
+ bw.write(dateLogContent);
|
|
|
402
|
+ Log.d(tag, "log to " + fullPath + " success!");
|
|
|
403
|
+ } catch (IOException e) {
|
|
|
404
|
+ e.printStackTrace();
|
|
|
405
|
+ Log.e(tag, "log to " + fullPath + " failed!");
|
|
|
406
|
+ } finally {
|
|
|
407
|
+ try {
|
|
|
408
|
+ if (bw != null) {
|
|
|
409
|
+ bw.close();
|
|
|
410
|
+ }
|
|
|
411
|
+ } catch (IOException e) {
|
|
|
412
|
+ e.printStackTrace();
|
|
|
413
|
+ }
|
|
|
414
|
+ }
|
|
|
415
|
+ }
|
|
|
416
|
+ }).start();
|
|
|
417
|
+ }
|
|
|
418
|
+
|
|
|
419
|
+ private static boolean createOrExistsFile(String filePath) {
|
|
|
420
|
+ return createOrExistsFile(isSpace(filePath) ? null : new File(filePath));
|
|
|
421
|
+ }
|
|
|
422
|
+
|
|
|
423
|
+ private static boolean createOrExistsFile(File file) {
|
|
|
424
|
+ if (file == null) return false;
|
|
|
425
|
+ if (file.exists()) return file.isFile();
|
|
|
426
|
+ if (!createOrExistsDir(file.getParentFile())) return false;
|
|
|
427
|
+ try {
|
|
|
428
|
+ return file.createNewFile();
|
|
|
429
|
+ } catch (IOException e) {
|
|
|
430
|
+ e.printStackTrace();
|
|
|
431
|
+ return false;
|
|
|
432
|
+ }
|
|
|
433
|
+ }
|
|
|
434
|
+
|
|
|
435
|
+ private static boolean createOrExistsDir(File file) {
|
|
|
436
|
+ return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
|
|
|
437
|
+ }
|
|
|
438
|
+
|
|
|
439
|
+ private static boolean isSpace(String s) {
|
|
|
440
|
+ if (s == null) return true;
|
|
|
441
|
+ for (int i = 0, len = s.length(); i < len; ++i) {
|
|
|
442
|
+ if (!Character.isWhitespace(s.charAt(i))) {
|
|
|
443
|
+ return false;
|
|
|
444
|
+ }
|
|
|
445
|
+ }
|
|
|
446
|
+ return true;
|
|
|
447
|
+ }
|
|
|
448
|
+} |
|
|
\ No newline at end of file |
...
|
...
|
|