Skip to content

Commit 8ccb3b8

Browse files
author
Blankj
committed
see 11/10 log
1 parent f90d93b commit 8ccb3b8

3 files changed

Lines changed: 46 additions & 49 deletions

File tree

update_log.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* 17/11/10 LogUtils新增日志头部,感谢Kanade
12
* 17/11/07 完善LogUtils无tag的多参数
23
* 17/11/06 修复LogUtils多参数打印失败的问题
34
* 17/11/01 完善ShellUtil的Msg换行,感谢香脆的大鸡排

utilcode/src/main/java/com/blankj/utilcode/util/CrashUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class CrashUtils {
5454
e.printStackTrace();
5555
}
5656

57-
CRASH_HEAD = "\n************* Crash Log Head ****************" +
57+
CRASH_HEAD = "************* Crash Log Head ****************" +
5858
"\nDevice Manufacturer: " + Build.MANUFACTURER +// 设备厂商
5959
"\nDevice Model : " + Build.MODEL +// 设备型号
6060
"\nAndroid Version : " + Build.VERSION.RELEASE +// 系统版本

utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
import java.util.Date;
2626
import java.util.Formatter;
2727
import java.util.Locale;
28+
import java.util.concurrent.Callable;
29+
import java.util.concurrent.ExecutionException;
2830
import java.util.concurrent.ExecutorService;
2931
import java.util.concurrent.Executors;
32+
import java.util.concurrent.Future;
3033

3134
import javax.xml.transform.OutputKeys;
3235
import 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
"\nDevice Manufacturer: " + Build.MANUFACTURER +// 设备厂商
447433
"\nDevice Model : " + Build.MODEL +// 设备型号
448434
"\nAndroid Version : " + Build.VERSION.RELEASE +// 系统版本
449435
"\nAndroid SDK : " + Build.VERSION.SDK_INT +// SDK版本
450436
"\nApp VersionName : " + versionName +
451437
"\nApp 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

Comments
 (0)