Dernière activité 1727273021

cool class from https://github.com/RohitAwate/Everest/blob/master/BugReporter/src/BugReporter.java

Révision 3e861c65d166b7c2761afaca8c1d2f362844245b

BugReporter.java Brut
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
17import java.io.*;
18import java.nio.charset.Charset;
19import java.time.LocalDate;
20import java.time.LocalDateTime;
21import java.util.Scanner;
22import java.util.zip.ZipEntry;
23import java.util.zip.ZipOutputStream;
24
25public 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