2
2
3
3
import android .os .Environment ;
4
4
import android .support .annotation .IntDef ;
5
+ import android .support .annotation .NonNull ;
5
6
import android .util .Log ;
6
7
7
8
import org .json .JSONArray ;
16
17
import java .io .StringWriter ;
17
18
import java .lang .annotation .Retention ;
18
19
import java .lang .annotation .RetentionPolicy ;
20
+ import java .text .Format ;
19
21
import java .text .SimpleDateFormat ;
20
22
import java .util .Date ;
21
23
import java .util .Formatter ;
@@ -67,15 +69,17 @@ public final class LogUtils {
67
69
private static boolean sLogBorderSwitch = true ; // log边框开关,默认开
68
70
private static int sLogFilter = V ; // log过滤器
69
71
70
- private static final String TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
71
- private static final String LEFT_BORDER = "║ " ;
72
- private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
73
- private static final String LINE_SEPARATOR = System .getProperty ("line.separator" );
74
- private static final int MAX_LEN = 4000 ;
72
+ private static final String FILE_SEP = System .getProperty ("file.separator" );
73
+ private static final String LINE_SEP = System .getProperty ("line.separator" );
74
+ private static final String TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
75
+ private static final String LEFT_BORDER = "║ " ;
76
+ private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
77
+ private static final int MAX_LEN = 4000 ;
75
78
76
79
private static final String NULL_TIPS = "Log with null object." ;
77
80
private static final String NULL = "null" ;
78
81
private static final String ARGS = "args" ;
82
+ private static final Format FORMAT = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ());
79
83
80
84
private LogUtils () {
81
85
throw new UnsupportedOperationException ("u can't instantiate me..." );
@@ -86,12 +90,22 @@ public static class Builder {
86
90
public Builder () {
87
91
if (Environment .MEDIA_MOUNTED .equals (Environment .getExternalStorageState ())
88
92
&& Utils .getContext ().getExternalCacheDir () != null )
89
- dir = Utils .getContext ().getExternalCacheDir () + File . separator + "log" + File . separator ;
93
+ dir = Utils .getContext ().getExternalCacheDir () + FILE_SEP + "log" + FILE_SEP ;
90
94
else {
91
- dir = Utils .getContext ().getCacheDir () + File . separator + "log" + File . separator ;
95
+ dir = Utils .getContext ().getCacheDir () + FILE_SEP + "log" + FILE_SEP ;
92
96
}
93
97
}
94
98
99
+ public Builder (@ NonNull String dir ) {
100
+ LogUtils .dir = dir ;
101
+ if (!dir .endsWith (FILE_SEP ))
102
+ LogUtils .dir += FILE_SEP ;
103
+ }
104
+
105
+ public Builder (@ NonNull File dir ) {
106
+ LogUtils .dir = dir .getAbsolutePath () + FILE_SEP ;
107
+ }
108
+
95
109
public Builder setLogSwitch (boolean logSwitch ) {
96
110
LogUtils .sLogSwitch = logSwitch ;
97
111
return this ;
@@ -249,7 +263,7 @@ private static String[] processContents(int type, String tag, Object... contents
249
263
}
250
264
String head = sLogHeadSwitch
251
265
? new Formatter ()
252
- .format ("Thread: %s, %s(%s.java:%d)" + LINE_SEPARATOR ,
266
+ .format ("Thread: %s, %s(%s.java:%d)" + LINE_SEP ,
253
267
Thread .currentThread ().getName (),
254
268
targetElement .getMethodName (),
255
269
className ,
@@ -275,17 +289,17 @@ private static String[] processContents(int type, String tag, Object... contents
275
289
.append ("]" )
276
290
.append (" = " )
277
291
.append (content == null ? NULL : content .toString ())
278
- .append (LINE_SEPARATOR );
292
+ .append (LINE_SEP );
279
293
}
280
294
body = sb .toString ();
281
295
}
282
296
}
283
297
String msg = head + body ;
284
298
if (sLogBorderSwitch ) {
285
299
StringBuilder sb = new StringBuilder ();
286
- String [] lines = msg .split (LINE_SEPARATOR );
300
+ String [] lines = msg .split (LINE_SEP );
287
301
for (String line : lines ) {
288
- sb .append (LEFT_BORDER ).append (line ).append (LINE_SEPARATOR );
302
+ sb .append (LEFT_BORDER ).append (line ).append (LINE_SEP );
289
303
}
290
304
msg = sb .toString ();
291
305
}
@@ -313,7 +327,7 @@ private static String formatXml(String xml) {
313
327
transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
314
328
transformer .setOutputProperty ("{http://xml.apache.org/xslt}indent-amount" , "4" );
315
329
transformer .transform (xmlInput , xmlOutput );
316
- xml = xmlOutput .getWriter ().toString ().replaceFirst (">" , ">" + LINE_SEPARATOR );
330
+ xml = xmlOutput .getWriter ().toString ().replaceFirst (">" , ">" + LINE_SEP );
317
331
} catch (Exception e ) {
318
332
e .printStackTrace ();
319
333
}
@@ -365,31 +379,32 @@ private static void print(final int type, final String tag, String msg) {
365
379
}
366
380
367
381
private static void print2File (final String tag , final String msg ) {
368
- Date now = new Date ();
369
- String date = new SimpleDateFormat ("MM-dd" , Locale .getDefault ()).format (now );
382
+ Date now = new Date (System .currentTimeMillis ());
383
+ String format = FORMAT .format (now );
384
+ String date = format .substring (0 , 5 );
385
+ String time = format .substring (6 );
370
386
final String fullPath = dir + date + ".txt" ;
371
387
if (!createOrExistsFile (fullPath )) {
372
388
Log .e (tag , "log to " + fullPath + " failed!" );
373
389
return ;
374
390
}
375
- String time = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ()).format (now );
376
391
StringBuilder sb = new StringBuilder ();
377
392
if (sLogBorderSwitch ) {
378
- sb .append (TOP_BORDER ).append (LINE_SEPARATOR );
393
+ sb .append (TOP_BORDER ).append (LINE_SEP );
379
394
sb .append (LEFT_BORDER )
380
395
.append (time )
381
396
.append (tag )
382
- .append (LINE_SEPARATOR )
397
+ .append (LINE_SEP )
383
398
.append (msg );
384
- sb .append (BOTTOM_BORDER ).append (LINE_SEPARATOR );
399
+ sb .append (BOTTOM_BORDER ).append (LINE_SEP );
385
400
} else {
386
401
sb .append (time )
387
402
.append (tag )
388
- .append (LINE_SEPARATOR )
403
+ .append (LINE_SEP )
389
404
.append (msg )
390
- .append (LINE_SEPARATOR );
405
+ .append (LINE_SEP );
391
406
}
392
- sb .append (LINE_SEPARATOR );
407
+ sb .append (LINE_SEP );
393
408
final String dateLogContent = sb .toString ();
394
409
if (executor == null ) {
395
410
executor = Executors .newSingleThreadExecutor ();
0 commit comments