2525import java .util .Date ;
2626import java .util .Formatter ;
2727import java .util .Locale ;
28+ import java .util .concurrent .Callable ;
29+ import java .util .concurrent .ExecutionException ;
2830import java .util .concurrent .ExecutorService ;
2931import java .util .concurrent .Executors ;
32+ import java .util .concurrent .Future ;
3033
3134import javax .xml .transform .OutputKeys ;
3235import javax .xml .transform .Source ;
@@ -392,38 +395,21 @@ private static void print2File(final int type, final String tag, final String ms
392395 .append (msg )
393396 .append (LINE_SEP );
394397 final String content = sb .toString ();
395- execute (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 (content );
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- });
398+ if (input2File (content , fullPath )) {
399+ Log .d (tag , "log to " + fullPath + " success!" );
400+ } else {
401+ Log .e (tag , "log to " + fullPath + " failed!" );
402+ }
417403 }
418404
419405 private static boolean createOrExistsFile (final String filePath ) {
420406 File file = new File (filePath );
421407 if (file .exists ()) return file .isFile ();
422408 if (!createOrExistsDir (file .getParentFile ())) return false ;
423409 try {
424- boolean r = file .createNewFile ();
425- printDeviceInfo (filePath );
426- return r ;
410+ boolean isCreate = file .createNewFile ();
411+ if ( isCreate ) printDeviceInfo (filePath );
412+ return isCreate ;
427413 } catch (IOException e ) {
428414 e .printStackTrace ();
429415 return false ;
@@ -442,34 +428,15 @@ private static void printDeviceInfo(final String filePath) {
442428 } catch (PackageManager .NameNotFoundException e ) {
443429 e .printStackTrace ();
444430 }
445- final String head = "\n ************* Log Head ****************" +
431+ final String head = "************* Log Head ****************" +
446432 "\n Device Manufacturer: " + Build .MANUFACTURER +// 设备厂商
447433 "\n Device Model : " + Build .MODEL +// 设备型号
448434 "\n Android Version : " + Build .VERSION .RELEASE +// 系统版本
449435 "\n Android SDK : " + Build .VERSION .SDK_INT +// SDK版本
450436 "\n App VersionName : " + versionName +
451437 "\n App VersionCode : " + versionCode +
452438 "\n ************* Log Head ****************\n \n " ;
453- execute (new Runnable () {
454- @ Override
455- public void run () {
456- BufferedWriter bw = null ;
457- try {
458- bw = new BufferedWriter (new FileWriter (filePath , true ));
459- bw .write (head );
460- } catch (IOException e ) {
461- e .printStackTrace ();
462- } finally {
463- try {
464- if (bw != null ) {
465- bw .close ();
466- }
467- } catch (IOException e ) {
468- e .printStackTrace ();
469- }
470- }
471- }
472- });
439+ input2File (head , filePath );
473440 }
474441
475442 private static boolean createOrExistsDir (final File file ) {
@@ -486,11 +453,40 @@ private static boolean isSpace(final String s) {
486453 return true ;
487454 }
488455
489- private static void execute ( Runnable runnable ) {
456+ private static boolean input2File ( final String input , final String filePath ) {
490457 if (sExecutor == null ) {
491458 sExecutor = Executors .newSingleThreadExecutor ();
492459 }
493- sExecutor .execute (runnable );
460+ Future <Boolean > submit = sExecutor .submit (new Callable <Boolean >() {
461+ @ Override
462+ public Boolean call () throws Exception {
463+ BufferedWriter bw = null ;
464+ try {
465+ bw = new BufferedWriter (new FileWriter (filePath , true ));
466+ bw .write (input );
467+ return true ;
468+ } catch (IOException e ) {
469+ e .printStackTrace ();
470+ return false ;
471+ } finally {
472+ try {
473+ if (bw != null ) {
474+ bw .close ();
475+ }
476+ } catch (IOException e ) {
477+ e .printStackTrace ();
478+ }
479+ }
480+ }
481+ });
482+ try {
483+ return submit .get ();
484+ } catch (InterruptedException e ) {
485+ e .printStackTrace ();
486+ } catch (ExecutionException e ) {
487+ e .printStackTrace ();
488+ }
489+ return false ;
494490 }
495491
496492 public static class Config {
0 commit comments