Skip to content

Commit

Permalink
make the log output faster, and add colors too
Browse files Browse the repository at this point in the history
  • Loading branch information
artoonie committed May 8, 2024
1 parent c02463b commit 66a89ef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import javafx.scene.control.Control;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuBar;
import javafx.scene.control.RadioButton;
import javafx.scene.control.SelectionMode;
Expand Down Expand Up @@ -126,7 +127,7 @@ public class GuiConfigController implements Initializable {
private boolean guiIsBusy;

@FXML
private TextArea textAreaStatus;
private ListView<Label> textAreaStatus;
@FXML
private TextArea textAreaHelp;
@FXML
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/network/brightspots/rcv/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Captures all INFO level logging for the execution of a session.
* "session" could span multiple tabulations in GUI mode.
*
* GUI handler (INFO) -> textArea
* GUI handler (INFO) -> listView
* Displays INFO level logging in GUI for user feedback in GUI mode.
*
* Default handler -> console
Expand All @@ -44,7 +44,12 @@
import java.util.logging.Level;
import java.util.logging.LogRecord;
import javafx.application.Platform;
import javafx.scene.control.TextArea;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.Background;
import javafx.scene.paint.Color;

class Logger {

Expand Down Expand Up @@ -156,23 +161,42 @@ private static void log(Level level, String message, Object... obj) {
}

// add logging to the provided text area for display to user in the GUI
static void addGuiLogging(TextArea textArea) {
static void addGuiLogging(ListView<Label> listView) {
ObservableList<Label> logMessages = FXCollections.observableArrayList();
listView.setItems(logMessages);

java.util.logging.Handler guiHandler =
new Handler() {
@Override
public void publish(LogRecord record) {
if (isLoggable(record)) {
String msg = getFormatter().format(record);
Label logLabel = new Label(msg);

// Set background color based on log level
if (record.getLevel() == Level.SEVERE) {
logLabel.setBackground(Background.fill(Color.DARKRED));
} else if (record.getLevel() == Level.WARNING) {
logLabel.setBackground(Background.fill(Color.DARKORANGE));
}

// if we are executing on the GUI thread we can post immediately (e.g. button clicks)
// otherwise schedule the text update to run on the GUI thread
if (Platform.isFxApplicationThread()) {
textArea.appendText(msg);
addFromMainThread(logLabel);
} else {
Platform.runLater(() -> textArea.appendText(msg));
Platform.runLater(() -> addFromMainThread(logLabel));
}
}
}

private void addFromMainThread(Label logLabel) {
logMessages.add(logLabel);

// Scroll to the bottom of the listview
listView.scrollTo(logMessages.size() - 1);
}

@Override
public void flush() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,12 @@
styleClass="help-text-area" wrapText="true" BorderPane.alignment="TOP_LEFT"/>
</right>
<bottom>
<TextArea id="textStatus" fx:id="textAreaStatus" editable="false" prefHeight="200.0"
prefWidth="200.0" styleClass="console-text-area" wrapText="true"
BorderPane.alignment="TOP_LEFT">
<ListView id="textStatus" fx:id="textAreaStatus" editable="false" prefHeight="200.0"
prefWidth="200.0" styleClass="console-text-area" BorderPane.alignment="TOP_LEFT">
<BorderPane.margin>
<Insets bottom="4.0" left="4.0" right="4.0" top="4.0"/>
</BorderPane.margin>
</TextArea>
</ListView>
</bottom>
</BorderPane>
</ScrollPane>

0 comments on commit 66a89ef

Please sign in to comment.