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 253cc41
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 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: 28 additions & 6 deletions src/main/java/network/brightspots/rcv/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.application.Platform;
import javafx.scene.control.TextArea;
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,40 @@ 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);
// 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
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 (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 253cc41

Please sign in to comment.