Skip to content

Commit c2f3245

Browse files
nitram509cmaglie
authored andcommitted
Fix issue arduino#8055 missing timestamps on serial monitor
1 parent 994ce8d commit c2f3245

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

app/src/processing/app/AbstractTextMonitor.java

+53-40
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,18 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
4343
protected JComboBox lineEndings;
4444
protected JComboBox serialRates;
4545

46-
private SimpleDateFormat logDateFormat;
47-
4846
public AbstractTextMonitor(BoardPort boardPort) {
4947
super(boardPort);
50-
logDateFormat = new SimpleDateFormat("HH:mm:ss.SSS -> ");
5148
}
52-
49+
5350
protected void onCreateWindow(Container mainPane) {
5451
Font consoleFont = Theme.getFont("console.font");
5552
Font editorFont = PreferencesData.getFont("editor.font");
5653
Font font = Theme.scale(new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize()));
5754

5855
mainPane.setLayout(new BorderLayout());
5956

60-
textArea = new TextAreaFIFO(8000000);
57+
textArea = new TextAreaFIFO(8_000_000);
6158
textArea.setRows(16);
6259
textArea.setColumns(40);
6360
textArea.setEditable(false);
@@ -70,7 +67,7 @@ protected void onCreateWindow(Container mainPane) {
7067
scrollPane = new JScrollPane(textArea);
7168

7269
mainPane.add(scrollPane, BorderLayout.CENTER);
73-
70+
7471
JPanel upperPane = new JPanel();
7572
upperPane.setLayout(new BoxLayout(upperPane, BoxLayout.X_AXIS));
7673
upperPane.setBorder(new EmptyBorder(4, 4, 4, 4));
@@ -165,49 +162,65 @@ public void onSendCommand(ActionListener listener) {
165162
textField.addActionListener(listener);
166163
sendButton.addActionListener(listener);
167164
}
168-
165+
169166
public void onClearCommand(ActionListener listener) {
170167
clearButton.addActionListener(listener);
171168
}
172169

173170
public void onSerialRateChange(ActionListener listener) {
174171
serialRates.addActionListener(listener);
175172
}
176-
177-
public void message(final String s) {
178-
SwingUtilities.invokeLater(new Runnable() {
179-
// Pre-allocate all objects used for streaming data
180-
Date t = new Date();
181-
String now;
182-
StringBuilder out = new StringBuilder(16384);
183-
boolean isStartingLine = false;
184-
185-
public void run() {
186-
if (addTimeStampBox.isSelected()) {
187-
t.setTime(System.currentTimeMillis());
188-
now = logDateFormat.format(t);
189-
out.setLength(0);
190-
191-
StringTokenizer tokenizer = new StringTokenizer(s, "\n", true);
192-
while (tokenizer.hasMoreTokens()) {
193-
if (isStartingLine) {
194-
out.append(now);
195-
}
196-
String token = tokenizer.nextToken();
197-
out.append(token);
198-
// tokenizer returns "\n" as a single token
199-
isStartingLine = token.charAt(0) == '\n';
200-
}
201-
202-
textArea.append(out.toString());
203-
} else {
204-
textArea.append(s);
205-
}
206173

207-
if (autoscrollBox.isSelected()) {
208-
textArea.setCaretPosition(textArea.getDocument().getLength());
174+
public void message(final String msg) {
175+
SwingUtilities.invokeLater(new UpdateTextAreaAction(textArea,
176+
addTimeStampBox.isSelected(),
177+
autoscrollBox.isSelected(),
178+
msg));
179+
}
180+
181+
static class UpdateTextAreaAction implements Runnable {
182+
183+
private static final String LINE_SEPARATOR = "\n";
184+
185+
private String msg;
186+
private boolean addTimeStamp;
187+
private boolean doAutoscroll;
188+
private TextAreaFIFO textArea;
189+
190+
UpdateTextAreaAction(TextAreaFIFO textArea, boolean addTimeStamp,
191+
boolean doAutoscroll, String msg) {
192+
this.msg = msg;
193+
this.textArea = textArea;
194+
this.addTimeStamp = addTimeStamp;
195+
this.doAutoscroll = doAutoscroll;
196+
}
197+
198+
public void run() {
199+
if (addTimeStamp) {
200+
textArea.append(addTimestamps(msg));
201+
} else {
202+
textArea.append(msg);
203+
}
204+
if (doAutoscroll) {
205+
textArea.setCaretPosition(textArea.getDocument().getLength());
206+
}
207+
}
208+
209+
private String addTimestamps(String text) {
210+
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
211+
final StringBuilder sb = new StringBuilder(text.length() + now.length());
212+
boolean isStartingLine = true;
213+
StringTokenizer tokenizer = new StringTokenizer(text, LINE_SEPARATOR, true);
214+
while (tokenizer.hasMoreTokens()) {
215+
if (isStartingLine) {
216+
sb.append(now);
209217
}
218+
String token = tokenizer.nextToken();
219+
sb.append(token);
220+
// tokenizer returns "\n" as a single token
221+
isStartingLine = token.equals(LINE_SEPARATOR);
210222
}
211-
});
223+
return sb.toString();
224+
}
212225
}
213226
}

0 commit comments

Comments
 (0)