BugReporter.java
· 5.4 KiB · Java
Brut
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.*;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class BugReporter {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Everest Bug Reporting Service");
System.out.println("-----------------------------\n");
System.out.println("Please describe the issue with as much detail and clarity as possible (no newline): ");
String userMessage = scanner.nextLine();
scanner.close();
generateReportFile(generateReport(userMessage));
generateZipFile();
System.out.println("\nYour report was submitted successfully reported and will be evaluated soon.");
System.out.println("Thank you! :)");
}
private static String generateReport(String userMessage) {
StringBuilder report = new StringBuilder();
report.append("Report date: ");
report.append(LocalDateTime.now());
report.append("\n\n");
report.append(getSystemDetails());
report.append("User Message:\n");
report.append(userMessage);
return report.toString();
}
private static String getSystemDetails() {
StringBuilder builder = new StringBuilder();
String OS = System.getProperty("os.name");
if (OS.equals("Linux")) {
builder.append(getLinuxDetails());
}
builder.append("OS: ");
builder.append(OS);
builder.append(" ");
builder.append(System.getProperty("os.arch"));
builder.append(" ");
builder.append(System.getProperty("os.version"));
builder.append("\nJava VM: ");
builder.append(System.getProperty("java.vm.name"));
builder.append("\nVM Version: ");
builder.append(System.getProperty("java.version"));
builder.append("\nVM Vendor: ");
builder.append(System.getProperty("java.vendor"));
builder.append("\n\n");
return builder.toString();
}
private static void generateReportFile(String reportContents) {
String reportFileName = "Report - " + LocalDate.now() + ".txt";
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("logs/" + reportFileName));
writer.write(reportContents);
writer.close();
} catch (IOException IOE) {
IOE.printStackTrace();
}
}
private static void generateZipFile() {
try {
Scanner scanner;
FileInputStream fileInputStream;
ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream("BugReport-" + LocalDate.now() + ".zip"), Charset.forName("UTF-8"));
File sourceDir = new File("logs/");
String[] logFiles = sourceDir.list();
for (String logFile : logFiles) {
zipStream.putNextEntry(new ZipEntry(logFile));
fileInputStream = new FileInputStream("logs/" + logFile);
scanner = new Scanner(fileInputStream);
while (scanner.hasNext()) {
zipStream.flush();
zipStream.write(scanner.nextLine().getBytes());
zipStream.write('\n');
}
zipStream.closeEntry();
scanner.close();
fileInputStream.close();
}
zipStream.close();
} catch (IOException IOE) {
IOE.printStackTrace();
}
}
private static String getLinuxDetails() {
String line;
try {
File etcDir = new File("/etc/");
String releaseFile = "";
for (String file : etcDir.list()) {
if (file.endsWith("-release")) {
releaseFile = file;
break;
}
}
BufferedReader reader = new BufferedReader(new FileReader("/etc/" + releaseFile));
while ((line = reader.readLine()) != null) {
if (line.startsWith("PRETTY_NAME"))
break;
}
reader.close();
if (!line.equals("")) {
line = "Distribution: " + line.substring(13, line.length() - 1) + "\n";
}
} catch (IOException IOE) {
line = "";
Scanner scanner = new Scanner(System.in);
System.out.println("We couldn't fetch information about your Linux distribution. Please fill in the following details:");
System.out.println("Distribution name: ");
line += "Distribution: " + scanner.nextLine() + "\n";
System.out.println("Version: ");
line += "Version: " + scanner.nextLine() + "\n";
scanner.close();
}
return line;
}
}
1 | /* |
2 | * Copyright 2018 Rohit Awate. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | import java.io.*; |
18 | import java.nio.charset.Charset; |
19 | import java.time.LocalDate; |
20 | import java.time.LocalDateTime; |
21 | import java.util.Scanner; |
22 | import java.util.zip.ZipEntry; |
23 | import java.util.zip.ZipOutputStream; |
24 | |
25 | public class BugReporter { |
26 | public static void main(String args[]) { |
27 | Scanner scanner = new Scanner(System.in); |
28 | System.out.println("Everest Bug Reporting Service"); |
29 | System.out.println("-----------------------------\n"); |
30 | System.out.println("Please describe the issue with as much detail and clarity as possible (no newline): "); |
31 | String userMessage = scanner.nextLine(); |
32 | scanner.close(); |
33 | |
34 | generateReportFile(generateReport(userMessage)); |
35 | generateZipFile(); |
36 | |
37 | System.out.println("\nYour report was submitted successfully reported and will be evaluated soon."); |
38 | System.out.println("Thank you! :)"); |
39 | } |
40 | |
41 | private static String generateReport(String userMessage) { |
42 | StringBuilder report = new StringBuilder(); |
43 | report.append("Report date: "); |
44 | report.append(LocalDateTime.now()); |
45 | report.append("\n\n"); |
46 | report.append(getSystemDetails()); |
47 | report.append("User Message:\n"); |
48 | report.append(userMessage); |
49 | return report.toString(); |
50 | } |
51 | |
52 | private static String getSystemDetails() { |
53 | StringBuilder builder = new StringBuilder(); |
54 | String OS = System.getProperty("os.name"); |
55 | if (OS.equals("Linux")) { |
56 | builder.append(getLinuxDetails()); |
57 | } |
58 | builder.append("OS: "); |
59 | builder.append(OS); |
60 | builder.append(" "); |
61 | builder.append(System.getProperty("os.arch")); |
62 | builder.append(" "); |
63 | builder.append(System.getProperty("os.version")); |
64 | builder.append("\nJava VM: "); |
65 | builder.append(System.getProperty("java.vm.name")); |
66 | builder.append("\nVM Version: "); |
67 | builder.append(System.getProperty("java.version")); |
68 | builder.append("\nVM Vendor: "); |
69 | builder.append(System.getProperty("java.vendor")); |
70 | builder.append("\n\n"); |
71 | |
72 | return builder.toString(); |
73 | } |
74 | |
75 | private static void generateReportFile(String reportContents) { |
76 | String reportFileName = "Report - " + LocalDate.now() + ".txt"; |
77 | try { |
78 | BufferedWriter writer = new BufferedWriter(new FileWriter("logs/" + reportFileName)); |
79 | writer.write(reportContents); |
80 | writer.close(); |
81 | } catch (IOException IOE) { |
82 | IOE.printStackTrace(); |
83 | } |
84 | } |
85 | |
86 | private static void generateZipFile() { |
87 | try { |
88 | Scanner scanner; |
89 | FileInputStream fileInputStream; |
90 | ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream("BugReport-" + LocalDate.now() + ".zip"), Charset.forName("UTF-8")); |
91 | File sourceDir = new File("logs/"); |
92 | String[] logFiles = sourceDir.list(); |
93 | |
94 | for (String logFile : logFiles) { |
95 | zipStream.putNextEntry(new ZipEntry(logFile)); |
96 | |
97 | fileInputStream = new FileInputStream("logs/" + logFile); |
98 | scanner = new Scanner(fileInputStream); |
99 | |
100 | while (scanner.hasNext()) { |
101 | zipStream.flush(); |
102 | zipStream.write(scanner.nextLine().getBytes()); |
103 | zipStream.write('\n'); |
104 | } |
105 | |
106 | zipStream.closeEntry(); |
107 | scanner.close(); |
108 | fileInputStream.close(); |
109 | } |
110 | |
111 | zipStream.close(); |
112 | } catch (IOException IOE) { |
113 | IOE.printStackTrace(); |
114 | } |
115 | } |
116 | |
117 | private static String getLinuxDetails() { |
118 | String line; |
119 | |
120 | try { |
121 | File etcDir = new File("/etc/"); |
122 | String releaseFile = ""; |
123 | for (String file : etcDir.list()) { |
124 | if (file.endsWith("-release")) { |
125 | releaseFile = file; |
126 | break; |
127 | } |
128 | } |
129 | BufferedReader reader = new BufferedReader(new FileReader("/etc/" + releaseFile)); |
130 | |
131 | while ((line = reader.readLine()) != null) { |
132 | if (line.startsWith("PRETTY_NAME")) |
133 | break; |
134 | } |
135 | reader.close(); |
136 | if (!line.equals("")) { |
137 | line = "Distribution: " + line.substring(13, line.length() - 1) + "\n"; |
138 | } |
139 | } catch (IOException IOE) { |
140 | line = ""; |
141 | Scanner scanner = new Scanner(System.in); |
142 | System.out.println("We couldn't fetch information about your Linux distribution. Please fill in the following details:"); |
143 | System.out.println("Distribution name: "); |
144 | line += "Distribution: " + scanner.nextLine() + "\n"; |
145 | System.out.println("Version: "); |
146 | line += "Version: " + scanner.nextLine() + "\n"; |
147 | scanner.close(); |
148 | } |
149 | return line; |
150 | } |
151 | } |
152 | |
153 |