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

make the log output faster, and add colors too #832

Merged
merged 6 commits into from
May 31, 2024
Merged
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
30 changes: 19 additions & 11 deletions src/main/java/network/brightspots/rcv/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.BlockingDeque;
Copy link
Contributor

Choose a reason for hiding this comment

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

unused? otherwise this seems fine to me!

import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
Expand Down Expand Up @@ -75,6 +77,7 @@ class Logger {
private static java.util.logging.Logger logger;
private static java.util.logging.FileHandler tabulationHandler;
private static String tabulationLogPattern;
private static final List<Label> labelsQueue = new ArrayList<>();

static void setup() {
logger = java.util.logging.Logger.getLogger("");
Expand Down Expand Up @@ -225,7 +228,7 @@ public void publish(LogRecord record) {
String msg = getFormatter().format(record);
Label logLabel = new Label(msg);
logLabel.setPadding(new Insets(0, 0, 0, 0));
logLabel.setWrapText(true);
logLabel.setWrapText(false);

// Set background color based on log level
if (record.getLevel() == Level.SEVERE) {
Expand All @@ -241,20 +244,25 @@ public void publish(LogRecord record) {
contextMenu.getItems().add(copyMenuItem);
logLabel.setContextMenu(contextMenu);

// 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()) {
addFromMainThread(logLabel);
} else {
Platform.runLater(() -> addFromMainThread(logLabel));
// Rather than adding to the list too many times in a row,
// we add to a queue and schedule an occasional update to the UI.
// This prevents the UI from lagging when there are many log messages.
synchronized (labelsQueue) {
labelsQueue.add(logLabel);

// The first item in the queue is the only one that needs to trigger the update.
if (labelsQueue.size() == 1) {
Platform.runLater(this::addFromMainThread);
}
}
}
}

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

// Scroll to the bottom of the listview
private void addFromMainThread() {
synchronized (labelsQueue) {
logMessages.addAll(labelsQueue);
labelsQueue.clear();
}
listView.scrollTo(logMessages.size() - 1);
}

Expand Down
Loading