-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathErrorReporter.java
More file actions
76 lines (69 loc) · 2.39 KB
/
ErrorReporter.java
File metadata and controls
76 lines (69 loc) · 2.39 KB
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
package RouteMapMaker;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
public class ErrorReporter {
private static ErrorReporter er = new ErrorReporter();
private String reportUrl;
private double version;
private static boolean debug = true;
public static boolean isDebugBuild() {
return debug;
}
public static void setReportURL(String s, double version) {
er.reportUrl = s;
er.version = version;
}
public static void report(Throwable e) {
if(er.reportUrl==null) {
return;
}
System.out.println("send error report to " + er.reportUrl);
try {
URL url = new URL(er.reportUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/plain");
StringBuilder sb = new StringBuilder("version: " + String.valueOf(er.version) + "\n"); //バージョン
sb.append(ErrorReporter.getMacAddr() + "\n");
sb.append(e.toString() + "\n"); //エラーメッセージ
Arrays.asList(e.getStackTrace()).forEach(st -> sb.append(st.toString() + "\n"));
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(sb.toString());
out.close();
conn.connect();
conn.getInputStream(); //これをもってHTTP接続が行われる.
} catch(IOException ioe) {
// stacktraceの送信だけなので特にエラー処理はしない.
ioe.printStackTrace();
}
}
public static String getMacAddr() throws SocketException, UnknownHostException {
byte[]mac = null;
//v4 localアドレスを持つnetwork interfaceを探す
for(NetworkInterface ni: Collections.list(NetworkInterface.getNetworkInterfaces())) {
for(InetAddress ip: Collections.list(ni.getInetAddresses())) {
if(ip.isSiteLocalAddress()) {
mac = ni.getHardwareAddress();
}
}
}
if(mac==null) {
// macアドレスが取得できない場合がある
return "NotAvailable";
}
StringBuilder mac_sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
mac_sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
return mac_sb.toString();
}
}