1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19: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,13 +43,10 @@ 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) {
Font consoleFont = Theme.getFont("console.font"); Font consoleFont = Theme.getFont("console.font");
Font editorFont = PreferencesData.getFont("editor.font"); Font editorFont = PreferencesData.getFont("editor.font");
@ -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);
@ -70,7 +67,7 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
scrollPane = new JScrollPane(textArea); scrollPane = new JScrollPane(textArea);
mainPane.add(scrollPane, BorderLayout.CENTER); mainPane.add(scrollPane, BorderLayout.CENTER);
JPanel upperPane = new JPanel(); JPanel upperPane = new JPanel();
upperPane.setLayout(new BoxLayout(upperPane, BoxLayout.X_AXIS)); upperPane.setLayout(new BoxLayout(upperPane, BoxLayout.X_AXIS));
upperPane.setBorder(new EmptyBorder(4, 4, 4, 4)); upperPane.setBorder(new EmptyBorder(4, 4, 4, 4));
@ -165,7 +162,7 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
textField.addActionListener(listener); textField.addActionListener(listener);
sendButton.addActionListener(listener); sendButton.addActionListener(listener);
} }
public void onClearCommand(ActionListener listener) { public void onClearCommand(ActionListener listener) {
clearButton.addActionListener(listener); clearButton.addActionListener(listener);
} }
@ -173,41 +170,57 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
public void onSerialRateChange(ActionListener listener) { public void onSerialRateChange(ActionListener listener) {
serialRates.addActionListener(listener); serialRates.addActionListener(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() { public void message(final String msg) {
if (addTimeStampBox.isSelected()) { SwingUtilities.invokeLater(new UpdateTextAreaAction(textArea,
t.setTime(System.currentTimeMillis()); addTimeStampBox.isSelected(),
now = logDateFormat.format(t); autoscrollBox.isSelected(),
out.setLength(0); msg));
}
StringTokenizer tokenizer = new StringTokenizer(s, "\n", true); static class UpdateTextAreaAction implements Runnable {
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()); private static final String LINE_SEPARATOR = "\n";
} else {
textArea.append(s);
}
if (autoscrollBox.isSelected()) { private String msg;
textArea.setCaretPosition(textArea.getDocument().getLength()); 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() {
if (addTimeStamp) {
textArea.append(addTimestamps(msg));
} else {
textArea.append(msg);
} }
}); if (doAutoscroll) {
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();
}
} }
} }