Skip to content

Serial Monitor: improved "show timestamp" performance #7911

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

Merged
merged 3 commits into from
Aug 23, 2018
Merged
Changes from all commits
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
36 changes: 22 additions & 14 deletions app/src/processing/app/AbstractTextMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import javax.swing.Box;
import javax.swing.BoxLayout;
Expand All @@ -24,9 +25,7 @@
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Document;

import cc.arduino.packages.BoardPort;

Expand All @@ -48,7 +47,7 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {

public AbstractTextMonitor(BoardPort boardPort) {
super(boardPort);
logDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
logDateFormat = new SimpleDateFormat("HH:mm:ss.SSS -> ");
}

protected void onCreateWindow(Container mainPane) {
Expand Down Expand Up @@ -177,21 +176,30 @@ public void onSerialRateChange(ActionListener listener) {

public void message(final String s) {
SwingUtilities.invokeLater(new Runnable() {
// Pre-allocate all objects used for streaming data
Date t = new Date();
String now;
StringBuilder out = new StringBuilder(16384);
boolean isStartingLine = false;

public void run() {
if (addTimeStampBox.isSelected()) {
String[] lines = s.split("(?<=\\n)");
Document doc = textArea.getDocument();
for (String currentLine : lines) {
try {
if (doc.getLength() == 0 || ((int) doc.getText(doc.getLength() - 1, 1).charAt(0) == 10)) {
textArea.append(logDateFormat.format(new Date()) + " -> " + currentLine);
} else {
textArea.append(currentLine);
}
} catch (BadLocationException e) {
e.printStackTrace();
t.setTime(System.currentTimeMillis());
now = logDateFormat.format(t);
out.setLength(0);

StringTokenizer tokenizer = new StringTokenizer(s, "\n", true);
while (tokenizer.hasMoreTokens()) {
if (isStartingLine) {
out.append(now);
}
String token = tokenizer.nextToken();
out.append(token);
// tokenizer returns "\n" as a single token
isStartingLine = token.charAt(0) == '\n';
}

textArea.append(out.toString());
} else {
textArea.append(s);
}
Expand Down