Skip to content

Commit 4f204c8

Browse files
committed
Removed compile time dependency on logback.
1 parent 540f99e commit 4f204c8

File tree

3 files changed

+60
-116
lines changed

3 files changed

+60
-116
lines changed

engine/build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ dependencies {
9999
compile group: 'org.abego.treelayout', name: 'org.abego.treelayout.core', version: '1.0.1'
100100
compile group: 'com.miglayout', name: 'miglayout-core', version: '4.2'
101101

102-
// CrashReporter
103-
compile group: 'brianbb', name: 'jpastebin', version: '1.0.0'
104-
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.13'
105-
106102
// Wildcard dependency to catch any libs provided with the project (remote repo preferred instead)
107103
compile fileTree(dir: 'libs', include: '*.jar')
108104

109105
// TODO: These could be moved into facade
106+
compile group: 'brianbb', name: 'jpastebin', version: '1.0.0'
107+
runtime group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.13'
110108
runtime group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.5'
111109
// And here is Groovy to read the config file
112110
runtime group: 'org.codehaus.groovy', name: 'groovy', version: '2.1.7'

facades/PC/src/main/java/org/terasology/engine/CrashReporter.java

Lines changed: 49 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@
1616

1717
package 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.*;
2432
import java.awt.event.ActionEvent;
2533
import java.awt.event.ActionListener;
2634
import java.awt.event.MouseAdapter;
@@ -33,46 +41,12 @@
3341
import java.net.URL;
3442
import java.nio.charset.Charset;
3543
import 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
*/
7852
public 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
}

facades/PC/src/main/java/org/terasology/engine/Terasology.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
*/
1616
package org.terasology.engine;
1717

18-
import java.awt.GraphicsEnvironment;
19-
import java.nio.file.Path;
20-
import java.nio.file.Paths;
21-
import java.util.Collection;
22-
18+
import com.google.common.collect.Lists;
2319
import org.terasology.engine.modes.StateMainMenu;
2420
import org.terasology.engine.paths.PathManager;
2521
import org.terasology.engine.subsystem.EngineSubsystem;
@@ -33,7 +29,10 @@
3329
import org.terasology.engine.subsystem.lwjgl.LwjglInput;
3430
import org.terasology.engine.subsystem.lwjgl.LwjglTimer;
3531

36-
import com.google.common.collect.Lists;
32+
import java.awt.*;
33+
import java.nio.file.Path;
34+
import java.nio.file.Paths;
35+
import java.util.Collection;
3736

3837
/**
3938
* Main method for launching Terasology
@@ -42,7 +41,7 @@
4241
* @author Kireev Anton <[email protected]>
4342
*/
4443
public final class Terasology {
45-
44+
4645
private static final String HOME_ARG = "-homedir=";
4746
private static final String LOCAL_ARG = "-homedir";
4847
private static final String HEADLESS_ARG = "-headless";
@@ -68,14 +67,14 @@ public static void main(String[] args) {
6867
} else {
6968
PathManager.getInstance().useDefaultHomePath();
7069
}
71-
70+
7271
Collection<EngineSubsystem> subsystemList;
7372
if (isHeadless) {
7473
subsystemList = Lists.newArrayList(new HeadlessGraphics(), new HeadlessTimer(), new HeadlessAudio(), new HeadlessInput());
7574
} else {
7675
subsystemList = Lists.<EngineSubsystem>newArrayList(new LwjglGraphics(), new LwjglTimer(), new LwjglAudio(), new LwjglInput());
7776
}
78-
77+
7978
TerasologyEngine engine = new TerasologyEngine(subsystemList);
8079
engine.init();
8180
if (isHeadless) {
@@ -85,7 +84,7 @@ public static void main(String[] args) {
8584
}
8685
engine.dispose();
8786
} catch (Throwable t) {
88-
87+
8988
if (!GraphicsEnvironment.isHeadless()) {
9089
CrashReporter.report(t);
9190
}

0 commit comments

Comments
 (0)