1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-19 08:52:15 +01:00

Stored some regexps in static finals, given names to threads, and slightly

optimized ConsoleOutputStream
This commit is contained in:
Federico Fissore 2015-11-27 15:06:40 +01:00
parent 53af4a7dea
commit aaebb0a4d6
10 changed files with 47 additions and 43 deletions

View File

@ -54,18 +54,20 @@ public class ConsoleOutputStream extends ByteArrayOutputStream {
private final SimpleAttributeSet attributes;
private final PrintStream printStream;
private final StringBuilder buffer;
private final Timer timer;
private volatile EditorConsole editorConsole;
private volatile boolean newLinePrinted;
public ConsoleOutputStream(SimpleAttributeSet attributes, PrintStream printStream) {
this.attributes = attributes;
this.printStream = printStream;
this.buffer = new StringBuilder();
this.newLinePrinted = false;
this.timer = new Timer(100, (e) -> {
if (editorConsole != null) {
this.timer = new Timer(500, (e) -> {
if (editorConsole != null && newLinePrinted) {
editorConsole.scrollDown();
newLinePrinted = false;
}
});
timer.setRepeats(false);
@ -76,41 +78,24 @@ public class ConsoleOutputStream extends ByteArrayOutputStream {
}
public synchronized void flush() {
String message = toString();
String text = toString();
if (message.length() == 0) {
if (text.length() == 0) {
return;
}
handleAppend(message);
printStream.print(text);
printInConsole(text);
reset();
}
private void handleAppend(String message) {
resetBufferIfDocumentEmpty();
buffer.append(message);
clearBuffer();
}
private void resetBufferIfDocumentEmpty() {
if (editorConsole != null && editorConsole.isEmpty()) {
buffer.setLength(0);
}
}
private void clearBuffer() {
String line = buffer.toString();
buffer.setLength(0);
printStream.print(line);
private void printInConsole(String text) {
newLinePrinted = newLinePrinted || text.contains("\n");
if (editorConsole != null) {
SwingUtilities.invokeLater(() -> {
try {
editorConsole.insertString(line, attributes);
editorConsole.insertString(text, attributes);
} catch (BadLocationException ble) {
//ignore
}

View File

@ -207,6 +207,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
setProgressVisible(false, "");
}
});
installerThread.setName("LibraryManager Update Thread");
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
installerThread.start();
}
@ -225,6 +226,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
setProgressVisible(false, "");
}
});
installerThread.setName("LibraryManager Installer Thread");
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
installerThread.start();
}
@ -252,6 +254,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
setProgressVisible(false, "");
}
});
installerThread.setName("LibraryManager Remove Thread");
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
installerThread.start();
}

View File

@ -147,6 +147,7 @@ public class ContributionManagerUI extends InstallerJDialog {
setProgressVisible(false, "");
}
});
installerThread.setName("ContributionManager Update Thread");
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
installerThread.start();
}
@ -171,6 +172,7 @@ public class ContributionManagerUI extends InstallerJDialog {
}
}
});
installerThread.setName("ContributionManager Install Thread");
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
installerThread.start();
}
@ -196,6 +198,7 @@ public class ContributionManagerUI extends InstallerJDialog {
setProgressVisible(false, "");
}
});
installerThread.setName("ContributionManager Remove Thread");
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
installerThread.start();
}

View File

@ -141,7 +141,9 @@ public class Base {
}
static public void guardedMain(String args[]) throws Exception {
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
Thread deleteFilesOnShutdownThread = new Thread(DeleteFilesOnShutdown.INSTANCE);
deleteFilesOnShutdownThread.setName("DeleteFilesOnShutdown");
Runtime.getRuntime().addShutdownHook(deleteFilesOnShutdownThread);
BaseNoGui.initLogger();

View File

@ -51,6 +51,7 @@ import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -105,6 +106,8 @@ public class Compiler implements MessageConsumer {
}
}
private static final Pattern ERROR_FORMAT = Pattern.compile("(.+\\.\\w+):(\\d+)(:\\d+)*:\\s*error:\\s*(.*)\\s*", Pattern.MULTILINE | Pattern.DOTALL);
private final String pathToSketch;
private final SketchData sketch;
private final String buildPath;
@ -427,6 +430,7 @@ public class Compiler implements MessageConsumer {
@Override
protected Thread createPump(InputStream is, OutputStream os, boolean closeWhenExhausted) {
final Thread result = new Thread(new MyStreamPumper(is, Compiler.this));
result.setName("MyStreamPumper Thread");
result.setDaemon(true);
return result;
@ -501,8 +505,7 @@ public class Compiler implements MessageConsumer {
}
}
String errorFormat = "(.+\\.\\w+):(\\d+)(:\\d+)*:\\s*error:\\s*(.*)\\s*";
String[] pieces = PApplet.match(s, errorFormat);
String[] pieces = PApplet.match(s, ERROR_FORMAT);
if (pieces != null) {
String error = pieces[pieces.length - 1], msg = "";

View File

@ -32,10 +32,13 @@ package cc.arduino.i18n;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class ExternalProcessOutputParser {
private static final Pattern SPLIT = Pattern.compile(" \\|\\|\\| ");
public Map<String, Object> parse(String s) {
if (!s.startsWith("===")) {
throw new IllegalArgumentException(s);
@ -45,7 +48,7 @@ public class ExternalProcessOutputParser {
Map<String, Object> output = new HashMap<>();
String[] parts = s.split(" \\|\\|\\| ");
String[] parts = SPLIT.split(s);
output.put("msg", parts[0]);
output.put("args", parseArgs(parts[1]));

View File

@ -56,9 +56,7 @@ public class DiscoveryManager {
}
}
Thread closeHook = new Thread(new Runnable() {
@Override
public void run() {
Thread closeHook = new Thread(() -> {
for (Discovery d : discoverers) {
try {
d.stop();
@ -66,8 +64,8 @@ public class DiscoveryManager {
e.printStackTrace(); //just printing as the JVM is terminating
}
}
}
});
closeHook.setName("DiscoveryManager closeHook");
Runtime.getRuntime().addShutdownHook(closeHook);
}

View File

@ -757,7 +757,9 @@ public class BaseNoGui {
}
System.setProperty("java.net.useSystemProxies", "true");
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
Thread deleteFilesOnShutdownThread = new Thread(DeleteFilesOnShutdown.INSTANCE);
deleteFilesOnShutdownThread.setName("DeleteFilesOnShutdown");
Runtime.getRuntime().addShutdownHook(deleteFilesOnShutdownThread);
initPlatform();

View File

@ -54,6 +54,7 @@ public class MessageSiphon implements Runnable {
this.lineTimeout = lineTimeout;
thread = new Thread(this);
thread.setName("MessageSiphon");
// don't set priority too low, otherwise exceptions won't
// bubble up in time (i.e. compile errors have a weird delay)
//thread.setPriority(Thread.MIN_PRIORITY);

View File

@ -497,7 +497,11 @@ public class PApplet {
*/
static public String[] match(String what, String regexp) {
Pattern p = Pattern.compile(regexp, Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = p.matcher(what);
return match(what, p);
}
static public String[] match(String what, Pattern pattern) {
Matcher m = pattern.matcher(what);
if (m.find()) {
int count = m.groupCount() + 1;
String[] groups = new String[count];