I have a simple custom logging framework like this:
package something;
import javafx.scene.control.TextArea;
public class MyLogger {
public final TextArea textArea;
private boolean verboseMode = false;
private boolean debugMode = false;
public MyLogger(final TextArea textArea) {
this.textArea = textArea;
}
public MyLogger setVerboseMode(boolean value) {
verboseMode = value;
return this;
}
public MyLogger setDebugMode(boolean value) {
debugMode = value;
return this;
}
public boolean writeMessage(String msg) {
textArea.appendText(msg);
return true;
}
public boolean logMessage(String msg) {
return writeMessage(msg + "\n");
}
public boolean logWarning(String msg) {
return writeMessage("Warning: " + msg + "\n");
}
public boolean logError(String msg) {
return writeMessage("Error: " + msg + "\n");
}
public boolean logVerbose(String msg) {
return verboseMode ? writeMessage(msg + "\n") : true;
}
public boolean logDebug(String msg) {
return debugMode ? writeMessage("[DEBUG] " + msg + "\n") : true;
}
}
Now what I want to do is to extend it so that it would be able to properly handle logging of messages via threads. I have tried solutions like using message queues with an AnimationTimer. It works but it slows the GUI down.
I also tried using a scheduled service which runs a thread that reads messages from the message queue, concatenates them, and appends them to TextArea (textArea.appendText(stringBuilder.toString())
). The problem is that the TextArea control goes unstable i.e. you had to highlight all texts with Ctrl-A
and try resizing the window to make them appear well. There are also some of them being displayed in a light-blue background not sure what's causing it. My first guess here is that the race condition may not be allowing the control to update itself well from the new strings. It is also worth noting that the textarea is wrapped around a ScrollPane so it adds the confusion if TextArea is actually the one having the problem or ScrollPane. I have to mention as well that this approach doesn't make the TextArea control update itself with messages quickly.
I thought about binding
TextArea.TextProperty()
to something that does the update but I'm not sure how I would do that properly knowing that the gatherer of messages (be it by a service or a lone thread) would still be running different from the GUI thread.
I have tried to look up on other known logging framework solutions like log4j and some stuffs referred here but none of them seems to give an apparent approach to logging via threads to TextArea. I also don't like the idea of building my logging system on top of them as they already have their pre-defined mechanisms like logging level, etc.
I've seen this as well. It implies using SwingUtilities.invokeLater(Runnable)
to update the control but I already tried a similar approach using javafx.application.platform.runLater()
which gets executed on the worker thread. I'm not sure if there was something I was doing wrong but it just hangs. It can produce messages but not when they're aggressive enough. I estimate that the worker thread running in a purely synchronous fashion can actually produce about 20 or more average lines per second and more when it's in debug mode. A possible workaround would be to add message queueing to it as well but that doesn't make sense anymore.
log-view.css
LogViewer.java
The section below on selectable text is supplemental to the solution posted above. If you don't need selectable text, you can ignore the selection below.
There a few different options:
Try implementing the text selection approach which is appropriate for you and, if you can't get it to work, create a new question specific to selectable text logs, with a mcve.