Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented error console in JavaFX #1383

Merged
merged 15 commits into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/main/java/net/sf/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
import net.sf.jabref.logic.importer.OutputPrinter;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.logging.GuiAppender;
import net.sf.jabref.logic.search.SearchQuery;
import net.sf.jabref.logic.undo.AddUndoableActionEvent;
import net.sf.jabref.logic.undo.UndoChangeEvent;
Expand Down Expand Up @@ -438,7 +437,7 @@ public void actionPerformed(ActionEvent e) {
private final AbstractAction manageJournals = new ManageJournalsAction();
private final AbstractAction databaseProperties = new DatabasePropertiesAction();
private final AbstractAction bibtexKeyPattern = new BibtexKeyPatternAction();
private final AbstractAction errorConsole = new ErrorConsoleAction(Globals.getStreamEavesdropper(), GuiAppender.CACHE);
private final AbstractAction errorConsole = new ErrorConsoleAction();

private final AbstractAction cleanupEntries = new GeneralAction(Actions.CLEANUP,
Localization.menuTitle("Cleanup entries") + ELLIPSES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import javafx.application.Platform;

import net.sf.jabref.gui.errorconsole.ErrorConsoleView;
import net.sf.jabref.logic.error.StreamEavesdropper;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.logging.Cache;

/**
* Such an error console can be
Expand All @@ -21,7 +19,7 @@
*/
public class ErrorConsoleAction extends AbstractAction {

public ErrorConsoleAction(StreamEavesdropper streamEavesdropper, Cache cache) {
public ErrorConsoleAction() {
super(Localization.menuTitle("View event log"));
putValue(Action.SHORT_DESCRIPTION, Localization.lang("Display all error messages"));
}
Expand Down
63 changes: 33 additions & 30 deletions src/main/java/net/sf/jabref/gui/errorconsole/ErrorConsoleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@

import net.sf.jabref.gui.FXAlert;
import net.sf.jabref.gui.IconTheme;
import net.sf.jabref.logic.error.LogMessage;
import net.sf.jabref.logic.error.MessageType;
import net.sf.jabref.logic.l10n.Localization;

import com.airhacks.afterburner.views.FXMLView;
import com.sun.star.lang.IndexOutOfBoundsException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;

import static org.apache.logging.log4j.Level.ERROR;
import static org.apache.logging.log4j.Level.INFO;
import static org.apache.logging.log4j.Level.WARN;

public class ErrorConsoleView extends FXMLView {

Expand All @@ -44,7 +51,7 @@ public class ErrorConsoleView extends FXMLView {
@FXML
private Button createIssueButton;
@FXML
private ListView<LogMessage> allMessages;
private ListView<LogEvent> allMessages;
@FXML
private Label descriptionLabel;

Expand All @@ -59,12 +66,14 @@ public void show() {
errorConsole.setDialogPane(pane);
errorConsole.setResizable(true);
errorConsole.show();
Log log = LogFactory.getLog(this.getClass());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was for test purposes, right? Please remove it.

log.error("", new IndexOutOfBoundsException());
}

@FXML
private void initialize() {
listViewStyle();
allMessages.setItems(errorViewModel.getAllMessagesData());
allMessages.itemsProperty().bind(errorViewModel.allMessagesDataproperty());
descriptionLabel.setGraphic(IconTheme.JabRefIcon.CONSOLE.getGraphicNode());
}

Expand All @@ -90,38 +99,32 @@ private void closeErrorDialog() {
private void listViewStyle() {
// Handler for listCell appearance (example for exception Cell)
allMessages.setCellFactory(
new Callback<ListView<LogMessage>, ListCell<LogMessage>>() {
new Callback<ListView<LogEvent>, ListCell<LogEvent>>() {
@Override
public ListCell<LogMessage> call(
ListView<LogMessage> listView) {
return new ListCell<LogMessage>() {
public ListCell<LogEvent> call(
ListView<LogEvent> listView) {
return new ListCell<LogEvent>() {

@Override
public void updateItem(LogMessage logMessage, boolean empty) {
public void updateItem(LogEvent logMessage, boolean empty) {
super.updateItem(logMessage, empty);
if (logMessage != null) {
setText(logMessage.getMessage());

MessageType prio = logMessage.getPriority();
switch (prio) {
case EXCEPTION:
getStyleClass().add("exception");
setGraphic(IconTheme.JabRefIcon.INTEGRITY_FAIL.getGraphicNode());
break;
case OUTPUT:
getStyleClass().add("output");
setGraphic(IconTheme.JabRefIcon.INTEGRITY_WARN.getGraphicNode());
break;
case LOG:
getStyleClass().add("log");
setGraphic(IconTheme.JabRefIcon.INTEGRITY_INFO.getGraphicNode());
break;
default:
break;
setText(logMessage.getMessage().toString());

Level prio = logMessage.getLevel();
if (prio.equals(ERROR)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls use a switch statement here and show an info by default (instead of nothing with null/null)

getStyleClass().add("exception");
setGraphic(IconTheme.JabRefIcon.INTEGRITY_FAIL.getGraphicNode());
} else if (prio.equals(WARN)) {
getStyleClass().add("output");
setGraphic(IconTheme.JabRefIcon.INTEGRITY_WARN.getGraphicNode());
} else if (prio.equals(INFO)) {
getStyleClass().add("log");
setGraphic(IconTheme.JabRefIcon.INTEGRITY_INFO.getGraphicNode());
} else {
setText(null);
setGraphic(null);
}
} else {
setText(null);
setGraphic(null);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@
import java.text.SimpleDateFormat;
import java.util.Date;

import javafx.collections.ObservableList;
import javafx.beans.property.ListProperty;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefGUI;
import net.sf.jabref.gui.ClipBoardManager;
import net.sf.jabref.gui.FXDialogs;
import net.sf.jabref.gui.desktop.JabRefDesktop;
import net.sf.jabref.logic.error.LogMessage;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.logging.LogMessages;
import net.sf.jabref.logic.util.BuildInfo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.utils.URIBuilder;
import org.apache.logging.log4j.core.LogEvent;

public class ErrorConsoleViewModel {

private static final Log LOGGER = LogFactory.getLog(ErrorConsoleViewModel.class);
private final DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
private final Date date = new Date();
private ObservableList<LogMessage> allMessagesData = LogMessages.getInstance().messagesProperty();
private ListProperty<LogEvent> allMessagesData = LogMessages.getInstance().messagesProperty();

public ObservableList<LogMessage> getAllMessagesData(){
public ListProperty<LogEvent> allMessagesDataproperty(){
return this.allMessagesData;
}

Expand All @@ -54,8 +54,8 @@ public ObservableList<LogMessage> getAllMessagesData(){
*/
private String getLogMessagesAsString (){
StringBuilder logMessagesContent = new StringBuilder();
for (LogMessage message : getAllMessagesData()) {
logMessagesContent.append(message.getMessage() + System.lineSeparator());
for (LogEvent message : allMessagesDataproperty()) {
logMessagesContent.append(message.getMessage().toString() + System.lineSeparator());
}
return logMessagesContent.toString();
}
Expand Down
61 changes: 0 additions & 61 deletions src/main/java/net/sf/jabref/logic/error/LogMessage.java

This file was deleted.

28 changes: 0 additions & 28 deletions src/main/java/net/sf/jabref/logic/error/MessageType.java

This file was deleted.

48 changes: 28 additions & 20 deletions src/main/java/net/sf/jabref/logic/error/StreamEavesdropper.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package net.sf.jabref.logic.error;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import javafx.application.Platform;

import net.sf.jabref.logic.logging.LogMessages;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.message.SimpleMessage;

/**
* Allows to eavesdrop on an out and an err stream.
* <p/>
* It can be used to listen to any messages to the System.out and System.err.
*/
public class StreamEavesdropper {

private final ByteArrayOutputStream errByteStream = new ByteArrayOutputStream();
private final ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();

private final PrintStream systemOut;
private final PrintStream systemErr;

Expand All @@ -33,40 +34,47 @@ public static StreamEavesdropper eavesdropOnSystem() {
return streamEavesdropper;
}

public TeeStream getOutStream() {
PrintStream consoleOut = new PrintStream(outByteStream) {
/**
* Return a new {@code PrintStream} which also creates a new log event with {@link Level#WARN} for each message and forwards it to the {@link LogMessages} archive.
* @return a PrintStream
*/
public PrintStream getOutStream() {
return new PrintStream(systemOut) {
@Override
public void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
String message = new String(buf, off, len);
addToLog(message, MessageType.OUTPUT);
addToLog(message, Level.WARN);
}
};
return new TeeStream(consoleOut, systemOut);
}

public TeeStream getErrStream() {
PrintStream consoleErr = new PrintStream(errByteStream) {
/**
* Return a new {@code PrintStream} which also creates a new log event with {@link Level#ERROR} for each message and forwards it to the {@link LogMessages} archive.
* @return a PrintStream
*/
public PrintStream getErrStream() {
return new PrintStream(systemErr) {
@Override
public void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
String message = new String(buf, off, len);
addToLog(message, MessageType.EXCEPTION
);
addToLog(message, Level.ERROR);
}
};
return new TeeStream(consoleErr, systemErr);
}

private void addToLog(String s, MessageType priority) {
if (!s.equals(System.lineSeparator())) {
LogMessage messageWithPriority = new LogMessage(s.replaceAll(System.lineSeparator(), ""), priority);
/**
* Creates a new log event with the given parameters and forwards it to the {@link LogMessages} archive.
* @param message message of log event
* @param priority level of log event
*/
private void addToLog(String message, Level priority) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please go through the code that all priority and prio are properly renamed to level.

if (!message.equals(System.lineSeparator())) {
String messageFormat = message.replaceAll(System.lineSeparator(), "");
LogEvent messageWithPriority = Log4jLogEvent.newBuilder().setMessage(new SimpleMessage(messageFormat)).setLevel(priority).build();
Platform.runLater(() -> LogMessages.getInstance().add(messageWithPriority));
}
}

public String getOutput() {
return outByteStream.toString();
}

}
Loading