Skip to content

Commit 2d448fa

Browse files
committed
android Crash 保存
1 parent 9568a0d commit 2d448fa

File tree

3 files changed

+204
-67
lines changed

3 files changed

+204
-67
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.netease.im;
2+
3+
import com.facebook.react.bridge.Promise;
4+
import com.facebook.react.bridge.ReactApplicationContext;
5+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
6+
import com.facebook.react.bridge.ReactMethod;
7+
import com.netease.im.uikit.common.util.storage.StorageType;
8+
import com.netease.im.uikit.common.util.storage.StorageUtil;
9+
10+
import org.json.JSONException;
11+
import org.json.JSONObject;
12+
13+
import java.io.BufferedReader;
14+
import java.io.File;
15+
import java.io.FileNotFoundException;
16+
import java.io.FileReader;
17+
import java.io.IOException;
18+
19+
/**
20+
* Created by dowin on 2017/11/3.
21+
*/
22+
23+
public class RNCrashModule extends ReactContextBaseJavaModule implements Thread.UncaughtExceptionHandler {
24+
25+
private final static String TAG = "CrashHandler";
26+
private final static String NAME = "CrashHandler";
27+
28+
public RNCrashModule(ReactApplicationContext reactContext) {
29+
super(reactContext);
30+
}
31+
32+
@Override
33+
public String getName() {
34+
return NAME;
35+
}
36+
37+
// object.put("count",String.valueOf(count));
38+
// object.put("time",timestamp);
39+
// object.put("device",SysInfoUtil.getPhoneModelWithManufacturer());
40+
// object.put("android",SysInfoUtil.getOsInfo());
41+
// object.put("system",Build.DISPLAY);
42+
// object.put("battery",battery());
43+
// object.put("rooted: ", isRooted() ? "yes" : "no");
44+
// object.put("ram",ram());
45+
// object.put("disk",disk());
46+
// object.put("ver", String.format("%d", InstallUtil.getVersionCode(context)));
47+
// object.put("caught", uncaught ? "no" : "yes");
48+
// object.put("network",NetworkUtil.getNetworkInfo(context));
49+
//
50+
// object.put("errorInfo",trace);
51+
@ReactMethod
52+
public void getErrorMessage(Promise promise) {
53+
54+
File mFile = new File(StorageUtil.getWritePath("jsonUpLoad.crashlog", StorageType.TYPE_LOG));
55+
56+
if (!mFile.exists()) {
57+
return;
58+
}
59+
try {
60+
BufferedReader bufferedReader = new BufferedReader(new FileReader(mFile));
61+
StringBuffer s = new StringBuffer();
62+
String line;
63+
while ((line = bufferedReader.readLine()) != null) {
64+
s.append(line).append(System.getProperty("line.separator"));
65+
}
66+
JSONObject object = new JSONObject(s.toString());
67+
promise.resolve(object.optString("errorInfo"));
68+
} catch (FileNotFoundException e) {
69+
e.printStackTrace();
70+
} catch (IOException e) {
71+
e.printStackTrace();
72+
} catch (JSONException e) {
73+
e.printStackTrace();
74+
}
75+
}
76+
77+
@ReactMethod
78+
public void deleteErrorMessage(Promise promise) {
79+
File mFile = new File(StorageUtil.getWritePath("jsonUpLoad.crashlog", StorageType.TYPE_LOG));
80+
if (mFile.exists()) {
81+
mFile.delete();
82+
}
83+
}
84+
85+
@Override
86+
public void uncaughtException(Thread thread, Throwable ex) {
87+
88+
}
89+
}

android/src/main/java/com/netease/im/common/crash/CrashSaver.java

Lines changed: 93 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,66 +21,65 @@
2121

2222
class CrashSaver {
2323

24-
public static final void save(Context context, Throwable ex,
25-
boolean uncaught) {
24+
public static final void save(Context context, Throwable ex, boolean uncaught) {
2625

27-
if (!StorageUtil.isExternalStorageExist()) {// 如果没有sdcard,则不存储
28-
return;
29-
}
30-
Writer writer = null;
31-
PrintWriter printWriter = null;
32-
String stackTrace = "";
33-
try {
34-
writer = new StringWriter();
35-
printWriter = new PrintWriter(writer);
36-
ex.printStackTrace(printWriter);
37-
Throwable cause = ex.getCause();
38-
while (cause != null) {
39-
cause.printStackTrace(printWriter);
40-
cause = cause.getCause();
41-
}
42-
stackTrace = writer.toString();
43-
} catch (Exception e) {
26+
if (!StorageUtil.isExternalStorageExist()) {// 如果没有sdcard,则不存储
27+
return;
28+
}
29+
Writer writer = null;
30+
PrintWriter printWriter = null;
31+
String stackTrace = "";
32+
try {
33+
writer = new StringWriter();
34+
printWriter = new PrintWriter(writer);
35+
ex.printStackTrace(printWriter);
36+
Throwable cause = ex.getCause();
37+
while (cause != null) {
38+
cause.printStackTrace(printWriter);
39+
cause = cause.getCause();
40+
}
41+
stackTrace = writer.toString();
42+
} catch (Exception e) {
4443
e.printStackTrace();
45-
} finally {
46-
if (writer != null) {
47-
try {
48-
writer.close();
49-
} catch (IOException e) {
50-
e.printStackTrace();
51-
}
52-
}
53-
if (printWriter != null) {
54-
printWriter.close();
55-
}
56-
}
44+
} finally {
45+
if (writer != null) {
46+
try {
47+
writer.close();
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
if (printWriter != null) {
53+
printWriter.close();
54+
}
55+
}
5756
String signature = stackTrace.replaceAll("\\([^\\(]*\\)", "");
58-
String filename = MD5.getStringMD5(signature);
59-
if(TextUtils.isEmpty(filename)) {
60-
return;
61-
}
62-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
63-
Date date = new Date();
64-
String timestamp = sdf.format(date);
65-
BufferedWriter mBufferedWriter = null;
66-
try {
67-
File mFile = new File(StorageUtil.getWritePath(
57+
String filename = MD5.getStringMD5(signature);
58+
if (TextUtils.isEmpty(filename)) {
59+
return;
60+
}
61+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
62+
Date date = new Date();
63+
String timestamp = sdf.format(date);
64+
BufferedWriter mBufferedWriter = null;
65+
try {
66+
File mFile = new File(StorageUtil.getWritePath(
6867
filename + ".crashlog", StorageType.TYPE_LOG));
69-
File pFile = mFile.getParentFile();
70-
if (!pFile.exists()) {// 如果文件夹不存在,则先创建文件夹
71-
pFile.mkdirs();
72-
}
73-
int count = 1;
74-
if(mFile.exists()) {
68+
File pFile = mFile.getParentFile();
69+
if (!pFile.exists()) {// 如果文件夹不存在,则先创建文件夹
70+
pFile.mkdirs();
71+
}
72+
int count = 1;
73+
if (mFile.exists()) {
7574
LineNumberReader reader = null;
7675
try {
7776
reader = new LineNumberReader(new FileReader(mFile));
7877
String line = reader.readLine();
79-
if(line.startsWith("count")) {
78+
if (line.startsWith("count")) {
8079
int index = line.indexOf(":");
81-
if(index != -1) {
80+
if (index != -1) {
8281
String count_str = line.substring(++index);
83-
if(count_str != null) {
82+
if (count_str != null) {
8483
count_str = count_str.trim();
8584
count = Integer.parseInt(count_str);
8685
count++;
@@ -90,7 +89,7 @@ public static final void save(Context context, Throwable ex,
9089
} catch (Exception e) {
9190
e.printStackTrace();
9291
} finally {
93-
if(reader != null) {
92+
if (reader != null) {
9493
try {
9594
reader.close();
9695
} catch (Exception e) {
@@ -100,23 +99,50 @@ public static final void save(Context context, Throwable ex,
10099
}
101100
}
102101
mFile.delete();
103-
}
102+
}
104103

105104
mFile.createNewFile();
106-
107-
mBufferedWriter = new BufferedWriter(new FileWriter(mFile, true));// 追加模式写文件
108-
mBufferedWriter.append(CrashSnapshot.snapshot(context, uncaught, timestamp, stackTrace, count));
109-
mBufferedWriter.flush();
110-
} catch (Exception e) {
111-
e.printStackTrace();
112-
} finally {
113-
if (mBufferedWriter != null) {
114-
try {
115-
mBufferedWriter.close();
116-
} catch (IOException e) {
105+
106+
mBufferedWriter = new BufferedWriter(new FileWriter(mFile, true));// 追加模式写文件
107+
mBufferedWriter.append(CrashSnapshot.snapshot(context, uncaught, timestamp, stackTrace, count));
108+
mBufferedWriter.flush();
109+
saveJson(context, uncaught, timestamp, stackTrace, count);
110+
} catch (Exception e) {
111+
e.printStackTrace();
112+
} finally {
113+
if (mBufferedWriter != null) {
114+
try {
115+
mBufferedWriter.close();
116+
} catch (IOException e) {
117+
e.printStackTrace();
118+
}
119+
}
120+
}
121+
}
122+
123+
public static void saveJson(Context context, boolean uncaught, String timestamp, String trace, int count) {
124+
File mFile = new File(StorageUtil.getWritePath("jsonUpLoad.crashlog", StorageType.TYPE_LOG));
125+
BufferedWriter mBufferedWriter = null;
126+
try {
127+
128+
File pFile = mFile.getParentFile();
129+
if (!pFile.exists()) {
130+
pFile.mkdirs();
131+
}
132+
133+
mBufferedWriter = new BufferedWriter(new FileWriter(mFile));
134+
mBufferedWriter.append(CrashSnapshot.snapshotJson(context, uncaught, timestamp, trace, count));
135+
mBufferedWriter.flush();
136+
} catch (IOException e) {
137+
e.printStackTrace();
138+
} finally {
139+
if (mBufferedWriter != null) {
140+
try {
141+
mBufferedWriter.close();
142+
} catch (IOException e) {
117143
e.printStackTrace();
118-
}
119-
}
120-
}
121-
}
144+
}
145+
}
146+
}
147+
}
122148
}

android/src/main/java/com/netease/im/common/crash/CrashSnapshot.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import android.provider.Settings.Secure;
1212
import android.text.TextUtils;
1313

14+
import com.alibaba.fastjson.JSONObject;
1415
import com.netease.im.IMApplication;
1516
import com.netease.im.common.sys.InstallUtil;
1617
import com.netease.im.common.sys.SysInfoUtil;
18+
import com.netease.im.uikit.common.util.log.LogUtil;
1719
import com.netease.im.uikit.common.util.sys.NetworkUtil;
1820

1921
import java.io.BufferedReader;
@@ -204,6 +206,25 @@ private static String getSizeWithUnit(long size) {
204206
}
205207

206208

209+
public static String snapshotJson(Context context, boolean uncaught, String timestamp, String trace, int count) {
210+
JSONObject object = new JSONObject();
211+
object.put("count",String.valueOf(count));
212+
object.put("time",timestamp);
213+
object.put("device",SysInfoUtil.getPhoneModelWithManufacturer());
214+
object.put("android",SysInfoUtil.getOsInfo());
215+
object.put("system",Build.DISPLAY);
216+
object.put("battery",battery());
217+
object.put("rooted: ", isRooted() ? "yes" : "no");
218+
object.put("ram",ram());
219+
object.put("disk",disk());
220+
object.put("ver", String.format("%d", InstallUtil.getVersionCode(context)));
221+
object.put("caught", uncaught ? "no" : "yes");
222+
object.put("network",NetworkUtil.getNetworkInfo(context));
223+
224+
object.put("errorInfo",trace);
225+
LogUtil.e("snapshot",object.toString());
226+
return object.toString();
227+
}
207228
public static String snapshot(Context context, boolean uncaught, String timestamp, String trace, int count) {
208229
Map<String, String> info = new LinkedHashMap<String, String>();
209230
info.put("count: ", String.valueOf(count));
@@ -232,6 +253,7 @@ public static String snapshot(Context context, boolean uncaught, String timestam
232253
sb.append(System.getProperty("line.separator"));
233254
sb.append(System.getProperty("line.separator"));
234255
sb.append(System.getProperty("line.separator"));
256+
LogUtil.e("snapshot",sb.toString());
235257
return sb.toString();
236258
}
237259

0 commit comments

Comments
 (0)