1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-31 20:52:13 +01:00

Fix issue #8055 missing timestamps on serial monitor

This commit is contained in:
nitram509 2018-10-11 12:36:35 +02:00 committed by Cristian Maglie
parent 994ce8d21d
commit c2f324508f

View File

@ -43,11 +43,8 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
protected JComboBox lineEndings; protected JComboBox lineEndings;
protected JComboBox serialRates; protected JComboBox serialRates;
private SimpleDateFormat logDateFormat;
public AbstractTextMonitor(BoardPort boardPort) { public AbstractTextMonitor(BoardPort boardPort) {
super(boardPort); super(boardPort);
logDateFormat = new SimpleDateFormat("HH:mm:ss.SSS -> ");
} }
protected void onCreateWindow(Container mainPane) { protected void onCreateWindow(Container mainPane) {
@ -57,7 +54,7 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
mainPane.setLayout(new BorderLayout()); mainPane.setLayout(new BorderLayout());
textArea = new TextAreaFIFO(8000000); textArea = new TextAreaFIFO(8_000_000);
textArea.setRows(16); textArea.setRows(16);
textArea.setColumns(40); textArea.setColumns(40);
textArea.setEditable(false); textArea.setEditable(false);
@ -174,40 +171,56 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
serialRates.addActionListener(listener); serialRates.addActionListener(listener);
} }
public void message(final String s) { public void message(final String msg) {
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new UpdateTextAreaAction(textArea,
// Pre-allocate all objects used for streaming data addTimeStampBox.isSelected(),
Date t = new Date(); autoscrollBox.isSelected(),
String now; msg));
StringBuilder out = new StringBuilder(16384); }
boolean isStartingLine = false;
static class UpdateTextAreaAction implements Runnable {
private static final String LINE_SEPARATOR = "\n";
private String msg;
private boolean addTimeStamp;
private boolean doAutoscroll;
private TextAreaFIFO textArea;
UpdateTextAreaAction(TextAreaFIFO textArea, boolean addTimeStamp,
boolean doAutoscroll, String msg) {
this.msg = msg;
this.textArea = textArea;
this.addTimeStamp = addTimeStamp;
this.doAutoscroll = doAutoscroll;
}
public void run() { public void run() {
if (addTimeStampBox.isSelected()) { if (addTimeStamp) {
t.setTime(System.currentTimeMillis()); textArea.append(addTimestamps(msg));
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 { } else {
textArea.append(s); textArea.append(msg);
} }
if (doAutoscroll) {
if (autoscrollBox.isSelected()) {
textArea.setCaretPosition(textArea.getDocument().getLength()); textArea.setCaretPosition(textArea.getDocument().getLength());
} }
} }
});
private String addTimestamps(String text) {
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
final StringBuilder sb = new StringBuilder(text.length() + now.length());
boolean isStartingLine = true;
StringTokenizer tokenizer = new StringTokenizer(text, LINE_SEPARATOR, true);
while (tokenizer.hasMoreTokens()) {
if (isStartingLine) {
sb.append(now);
}
String token = tokenizer.nextToken();
sb.append(token);
// tokenizer returns "\n" as a single token
isStartingLine = token.equals(LINE_SEPARATOR);
}
return sb.toString();
}
} }
} }