1616
1717package org .terasology .engine ;
1818
19- import java .awt .Component ;
20- import java .awt .Cursor ;
21- import java .awt .Desktop ;
22- import java .awt .Dimension ;
23- import java .awt .GridLayout ;
19+ import com .google .common .base .Joiner ;
20+ import org .jpaste .exceptions .PasteException ;
21+ import org .jpaste .pastebin .PasteExpireDate ;
22+ import org .jpaste .pastebin .Pastebin ;
23+ import org .jpaste .pastebin .PastebinLink ;
24+ import org .jpaste .pastebin .PastebinPaste ;
25+ import org .terasology .engine .paths .PathManager ;
26+
27+ import javax .imageio .ImageIO ;
28+ import javax .swing .*;
29+ import javax .swing .event .DocumentEvent ;
30+ import javax .swing .event .DocumentListener ;
31+ import java .awt .*;
2432import java .awt .event .ActionEvent ;
2533import java .awt .event .ActionListener ;
2634import java .awt .event .MouseAdapter ;
3341import java .net .URL ;
3442import java .nio .charset .Charset ;
3543import java .nio .file .Files ;
36- import java .nio .file .Paths ;
37- import java .util .Iterator ;
38- import java .util .List ;
39-
40- import javax .imageio .ImageIO ;
41- import javax .swing .Box ;
42- import javax .swing .BoxLayout ;
43- import javax .swing .Icon ;
44- import javax .swing .ImageIcon ;
45- import javax .swing .JButton ;
46- import javax .swing .JDialog ;
47- import javax .swing .JLabel ;
48- import javax .swing .JOptionPane ;
49- import javax .swing .JPanel ;
50- import javax .swing .JScrollPane ;
51- import javax .swing .JTabbedPane ;
52- import javax .swing .JTextArea ;
53- import javax .swing .LookAndFeel ;
54- import javax .swing .SwingUtilities ;
55- import javax .swing .UIManager ;
56- import javax .swing .event .DocumentEvent ;
57- import javax .swing .event .DocumentListener ;
58-
59- import org .jpaste .exceptions .PasteException ;
60- import org .jpaste .pastebin .PasteExpireDate ;
61- import org .jpaste .pastebin .Pastebin ;
62- import org .jpaste .pastebin .PastebinLink ;
63- import org .jpaste .pastebin .PastebinPaste ;
64- import org .slf4j .LoggerFactory ;
65-
66- import ch .qos .logback .classic .Logger ;
67- import ch .qos .logback .classic .spi .ILoggingEvent ;
68- import ch .qos .logback .core .Appender ;
69- import ch .qos .logback .core .FileAppender ;
70-
71- import com .google .common .base .Joiner ;
44+ import java .nio .file .Path ;
7245
7346/**
7447 * Displays a detailed error message and provides some options to communicate with devs.
7548 * Errors are reported to {@link System#err}
49+ *
7650 * @author Martin Steiger
7751 */
7852public final class CrashReporter {
@@ -89,15 +63,15 @@ public final class CrashReporter {
8963 private CrashReporter () {
9064 // don't create any instances
9165 }
92-
66+
9367 public static void report (final Throwable t ) {
9468
9569 // Swing element methods must be called in the swing thread
9670 try {
9771 final String logFileContent = getLogFileContent ();
9872
9973 SwingUtilities .invokeAndWait (new Runnable () {
100-
74+
10175 @ Override
10276 public void run () {
10377 LookAndFeel oldLaF = UIManager .getLookAndFeel ();
@@ -120,16 +94,16 @@ public void run() {
12094 }
12195
12296 private static void showModalDialog (Throwable exception , final String logFileContent ) {
123-
97+
12498 JPanel mainPanel = new JPanel ();
12599 mainPanel .setLayout (new BoxLayout (mainPanel , BoxLayout .Y_AXIS ));
126-
100+
127101 // Replace newline chars. with html newline elements (not needed in most cases)
128102 String text = exception .toString ().replaceAll ("\\ r?\\ n" , "<br/>" );
129103 JLabel message = new JLabel ("<html><h3>A fatal error occurred</h3><br/>" + text + "</html>" );
130104 mainPanel .add (message );
131105 message .setAlignmentX (Component .LEFT_ALIGNMENT );
132-
106+
133107 mainPanel .add (Box .createRigidArea (new Dimension (0 , 10 )));
134108
135109 // Tab pane
@@ -159,7 +133,7 @@ private static void showModalDialog(Throwable exception, final String logFileCon
159133 final JButton pastebinUpload = new JButton ("Upload log file to PasteBin" );
160134 pastebinUpload .setIcon (loadIcon ("icons/pastebin.png" ));
161135 pastebinUpload .addActionListener (new ActionListener () {
162-
136+
163137 @ Override
164138 public void actionPerformed (ActionEvent event ) {
165139
@@ -172,17 +146,17 @@ public void actionPerformed(ActionEvent event) {
172146 });
173147 // disable upload if log area text field is empty
174148 logArea .getDocument ().addDocumentListener (new DocumentListener () {
175-
149+
176150 @ Override
177151 public void removeUpdate (DocumentEvent e ) {
178152 update ();
179153 }
180-
154+
181155 @ Override
182156 public void insertUpdate (DocumentEvent e ) {
183157 update ();
184158 }
185-
159+
186160 @ Override
187161 public void changedUpdate (DocumentEvent e ) {
188162 update ();
@@ -193,12 +167,12 @@ private void update() {
193167 }
194168 });
195169 pastebinUpload .setEnabled (!logArea .getText ().isEmpty ()); // initial update of the button
196-
170+
197171 buttonPanel .add (pastebinUpload );
198172 JButton githubIssueButton = new JButton ("File an issue on GitHub" );
199173 githubIssueButton .setIcon (loadIcon ("icons/github.png" ));
200174 githubIssueButton .addActionListener (new ActionListener () {
201-
175+
202176 @ Override
203177 public void actionPerformed (ActionEvent e ) {
204178 openInBrowser (REPORT_ISSUE_LINK );
@@ -208,7 +182,7 @@ public void actionPerformed(ActionEvent e) {
208182 JButton enterIrc = new JButton ("Enter IRC channel" );
209183 enterIrc .setIcon (loadIcon ("icons/irc.png" ));
210184 enterIrc .addActionListener (new ActionListener () {
211-
185+
212186 @ Override
213187 public void actionPerformed (ActionEvent e ) {
214188 openInBrowser (JOIN_IRC_LINK );
@@ -218,20 +192,20 @@ public void actionPerformed(ActionEvent e) {
218192
219193 mainPanel .add (buttonPanel );
220194 mainPanel .add (Box .createRigidArea (new Dimension (0 , 10 )));
221-
195+
222196 // Custom close button
223197 JButton closeButton = new JButton ("Close" , loadIcon ("icons/close.png" ));
224-
198+
225199 showDialog (mainPanel , closeButton , "Fatal Error" , JOptionPane .ERROR_MESSAGE );
226200 }
227-
201+
228202 private static void showDialog (Component mainPanel , JButton closeButton , String title , int messageType ) {
229- Object [] opts = new Object [] { closeButton };
230-
203+ Object [] opts = new Object []{ closeButton };
204+
231205 // The error-message pane
232206 final JOptionPane pane = new JOptionPane (mainPanel , messageType , JOptionPane .DEFAULT_OPTION , null , opts , opts [0 ]);
233207 closeButton .addActionListener (new ActionListener () {
234-
208+
235209 @ Override
236210 public void actionPerformed (ActionEvent e ) {
237211 // calling setValue() closes the dialog
@@ -274,7 +248,7 @@ protected static void uploadPaste(final PastebinPaste paste) {
274248 public void run () {
275249 try {
276250 final PastebinLink link = paste .paste ();
277-
251+
278252 SwingUtilities .invokeLater (new Runnable () {
279253
280254 @ Override
@@ -286,7 +260,9 @@ public void run() {
286260 label .addMouseListener (new MouseAdapter () {
287261 public void mouseClicked (java .awt .event .MouseEvent e ) {
288262 openInBrowser (url );
289- };
263+ }
264+
265+ ;
290266 });
291267 }
292268 });
@@ -302,7 +278,7 @@ public void run() {
302278 }
303279 }
304280 };
305-
281+
306282 Thread thread = new Thread (runnable , "Upload paste" );
307283 thread .start ();
308284
@@ -325,52 +301,23 @@ private static void openInBrowser(String url) {
325301
326302 private static String getLogFileContent () {
327303 StringBuilder builder = new StringBuilder ();
328-
329- List <String > lines ;
330- try {
331- String logFile = getLogFilename ();
332304
333- lines = Files .readAllLines (Paths .get (logFile ), Charset .defaultCharset ());
334- for (String line : lines ) {
335- builder .append (line );
336- builder .append (System .lineSeparator ());
305+ try {
306+ Path logDirectory = PathManager .getInstance ().getLogPath ();
307+ if (logDirectory != null ) {
308+ Path logPath = PathManager .getInstance ().getLogPath ().resolve ("Terasology.log" );
309+ if (Files .exists (logPath )) {
310+ for (String line : Files .readAllLines (logPath , Charset .defaultCharset ())) {
311+ builder .append (line );
312+ builder .append (System .lineSeparator ());
313+ }
314+ }
337315 }
338316 } catch (Exception e ) {
339317 // we catch all here, because we want to continue execution in all cases
340318 e .printStackTrace (System .err );
341319 }
342-
343- return builder .toString ();
344- }
345320
346- private static String getLogFilename () {
347- String logFile = null ;
348-
349- org .slf4j .Logger logger = LoggerFactory .getLogger (org .slf4j .Logger .ROOT_LOGGER_NAME );
350-
351- // We try to get log filename directly from the Logback system
352- // PathManager only knows the path, and determining the file name is not
353- // straightforward for RollingFileAppenders
354-
355- if (logger instanceof Logger ) {
356- Logger logbackLogger = (Logger ) logger ;
357- Iterator <Appender <ILoggingEvent >> it = logbackLogger .iteratorForAppenders ();
358- while (it .hasNext ()) {
359- Appender <ILoggingEvent > app = it .next ();
360-
361- if (app instanceof FileAppender ) {
362- FileAppender <ILoggingEvent > fileApp = (FileAppender <ILoggingEvent >) app ;
363- if (logFile == null ) {
364- logFile = fileApp .getFile ();
365- } else {
366- System .err .println ("Multiple log files found!" );
367- }
368- }
369- }
370- } else {
371- System .err .println ("Logger ist not a Logback logger, but " + logger .getClass ().getName ());
372- }
373-
374- return logFile ;
321+ return builder .toString ();
375322 }
376323}
0 commit comments