a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.basic.security.utils;
 
import android.os.Environment;
 
import com.basic.security.activity.MainActivity;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
public class LogFileUtils {
    private static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
 
    public static void logStacktraceToFile(MainActivity maFragments) {
        if (1 == 1) {
            return;
        }
        try {
            throw new RuntimeException("showFragment");
        } catch (Exception ex) {
            Map<String, String> infos = new HashMap<>();
            StringBuffer sb = new StringBuffer("");
            try {
                SimpleDateFormat sDateFormat = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                String date = sDateFormat.format(new java.util.Date());
                sb.append("\r\n" + date + "\n");
                for (Map.Entry<String, String> entry : infos.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    sb.append(key + "=" + value + "\n");
                }
                Writer writer = new StringWriter();
                PrintWriter printWriter = new PrintWriter(writer);
                ex.printStackTrace(printWriter);
                Throwable cause = ex.getCause();
                while (cause != null) {
                    cause.printStackTrace(printWriter);
                    cause = cause.getCause();
                }
                printWriter.flush();
                printWriter.close();
                String result = writer.toString();
                sb.append(result);
                String fileName = writeFile(sb.toString());
            } catch (Exception e) {
                sb.append("an error occured while writing file...\r\n");
            }
        }
    }
 
    public static String getGlobalpath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath()
                + File.separator + "debug" + File.separator;
    }
 
    private static String writeFile(String sb) throws Exception {
        String time = formatter.format(new Date());
        String fileName = "crash-" + time + ".log";
//        if (FileUtil.hasSdcard()) {
        if (true) {
            String path = getGlobalpath();
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            FileOutputStream fos = new FileOutputStream(path + fileName, true);
            fos.write(sb.getBytes());
            fos.flush();
            fos.close();
        }
        return fileName;
    }
}