@@ -102,7 +102,7 @@ public final class LogUtils {
102
102
private static final String PLACEHOLDER = " " ;
103
103
private static final Config CONFIG = new Config ();
104
104
105
- private static final ThreadLocal < SimpleDateFormat > SDF_THREAD_LOCAL = new ThreadLocal <>() ;
105
+ private static SimpleDateFormat simpleDateFormat ;
106
106
107
107
private static final ExecutorService EXECUTOR = Executors .newSingleThreadExecutor ();
108
108
@@ -214,7 +214,7 @@ public static void xml(@TYPE final int type, final String tag, final String cont
214
214
215
215
public static void log (final int type , final String tag , final Object ... contents ) {
216
216
if (!CONFIG .isLogSwitch ()) return ;
217
- int type_low = type & 0x0f , type_high = type & 0xf0 ;
217
+ final int type_low = type & 0x0f , type_high = type & 0xf0 ;
218
218
if (CONFIG .isLog2ConsoleSwitch () || CONFIG .isLog2FileSwitch () || type_high == FILE ) {
219
219
if (type_low < CONFIG .mConsoleFilter && type_low < CONFIG .mFileFilter ) return ;
220
220
final TagHead tagHead = processTagAndHead (tag );
@@ -223,7 +223,12 @@ public static void log(final int type, final String tag, final Object... content
223
223
print2Console (type_low , tagHead .tag , tagHead .consoleHead , body );
224
224
}
225
225
if ((CONFIG .isLog2FileSwitch () || type_high == FILE ) && type_low >= CONFIG .mFileFilter ) {
226
- print2File (type_low , tagHead .tag , tagHead .fileHead + body );
226
+ EXECUTOR .execute (new Runnable () {
227
+ @ Override
228
+ public void run () {
229
+ print2File (type_low , tagHead .tag , tagHead .fileHead + body );
230
+ }
231
+ });
227
232
}
228
233
}
229
234
}
@@ -471,45 +476,40 @@ private static void printSingleTagMsg(final int type, final String tag, final St
471
476
}
472
477
473
478
private static void print2File (final int type , final String tag , final String msg ) {
474
- Date now = new Date (System .currentTimeMillis ());
475
- String format = getSdf ().format (now );
479
+ String format = getSdf ().format (new Date ());
476
480
String date = format .substring (0 , 10 );
477
481
String time = format .substring (11 );
478
482
final String fullPath =
479
- CONFIG .getDir () + CONFIG .getFilePrefix () + "- " + date + "- " + CONFIG .getProcessName () + ".txt" ;
480
- if (!createOrExistsFile (fullPath )) {
483
+ CONFIG .getDir () + CONFIG .getFilePrefix () + "_ " + date + "_ " + CONFIG .getProcessName () + ".txt" ;
484
+ if (!createOrExistsFile (fullPath , date )) {
481
485
Log .e ("LogUtils" , "create " + fullPath + " failed!" );
482
486
return ;
483
487
}
484
- StringBuilder sb = new StringBuilder ();
485
- sb .append (time )
486
- .append (T [type - V ])
487
- .append ("/" )
488
- .append (tag )
489
- .append (msg )
490
- .append (LINE_SEP );
491
- final String content = sb .toString ();
488
+ final String content = time +
489
+ T [type - V ] +
490
+ "/" +
491
+ tag +
492
+ msg +
493
+ LINE_SEP ;
492
494
input2File (content , fullPath );
493
495
}
494
496
495
497
private static SimpleDateFormat getSdf () {
496
- SimpleDateFormat simpleDateFormat = SDF_THREAD_LOCAL .get ();
497
498
if (simpleDateFormat == null ) {
498
- simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" , Locale .getDefault ());
499
- SDF_THREAD_LOCAL .set (simpleDateFormat );
499
+ simpleDateFormat = new SimpleDateFormat ("yyyy_MM_dd HH:mm:ss" , Locale .getDefault ());
500
500
}
501
501
return simpleDateFormat ;
502
502
}
503
503
504
- private static boolean createOrExistsFile (final String filePath ) {
504
+ private static boolean createOrExistsFile (final String filePath , final String date ) {
505
505
File file = new File (filePath );
506
506
if (file .exists ()) return file .isFile ();
507
507
if (!createOrExistsDir (file .getParentFile ())) return false ;
508
508
try {
509
- deleteDueLogs (filePath );
509
+ deleteDueLogs (filePath , date );
510
510
boolean isCreate = file .createNewFile ();
511
511
if (isCreate ) {
512
- printDeviceInfo (filePath );
512
+ printDeviceInfo (filePath , date );
513
513
}
514
514
return isCreate ;
515
515
} catch (IOException e ) {
@@ -518,22 +518,20 @@ private static boolean createOrExistsFile(final String filePath) {
518
518
}
519
519
}
520
520
521
- private static void deleteDueLogs (String filePath ) {
521
+ private static void deleteDueLogs (final String filePath , final String date ) {
522
522
if (CONFIG .getSaveDays () <= 0 ) return ;
523
523
File file = new File (filePath );
524
524
File parentFile = file .getParentFile ();
525
525
File [] files = parentFile .listFiles (new FilenameFilter () {
526
526
@ Override
527
527
public boolean accept (File dir , String name ) {
528
- return name .matches ("^" + CONFIG .getFilePrefix () + "- [0-9]{4}- [0-9]{2}- [0-9]{2}-.* \\ .txt $" );
528
+ return name .matches ("^" + CONFIG .getFilePrefix () + "_ [0-9]{4}_ [0-9]{2}_ [0-9]{2}_.* $" );
529
529
}
530
530
});
531
531
if (files == null || files .length <= 0 ) return ;
532
- final int length = filePath .length ();
533
- final SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd" , Locale .getDefault ());
532
+ final SimpleDateFormat sdf = new SimpleDateFormat ("yyyy_MM_dd" , Locale .getDefault ());
534
533
try {
535
- String curDay = findDate (filePath );
536
- long dueMillis = sdf .parse (curDay ).getTime () - CONFIG .getSaveDays () * 86400000L ;
534
+ long dueMillis = sdf .parse (date ).getTime () - CONFIG .getSaveDays () * 86400000L ;
537
535
for (final File aFile : files ) {
538
536
String name = aFile .getName ();
539
537
int l = name .length ();
@@ -556,15 +554,15 @@ public void run() {
556
554
}
557
555
558
556
private static String findDate (String str ) {
559
- Pattern pattern = Pattern .compile ("[0-9]{4}- [0-9]{2}- [0-9]{2}" );
557
+ Pattern pattern = Pattern .compile ("[0-9]{4}_ [0-9]{2}_ [0-9]{2}" );
560
558
Matcher matcher = pattern .matcher (str );
561
559
if (matcher .find ()) {
562
560
return matcher .group ();
563
561
}
564
562
return "" ;
565
563
}
566
564
567
- private static void printDeviceInfo (final String filePath ) {
565
+ private static void printDeviceInfo (final String filePath , final String date ) {
568
566
String versionName = "" ;
569
567
int versionCode = 0 ;
570
568
try {
@@ -578,9 +576,8 @@ private static void printDeviceInfo(final String filePath) {
578
576
} catch (PackageManager .NameNotFoundException e ) {
579
577
e .printStackTrace ();
580
578
}
581
- String time = filePath .substring (filePath .length () - 14 , filePath .length () - 4 );
582
579
final String head = "************* Log Head ****************" +
583
- "\n Date of Log : " + time +
580
+ "\n Date of Log : " + date +
584
581
"\n Device Manufacturer: " + Build .MANUFACTURER +
585
582
"\n Device Model : " + Build .MODEL +
586
583
"\n Android Version : " + Build .VERSION .RELEASE +
@@ -607,27 +604,22 @@ private static boolean isSpace(final String s) {
607
604
608
605
private static void input2File (final String input , final String filePath ) {
609
606
if (CONFIG .mFileWriter == null ) {
610
- EXECUTOR .execute (new Runnable () {
611
- @ Override
612
- public void run () {
613
- BufferedWriter bw = null ;
614
- try {
615
- bw = new BufferedWriter (new FileWriter (filePath , true ));
616
- bw .write (input );
617
- } catch (IOException e ) {
618
- e .printStackTrace ();
619
- Log .e ("LogUtils" , "log to " + filePath + " failed!" );
620
- } finally {
621
- try {
622
- if (bw != null ) {
623
- bw .close ();
624
- }
625
- } catch (IOException e ) {
626
- e .printStackTrace ();
627
- }
607
+ BufferedWriter bw = null ;
608
+ try {
609
+ bw = new BufferedWriter (new FileWriter (filePath , true ));
610
+ bw .write (input );
611
+ } catch (IOException e ) {
612
+ e .printStackTrace ();
613
+ Log .e ("LogUtils" , "log to " + filePath + " failed!" );
614
+ } finally {
615
+ try {
616
+ if (bw != null ) {
617
+ bw .close ();
628
618
}
619
+ } catch (IOException e ) {
620
+ e .printStackTrace ();
629
621
}
630
- });
622
+ }
631
623
} else {
632
624
CONFIG .mFileWriter .write (filePath , input );
633
625
}
0 commit comments