Skip to content

Commit f301618

Browse files
committed
see 04/10 log
1 parent 50173f2 commit f301618

File tree

2 files changed

+126
-115
lines changed

2 files changed

+126
-115
lines changed

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

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.os.Environment;
88
import android.support.annotation.NonNull;
99
import android.support.annotation.RequiresPermission;
10+
import android.util.Log;
1011

1112
import java.io.BufferedWriter;
1213
import java.io.File;
@@ -18,8 +19,10 @@
1819
import java.text.Format;
1920
import java.text.SimpleDateFormat;
2021
import java.util.Date;
21-
import java.util.concurrent.ExecutorService;
22+
import java.util.concurrent.Callable;
23+
import java.util.concurrent.ExecutionException;
2224
import java.util.concurrent.Executors;
25+
import java.util.concurrent.Future;
2326

2427
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
2528

@@ -38,8 +41,6 @@ public final class CrashUtils {
3841
private static String versionName;
3942
private static int versionCode;
4043

41-
private static ExecutorService sExecutor;
42-
4344
private static final String FILE_SEP = System.getProperty("file.separator");
4445
@SuppressLint("SimpleDateFormat")
4546
private static final Format FORMAT = new SimpleDateFormat("MM-dd HH-mm-ss");
@@ -105,30 +106,11 @@ public void uncaughtException(final Thread t, final Throwable e) {
105106
Date now = new Date(System.currentTimeMillis());
106107
String fileName = FORMAT.format(now) + ".txt";
107108
final String fullPath = (dir == null ? defaultDir : dir) + fileName;
108-
if (!createOrExistsFile(fullPath)) return;
109-
if (sExecutor == null) {
110-
sExecutor = Executors.newSingleThreadExecutor();
109+
if (createOrExistsFile(fullPath)) {
110+
input2File(crashInfo, fullPath);
111+
} else {
112+
Log.e("CrashUtils", "create " + fullPath + " failed!");
111113
}
112-
sExecutor.execute(new Runnable() {
113-
@Override
114-
public void run() {
115-
BufferedWriter bw = null;
116-
try {
117-
bw = new BufferedWriter(new FileWriter(fullPath, false));
118-
bw.write(crashInfo);
119-
} catch (IOException e) {
120-
e.printStackTrace();
121-
} finally {
122-
if (bw != null) {
123-
try {
124-
bw.close();
125-
} catch (IOException e1) {
126-
e1.printStackTrace();
127-
}
128-
}
129-
}
130-
}
131-
});
132114

133115
if (sOnCrashListener != null) {
134116
sOnCrashListener.onCrash(crashInfo, e);
@@ -229,6 +211,39 @@ public static void init(final String crashDirPath, final OnCrashListener onCrash
229211
Thread.setDefaultUncaughtExceptionHandler(UNCAUGHT_EXCEPTION_HANDLER);
230212
}
231213

214+
private static void input2File(final String input, final String filePath) {
215+
Future<Boolean> submit = Executors.newSingleThreadExecutor().submit(new Callable<Boolean>() {
216+
@Override
217+
public Boolean call() throws Exception {
218+
BufferedWriter bw = null;
219+
try {
220+
bw = new BufferedWriter(new FileWriter(filePath, true));
221+
bw.write(input);
222+
return true;
223+
} catch (IOException e) {
224+
e.printStackTrace();
225+
return false;
226+
} finally {
227+
try {
228+
if (bw != null) {
229+
bw.close();
230+
}
231+
} catch (IOException e) {
232+
e.printStackTrace();
233+
}
234+
}
235+
}
236+
});
237+
try {
238+
if (submit.get()) return;
239+
} catch (InterruptedException e) {
240+
e.printStackTrace();
241+
} catch (ExecutionException e) {
242+
e.printStackTrace();
243+
}
244+
Log.e("CrashUtils", "write crash info to " + filePath + " failed!");
245+
}
246+
232247
private static boolean createOrExistsFile(final String filePath) {
233248
File file = new File(filePath);
234249
if (file.exists()) return file.isFile();

0 commit comments

Comments
 (0)