2121
2222class 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}
0 commit comments