From aaebb0a4d63573acb4ac42e3436d5d3d9d611364 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Fri, 27 Nov 2015 15:06:40 +0100 Subject: [PATCH] Stored some regexps in static finals, given names to threads, and slightly optimized ConsoleOutputStream --- app/src/cc/arduino/ConsoleOutputStream.java | 41 ++++++------------- .../libraries/ui/LibraryManagerUI.java | 3 ++ .../packages/ui/ContributionManagerUI.java | 3 ++ app/src/processing/app/Base.java | 4 +- arduino-core/src/cc/arduino/Compiler.java | 7 +++- .../i18n/ExternalProcessOutputParser.java | 5 ++- .../cc/arduino/packages/DiscoveryManager.java | 16 ++++---- .../src/processing/app/BaseNoGui.java | 4 +- .../processing/app/debug/MessageSiphon.java | 1 + .../src/processing/app/legacy/PApplet.java | 6 ++- 10 files changed, 47 insertions(+), 43 deletions(-) diff --git a/app/src/cc/arduino/ConsoleOutputStream.java b/app/src/cc/arduino/ConsoleOutputStream.java index bfcea7839..164c825ed 100644 --- a/app/src/cc/arduino/ConsoleOutputStream.java +++ b/app/src/cc/arduino/ConsoleOutputStream.java @@ -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 } diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java index 44ed6bd69..988ffa0ab 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java @@ -207,6 +207,7 @@ public class LibraryManagerUI extends InstallerJDialog { setProgressVisible(false, ""); } }); + installerThread.setName("LibraryManager Update Thread"); installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage)); installerThread.start(); } @@ -225,6 +226,7 @@ public class LibraryManagerUI extends InstallerJDialog { setProgressVisible(false, ""); } }); + installerThread.setName("LibraryManager Installer Thread"); installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage)); installerThread.start(); } @@ -252,6 +254,7 @@ public class LibraryManagerUI extends InstallerJDialog { setProgressVisible(false, ""); } }); + installerThread.setName("LibraryManager Remove Thread"); installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage)); installerThread.start(); } diff --git a/app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java b/app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java index 18dadeb9e..307c02cf6 100644 --- a/app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java +++ b/app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java @@ -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(); } diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index ad7540d17..f7af1aa33 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -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(); diff --git a/arduino-core/src/cc/arduino/Compiler.java b/arduino-core/src/cc/arduino/Compiler.java index 71b601190..82b91c8c4 100644 --- a/arduino-core/src/cc/arduino/Compiler.java +++ b/arduino-core/src/cc/arduino/Compiler.java @@ -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 = ""; diff --git a/arduino-core/src/cc/arduino/i18n/ExternalProcessOutputParser.java b/arduino-core/src/cc/arduino/i18n/ExternalProcessOutputParser.java index 0bd4d2bc6..eb0c4e1de 100644 --- a/arduino-core/src/cc/arduino/i18n/ExternalProcessOutputParser.java +++ b/arduino-core/src/cc/arduino/i18n/ExternalProcessOutputParser.java @@ -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 parse(String s) { if (!s.startsWith("===")) { throw new IllegalArgumentException(s); @@ -45,7 +48,7 @@ public class ExternalProcessOutputParser { Map output = new HashMap<>(); - String[] parts = s.split(" \\|\\|\\| "); + String[] parts = SPLIT.split(s); output.put("msg", parts[0]); output.put("args", parseArgs(parts[1])); diff --git a/arduino-core/src/cc/arduino/packages/DiscoveryManager.java b/arduino-core/src/cc/arduino/packages/DiscoveryManager.java index b8ea88b65..add7d0671 100644 --- a/arduino-core/src/cc/arduino/packages/DiscoveryManager.java +++ b/arduino-core/src/cc/arduino/packages/DiscoveryManager.java @@ -56,18 +56,16 @@ public class DiscoveryManager { } } - Thread closeHook = new Thread(new Runnable() { - @Override - public void run() { - for (Discovery d : discoverers) { - try { - d.stop(); - } catch (Exception e) { - e.printStackTrace(); //just printing as the JVM is terminating - } + Thread closeHook = new Thread(() -> { + for (Discovery d : discoverers) { + try { + d.stop(); + } catch (Exception e) { + e.printStackTrace(); //just printing as the JVM is terminating } } }); + closeHook.setName("DiscoveryManager closeHook"); Runtime.getRuntime().addShutdownHook(closeHook); } diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index e8e72c66c..b39eb3a13 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -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(); diff --git a/arduino-core/src/processing/app/debug/MessageSiphon.java b/arduino-core/src/processing/app/debug/MessageSiphon.java index c9abf0ea7..ff9970579 100644 --- a/arduino-core/src/processing/app/debug/MessageSiphon.java +++ b/arduino-core/src/processing/app/debug/MessageSiphon.java @@ -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); diff --git a/arduino-core/src/processing/app/legacy/PApplet.java b/arduino-core/src/processing/app/legacy/PApplet.java index 87c0fb477..071247f55 100644 --- a/arduino-core/src/processing/app/legacy/PApplet.java +++ b/arduino-core/src/processing/app/legacy/PApplet.java @@ -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];