9
9
import org .json .JSONObject ;
10
10
11
11
import java .io .BufferedWriter ;
12
- import java .io .ByteArrayOutputStream ;
13
12
import java .io .File ;
14
13
import java .io .FileWriter ;
15
14
import java .io .IOException ;
24
23
import java .util .Locale ;
25
24
import java .util .concurrent .ExecutorService ;
26
25
import java .util .concurrent .Executors ;
27
- import java .util .zip .DataFormatException ;
28
- import java .util .zip .Deflater ;
29
- import java .util .zip .Inflater ;
30
26
31
27
import javax .xml .transform .OutputKeys ;
32
28
import javax .xml .transform .Source ;
@@ -62,19 +58,21 @@ public final class LogUtils {
62
58
private static final int FILE = 0x10 ;
63
59
private static final int JSON = 0x20 ;
64
60
private static final int XML = 0x30 ;
65
- private static ExecutorService executor ;
66
- private static String defaultDir ;// log默认存储目录
67
- private static String dir ; // log存储目录
68
-
69
- private static boolean sLogSwitch = true ; // log总开关,默认开
70
- private static boolean sLog2ConsoleSwitch = true ; // logcat是否打印,默认打印
71
- private static String sGlobalTag = null ; // log标签
72
- private static boolean sTagIsSpace = true ; // log标签是否为空白
73
- private static boolean sLogHeadSwitch = true ; // log头部开关,默认开
74
- private static boolean sLog2FileSwitch = false ;// log写入文件开关,默认关
75
- private static boolean sLogBorderSwitch = true ; // log边框开关,默认开
76
- private static int sConsoleFilter = V ; // log控制台过滤器
77
- private static int sFileFilter = V ; // log文件过滤器
61
+
62
+ private static ExecutorService sExecutor ;
63
+ private static String sDefaultDir ;// log默认存储目录
64
+ private static String sDir ; // log存储目录
65
+ private static String sFilePrefix = "util" ;// log文件前缀
66
+ private static boolean sLogSwitch = true ; // log总开关,默认开
67
+ private static boolean sLog2ConsoleSwitch = true ; // logcat是否打印,默认打印
68
+ private static String sGlobalTag = null ; // log标签
69
+ private static boolean sTagIsSpace = true ; // log标签是否为空白
70
+ private static boolean sLogHeadSwitch = true ; // log头部开关,默认开
71
+ private static boolean sLog2FileSwitch = false ; // log写入文件开关,默认关
72
+ private static boolean sLogBorderSwitch = true ; // log边框开关,默认开
73
+ private static int sConsoleFilter = V ; // log控制台过滤器
74
+ private static int sFileFilter = V ; // log文件过滤器
75
+ public static int sStackDeep = 1 ; // log栈深度
78
76
79
77
private static final Config CONFIG = new Config ();
80
78
private static final String FILE_SEP = System .getProperty ("file.separator" );
@@ -84,10 +82,9 @@ public final class LogUtils {
84
82
private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
85
83
private static final int MAX_LEN = 4000 ;
86
84
private static final Format FORMAT = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ());
87
-
88
- private static final String NULL_TIPS = "Log with null object." ;
89
- private static final String NULL = "null" ;
90
- private static final String ARGS = "args" ;
85
+ private static final String NULL_TIPS = "Log with null object." ;
86
+ private static final String NULL = "null" ;
87
+ private static final String ARGS = "args" ;
91
88
92
89
private LogUtils () {
93
90
throw new UnsupportedOperationException ("u can't instantiate me..." );
@@ -214,9 +211,7 @@ private static String[] processTagAndHead(String tag) {
214
211
StackTraceElement targetElement = new Throwable ().getStackTrace ()[3 ];
215
212
String fileName = targetElement .getFileName ();
216
213
String className = fileName .substring (0 , fileName .indexOf ('.' ));
217
- if (sTagIsSpace ) {
218
- tag = isSpace (tag ) ? className : tag ;
219
- }
214
+ if (sTagIsSpace ) tag = isSpace (tag ) ? className : tag ;
220
215
if (sLogHeadSwitch ) {
221
216
String head = new Formatter ()
222
217
.format ("%s, %s(%s:%d)" ,
@@ -331,7 +326,7 @@ private static void print2File(final int type, final String tag, final String ms
331
326
String format = FORMAT .format (now );
332
327
String date = format .substring (0 , 5 );
333
328
String time = format .substring (6 );
334
- final String fullPath = (dir == null ? defaultDir : dir ) + date + ".txt" ;
329
+ final String fullPath = (sDir == null ? sDefaultDir : sDir ) + sFilePrefix + "-" + date + ".txt" ;
335
330
if (!createOrExistsFile (fullPath )) {
336
331
Log .e (tag , "log to " + fullPath + " failed!" );
337
332
return ;
@@ -344,10 +339,10 @@ private static void print2File(final int type, final String tag, final String ms
344
339
.append (msg )
345
340
.append (LINE_SEP );
346
341
final String content = sb .toString ();
347
- if (executor == null ) {
348
- executor = Executors .newSingleThreadExecutor ();
342
+ if (sExecutor == null ) {
343
+ sExecutor = Executors .newSingleThreadExecutor ();
349
344
}
350
- executor .execute (new Runnable () {
345
+ sExecutor .execute (new Runnable () {
351
346
@ Override
352
347
public void run () {
353
348
BufferedWriter bw = null ;
@@ -397,113 +392,83 @@ private static boolean isSpace(final String s) {
397
392
return true ;
398
393
}
399
394
400
- public static byte [] compress (final byte input []) {
401
- ByteArrayOutputStream bos = new ByteArrayOutputStream ();
402
- Deflater compressor = new Deflater (1 );
403
- try {
404
- compressor .setInput (input );
405
- compressor .finish ();
406
- final byte [] buf = new byte [2048 ];
407
- while (!compressor .finished ()) {
408
- int count = compressor .deflate (buf );
409
- bos .write (buf , 0 , count );
410
- }
411
- } finally {
412
- compressor .end ();
413
- }
414
- return bos .toByteArray ();
415
- }
416
-
417
- public static byte [] uncompress (final byte [] input ) {
418
- ByteArrayOutputStream bos = new ByteArrayOutputStream ();
419
- Inflater decompressor = new Inflater ();
420
- try {
421
- decompressor .setInput (input );
422
- final byte [] buf = new byte [2048 ];
423
- while (!decompressor .finished ()) {
424
- int count = 0 ;
425
- try {
426
- count = decompressor .inflate (buf );
427
- } catch (DataFormatException e ) {
428
- e .printStackTrace ();
429
- }
430
- bos .write (buf , 0 , count );
431
- }
432
- } finally {
433
- decompressor .end ();
434
- }
435
- return bos .toByteArray ();
436
- }
437
-
438
-
439
395
public static class Config {
440
396
private Config () {
441
- if (defaultDir != null ) return ;
397
+ if (sDefaultDir != null ) return ;
442
398
if (Environment .MEDIA_MOUNTED .equals (Environment .getExternalStorageState ())
443
399
&& Utils .getApp ().getExternalCacheDir () != null )
444
- defaultDir = Utils .getApp ().getExternalCacheDir () + FILE_SEP + "log" + FILE_SEP ;
400
+ sDefaultDir = Utils .getApp ().getExternalCacheDir () + FILE_SEP + "log" + FILE_SEP ;
445
401
else {
446
- defaultDir = Utils .getApp ().getCacheDir () + FILE_SEP + "log" + FILE_SEP ;
402
+ sDefaultDir = Utils .getApp ().getCacheDir () + FILE_SEP + "log" + FILE_SEP ;
447
403
}
448
404
}
449
405
450
406
public Config setLogSwitch (final boolean logSwitch ) {
451
- LogUtils . sLogSwitch = logSwitch ;
407
+ sLogSwitch = logSwitch ;
452
408
return this ;
453
409
}
454
410
455
411
public Config setConsoleSwitch (final boolean consoleSwitch ) {
456
- LogUtils . sLog2ConsoleSwitch = consoleSwitch ;
412
+ sLog2ConsoleSwitch = consoleSwitch ;
457
413
return this ;
458
414
}
459
415
460
416
public Config setGlobalTag (final String tag ) {
461
417
if (isSpace (tag )) {
462
- LogUtils . sGlobalTag = "" ;
418
+ sGlobalTag = "" ;
463
419
sTagIsSpace = true ;
464
420
} else {
465
- LogUtils . sGlobalTag = tag ;
421
+ sGlobalTag = tag ;
466
422
sTagIsSpace = false ;
467
423
}
468
424
return this ;
469
425
}
470
426
471
427
public Config setLogHeadSwitch (final boolean logHeadSwitch ) {
472
- LogUtils . sLogHeadSwitch = logHeadSwitch ;
428
+ sLogHeadSwitch = logHeadSwitch ;
473
429
return this ;
474
430
}
475
431
476
432
public Config setLog2FileSwitch (final boolean log2FileSwitch ) {
477
- LogUtils . sLog2FileSwitch = log2FileSwitch ;
433
+ sLog2FileSwitch = log2FileSwitch ;
478
434
return this ;
479
435
}
480
436
481
437
public Config setDir (final String dir ) {
482
438
if (isSpace (dir )) {
483
- LogUtils . dir = null ;
439
+ sDir = null ;
484
440
} else {
485
- LogUtils . dir = dir .endsWith (FILE_SEP ) ? dir : dir + FILE_SEP ;
441
+ sDir = dir .endsWith (FILE_SEP ) ? dir : dir + FILE_SEP ;
486
442
}
487
443
return this ;
488
444
}
489
445
490
446
public Config setDir (final File dir ) {
491
- LogUtils .dir = dir == null ? null : dir .getAbsolutePath () + FILE_SEP ;
447
+ sDir = dir == null ? null : dir .getAbsolutePath () + FILE_SEP ;
448
+ return this ;
449
+ }
450
+
451
+ public Config setFilePrefix (final String filePrefix ) {
452
+ if (isSpace (filePrefix )) {
453
+ sFilePrefix = "util" ;
454
+ } else {
455
+ sFilePrefix = filePrefix ;
456
+ }
492
457
return this ;
493
458
}
494
459
495
460
public Config setBorderSwitch (final boolean borderSwitch ) {
496
- LogUtils . sLogBorderSwitch = borderSwitch ;
461
+ sLogBorderSwitch = borderSwitch ;
497
462
return this ;
498
463
}
499
464
500
465
public Config setConsoleFilter (@ TYPE final int consoleFilter ) {
501
- LogUtils . sConsoleFilter = consoleFilter ;
466
+ sConsoleFilter = consoleFilter ;
502
467
return this ;
503
468
}
504
469
505
470
public Config setFileFilter (@ TYPE final int fileFilter ) {
506
- LogUtils . sFileFilter = fileFilter ;
471
+ sFileFilter = fileFilter ;
507
472
return this ;
508
473
}
509
474
@@ -514,7 +479,8 @@ public String toString() {
514
479
+ LINE_SEP + "tag: " + (sTagIsSpace ? "null" : sGlobalTag )
515
480
+ LINE_SEP + "head: " + sLogHeadSwitch
516
481
+ LINE_SEP + "file: " + sLog2FileSwitch
517
- + LINE_SEP + "dir: " + (dir == null ? defaultDir : dir )
482
+ + LINE_SEP + "dir: " + (sDir == null ? sDefaultDir : sDir )
483
+ + LINE_SEP + "filePrefix" + sFilePrefix
518
484
+ LINE_SEP + "border: " + sLogBorderSwitch
519
485
+ LINE_SEP + "consoleFilter: " + T [sConsoleFilter - V ]
520
486
+ LINE_SEP + "fileFilter: " + T [sFileFilter - V ];
0 commit comments