| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,7 +30,7 @@ | |||
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | 32 | /** | |
| 33 | - * Message console that sits below the editing area. | ||
| 33 | + * Non-GUI handling of System.out and System.err redirection. | ||
| 34 | 34 | * <p /> | |
| 35 | 35 | * Be careful when debugging this class, because if it's throwing exceptions, | |
| 36 | 36 | * don't take over System.err, and debug while watching just System.out | |
@@ -43,9 +43,6 @@ | |||
| 43 | 43 | * get along with one another. Use 'ant run' to work on encoding-related issues. | |
| 44 | 44 | */ | |
| 45 | 45 | public class Console { | |
| 46 | - // PrintStream sketchOut; | ||
| 47 | - // PrintStream sketchErr; | ||
| 48 | - | ||
| 49 | 46 | // Single static instance shared because there's only one real System.out. | |
| 50 | 47 | // Within the input handlers, the currentConsole variable will be used to | |
| 51 | 48 | // echo things to the correct location. | |
@@ -72,6 +69,13 @@ public class Console { | |||
| 72 | 69 | ||
| 73 | 70 | ||
| 74 | 71 | static public void startup() { | |
| 72 | + if (systemOut != null) { | ||
| 73 | + // TODO fix this dreadful style choice in how the Console is initialized | ||
| 74 | + // (This is not good code.. startup() should gracefully deal with this. | ||
| 75 | + // It's just a low priority relative to the likelihood of trouble.) | ||
| 76 | + new Exception("startup() called more than once").printStackTrace(systemErr); | ||
| 77 | + return; | ||
| 78 | + } | ||
| 75 | 79 | systemOut = System.out; | |
| 76 | 80 | systemErr = System.err; | |
| 77 | 81 | ||
@@ -115,8 +119,6 @@ public boolean accept(File file) { | |||
| 115 | 119 | File errFile = new File(consoleDir, stamp + ".err"); | |
| 116 | 120 | stderrFile = new FileOutputStream(errFile); | |
| 117 | 121 | ||
| 118 | - // consoleOut = new PrintStream(new EditorConsoleStream(false, null)); | ||
| 119 | - // consoleErr = new PrintStream(new EditorConsoleStream(true, null)); | ||
| 120 | 122 | consoleOut = new PrintStream(new ConsoleStream(false)); | |
| 121 | 123 | consoleErr = new PrintStream(new ConsoleStream(true)); | |
| 122 | 124 | ||
@@ -144,20 +146,14 @@ static public void setEditor(OutputStream out, OutputStream err) { | |||
| 144 | 146 | } | |
| 145 | 147 | ||
| 146 | 148 | ||
| 147 | - // public Console() { | ||
| 148 | - // sketchOut = new PrintStream(new EditorConsoleStream(false, this)); | ||
| 149 | - // sketchErr = new PrintStream(new EditorConsoleStream(true, this)); | ||
| 150 | - // } | ||
| 151 | - | ||
| 152 | - | ||
| 153 | - // public PrintStream getOut() { | ||
| 154 | - // return sketchOut; | ||
| 155 | - // } | ||
| 149 | + static public void systemOut(String what) { | ||
| 150 | + systemOut.println(what); | ||
| 151 | + } | ||
| 156 | 152 | ||
| 157 | 153 | ||
| 158 | - // public PrintStream getErr() { | ||
| 159 | - // return sketchErr; | ||
| 160 | - // } | ||
| 154 | + static public void systemErr(String what) { | ||
| 155 | + systemErr.println(what); | ||
| 156 | + } | ||
| 161 | 157 | ||
| 162 | 158 | ||
| 163 | 159 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,13 +7,13 @@ | |||
| 7 | 7 | */ | |
| 8 | 8 | /* | |
| 9 | 9 | * Copyright (c) 1997-2001 by Sun Microsystems, Inc. All Rights Reserved. | |
| 10 | - * | ||
| 10 | + * | ||
| 11 | 11 | * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, | |
| 12 | 12 | * modify and redistribute this software in source and binary code form, | |
| 13 | 13 | * provided that i) this copyright notice and license appear on all copies of | |
| 14 | 14 | * the software; and ii) Licensee does not utilize the software in a manner | |
| 15 | 15 | * which is disparaging to Sun. | |
| 16 | - * | ||
| 16 | + * | ||
| 17 | 17 | * This software is provided "AS IS," without a warranty of any kind. ALL | |
| 18 | 18 | * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY | |
| 19 | 19 | * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR | |
@@ -25,7 +25,7 @@ | |||
| 25 | 25 | * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF | |
| 26 | 26 | * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE | |
| 27 | 27 | * POSSIBILITY OF SUCH DAMAGES. | |
| 28 | - * | ||
| 28 | + * | ||
| 29 | 29 | * This software is not designed or intended for use in on-line control of | |
| 30 | 30 | * aircraft, air traffic, aircraft navigation or aircraft communications; or in | |
| 31 | 31 | * the design, construction, operation or maintenance of any nuclear | |
@@ -50,7 +50,7 @@ public class StreamRedirectThread extends Thread { | |||
| 50 | 50 | ||
| 51 | 51 | private static final int BUFFER_SIZE = 2048; | |
| 52 | 52 | ||
| 53 | - | ||
| 53 | + | ||
| 54 | 54 | /** | |
| 55 | 55 | * Set up for copy. | |
| 56 | 56 | * @param name Name of the thread | |
@@ -63,15 +63,15 @@ public StreamRedirectThread(String name, InputStream in, OutputStream out) { | |||
| 63 | 63 | this.out = new OutputStreamWriter(out); | |
| 64 | 64 | setPriority(Thread.MAX_PRIORITY-1); | |
| 65 | 65 | } | |
| 66 | - | ||
| 67 | - | ||
| 66 | + | ||
| 67 | + | ||
| 68 | 68 | public StreamRedirectThread(String name, Reader in, Writer out) { | |
| 69 | 69 | super(name); | |
| 70 | 70 | this.in = in; | |
| 71 | 71 | this.out = out; | |
| 72 | 72 | setPriority(Thread.MAX_PRIORITY-1); | |
| 73 | 73 | } | |
| 74 | - | ||
| 74 | + | ||
| 75 | 75 | ||
| 76 | 76 | /** | |
| 77 | 77 | * Copy. | |
@@ -80,16 +80,15 @@ public void run() { | |||
| 80 | 80 | try { | |
| 81 | 81 | char[] cbuf = new char[BUFFER_SIZE]; | |
| 82 | 82 | int count; | |
| 83 | - //System.out.println("opening streamredirectthread"); | ||
| 84 | 83 | while ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) { | |
| 85 | 84 | out.write(cbuf, 0, count); | |
| 86 | 85 | // had to add the flush() here.. maybe shouldn't be using writer? [fry] | |
| 87 | 86 | out.flush(); | |
| 88 | 87 | } | |
| 89 | - //System.out.println("exiting streamredirectthread"); | ||
| 90 | 88 | out.flush(); | |
| 91 | - } catch(IOException exc) { | ||
| 92 | - System.err.println("Child I/O Transfer - " + exc); | ||
| 89 | + | ||
| 90 | + } catch (IOException exc) { | ||
| 91 | + processing.app.Console.systemErr("Child I/O Transfer - " + exc); | ||
| 93 | 92 | } | |
| 94 | 93 | } | |
| 95 | 94 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,8 +32,7 @@ | |||
| 32 | 32 | import java.awt.event.ActionListener; | |
| 33 | 33 | import java.io.OutputStream; | |
| 34 | 34 | import java.io.PrintStream; | |
| 35 | - import java.util.ArrayList; | ||
| 36 | - import java.util.List; | ||
| 35 | + import java.util.concurrent.LinkedBlockingQueue; | ||
| 37 | 36 | ||
| 38 | 37 | import javax.swing.*; | |
| 39 | 38 | import javax.swing.border.MatteBorder; | |
@@ -46,16 +45,6 @@ | |||
| 46 | 45 | ||
| 47 | 46 | /** | |
| 48 | 47 | * Message console that sits below the editing area. | |
| 49 | - * <p /> | ||
| 50 | - * Be careful when debugging this class, because if it's throwing exceptions, | ||
| 51 | - * don't take over System.err, and debug while watching just System.out | ||
| 52 | - * or just call println() or whatever directly to systemOut or systemErr. | ||
| 53 | - * <p /> | ||
| 54 | - * Also note that encodings will not work properly when run from Eclipse. This | ||
| 55 | - * means that if you use non-ASCII characters in a println() or some such, | ||
| 56 | - * the characters won't print properly in the Processing and/or Eclipse console. | ||
| 57 | - * It seems that Eclipse's console-grabbing and that of Processing don't | ||
| 58 | - * get along with one another. Use 'ant run' to work on encoding-related issues. | ||
| 59 | 48 | */ | |
| 60 | 49 | public class EditorConsole extends JScrollPane { | |
| 61 | 50 | Editor editor; | |
@@ -69,6 +58,7 @@ public class EditorConsole extends JScrollPane { | |||
| 69 | 58 | MutableAttributeSet errStyle; | |
| 70 | 59 | ||
| 71 | 60 | int maxLineCount; | |
| 61 | + int maxCharCount; | ||
| 72 | 62 | ||
| 73 | 63 | PrintStream sketchOut; | |
| 74 | 64 | PrintStream sketchErr; | |
@@ -79,9 +69,10 @@ public class EditorConsole extends JScrollPane { | |||
| 79 | 69 | public EditorConsole(Editor editor) { | |
| 80 | 70 | this.editor = editor; | |
| 81 | 71 | ||
| 82 | - maxLineCount = Preferences.getInteger("console.length"); | ||
| 72 | + maxLineCount = Preferences.getInteger("console.lines"); | ||
| 73 | + maxCharCount = Preferences.getInteger("console.chars"); | ||
| 83 | 74 | ||
| 84 | - consoleDoc = new BufferedStyledDocument(10000, maxLineCount); | ||
| 75 | + consoleDoc = new BufferedStyledDocument(10000, maxLineCount, maxCharCount); | ||
| 85 | 76 | consoleTextPane = new JTextPane(consoleDoc); | |
| 86 | 77 | consoleTextPane.setEditable(false); | |
| 87 | 78 | ||
@@ -98,7 +89,7 @@ public EditorConsole(Editor editor) { | |||
| 98 | 89 | ||
| 99 | 90 | protected void flush() { | |
| 100 | 91 | // only if new text has been added | |
| 101 | - if (consoleDoc.hasAppendage) { | ||
| 92 | + if (consoleDoc.hasAppendage()) { | ||
| 102 | 93 | // insert the text that's been added in the meantime | |
| 103 | 94 | consoleDoc.insertAll(); | |
| 104 | 95 | // always move to the end of the text as it's added | |
@@ -150,8 +141,7 @@ public PrintStream getErr() { | |||
| 150 | 141 | */ | |
| 151 | 142 | protected void updateAppearance() { | |
| 152 | 143 | String fontFamily = Preferences.get("editor.font.family"); | |
| 153 | - int fontSize = | ||
| 154 | - Toolkit.zoom(Preferences.getInteger("console.font.size")); | ||
| 144 | + int fontSize = Toolkit.zoom(Preferences.getInteger("console.font.size")); | ||
| 155 | 145 | StyleConstants.setFontFamily(stdStyle, fontFamily); | |
| 156 | 146 | StyleConstants.setFontSize(stdStyle, fontSize); | |
| 157 | 147 | StyleConstants.setFontFamily(errStyle, fontFamily); | |
@@ -232,7 +222,7 @@ void setCurrent() { | |||
| 232 | 222 | } | |
| 233 | 223 | ||
| 234 | 224 | ||
| 235 | - synchronized public void message(String what, boolean err) { | ||
| 225 | + public void message(String what, boolean err) { | ||
| 236 | 226 | if (err && (what.contains("invalid context 0x0") || (what.contains("invalid drawable")))) { | |
| 237 | 227 | // Respectfully declining... This is a quirk of more recent releases of | |
| 238 | 228 | // Java on Mac OS X, but is widely reported as the source of any other | |
@@ -302,22 +292,32 @@ public void write(int b) { | |||
| 302 | 292 | * swing event thread, so they need to be synchronized | |
| 303 | 293 | */ | |
| 304 | 294 | class BufferedStyledDocument extends DefaultStyledDocument { | |
| 305 | - List<ElementSpec> elements = new ArrayList<>(); | ||
| 306 | - int maxLineLength, maxLineCount; | ||
| 295 | + //List<ElementSpec> elements = new ArrayList<>(); | ||
| 296 | + LinkedBlockingQueue<ElementSpec> elements; | ||
| 297 | + // AtomicInteger queuedLineCount = new AtomicInteger(); | ||
| 298 | + int maxLineLength, maxLineCount, maxCharCount; | ||
| 307 | 299 | int currentLineLength = 0; | |
| 308 | 300 | boolean needLineBreak = false; | |
| 309 | - boolean hasAppendage = false; | ||
| 301 | + // boolean hasAppendage = false; | ||
| 302 | + final Object insertLock = new Object(); | ||
| 310 | 303 | ||
| 311 | - public BufferedStyledDocument(int maxLineLength, int maxLineCount) { | ||
| 304 | + public BufferedStyledDocument(int maxLineLength, int maxLineCount, | ||
| 305 | + int maxCharCount) { | ||
| 312 | 306 | this.maxLineLength = maxLineLength; | |
| 313 | 307 | this.maxLineCount = maxLineCount; | |
| 308 | + this.maxCharCount = maxCharCount; | ||
| 309 | + elements = new LinkedBlockingQueue<>(); | ||
| 310 | + } | ||
| 311 | + | ||
| 312 | + // monitor this so that it's only updated when needed (otherwise console | ||
| 313 | + // updates every 250 ms when an app isn't even running.. see bug 180) | ||
| 314 | + public boolean hasAppendage() { | ||
| 315 | + return elements.size() > 0; | ||
| 314 | 316 | } | |
| 315 | 317 | ||
| 316 | 318 | /** buffer a string for insertion at the end of the DefaultStyledDocument */ | |
| 317 | - public synchronized void appendString(String str, AttributeSet a) { | ||
| 318 | - // do this so that it's only updated when needed (otherwise console | ||
| 319 | - // updates every 250 ms when an app isn't even running.. see bug 180) | ||
| 320 | - hasAppendage = true; | ||
| 319 | + public void appendString(String str, AttributeSet a) { | ||
| 320 | + // hasAppendage = true; | ||
| 321 | 321 | ||
| 322 | 322 | // process each line of the string | |
| 323 | 323 | while (str.length() > 0) { | |
@@ -326,6 +326,7 @@ public synchronized void appendString(String str, AttributeSet a) { | |||
| 326 | 326 | if (needLineBreak || currentLineLength > maxLineLength) { | |
| 327 | 327 | elements.add(new ElementSpec(a, ElementSpec.EndTagType)); | |
| 328 | 328 | elements.add(new ElementSpec(a, ElementSpec.StartTagType)); | |
| 329 | + // queuedLineCount.incrementAndGet(); | ||
| 329 | 330 | currentLineLength = 0; | |
| 330 | 331 | } | |
| 331 | 332 | ||
@@ -341,15 +342,42 @@ public synchronized void appendString(String str, AttributeSet a) { | |||
| 341 | 342 | needLineBreak = true; | |
| 342 | 343 | str = str.substring(str.indexOf('\n') + 1); // eat the line | |
| 343 | 344 | } | |
| 345 | + /* | ||
| 346 | + while (queuedLineCount.get() > maxLineCount) { | ||
| 347 | + Console.systemOut("too many: " + queuedLineCount); | ||
| 348 | + ElementSpec elem = elements.remove(); | ||
| 349 | + if (elem.getType() == ElementSpec.EndTagType) { | ||
| 350 | + queuedLineCount.decrementAndGet(); | ||
| 351 | + } | ||
| 352 | + } | ||
| 353 | + */ | ||
| 354 | + } | ||
| 355 | + if (elements.size() > 1000) { | ||
| 356 | + insertAll(); | ||
| 344 | 357 | } | |
| 345 | 358 | } | |
| 346 | 359 | ||
| 347 | 360 | /** insert the buffered strings */ | |
| 348 | - public synchronized void insertAll() { | ||
| 349 | - ElementSpec[] elementArray = new ElementSpec[elements.size()]; | ||
| 350 | - elements.toArray(elementArray); | ||
| 361 | + public void insertAll() { | ||
| 362 | + /* | ||
| 363 | + // each line is ~3 elements | ||
| 364 | + int tooMany = elements.size() - maxLineCount*3; | ||
| 365 | + if (tooMany > 0) { | ||
| 366 | + try { | ||
| 367 | + remove(0, getLength()); // clear the document first | ||
| 368 | + } catch (BadLocationException ble) { | ||
| 369 | + ble.printStackTrace(); | ||
| 370 | + } | ||
| 371 | + Console.systemOut("skipping " + elements.size()); | ||
| 372 | + for (int i = 0; i < tooMany; i++) { | ||
| 373 | + elements.remove(); | ||
| 374 | + } | ||
| 375 | + } | ||
| 376 | + */ | ||
| 377 | + ElementSpec[] elementArray = elements.toArray(new ElementSpec[0]); | ||
| 351 | 378 | ||
| 352 | 379 | try { | |
| 380 | + /* | ||
| 353 | 381 | // check how many lines have been used so far | |
| 354 | 382 | // if too many, shave off a few lines from the beginning | |
| 355 | 383 | Element element = super.getDefaultRootElement(); | |
@@ -366,13 +394,42 @@ public synchronized void insertAll() { | |||
| 366 | 394 | // remove to the end of the 200th line | |
| 367 | 395 | super.remove(0, endOffset); | |
| 368 | 396 | } | |
| 369 | - super.insert(super.getLength(), elementArray); | ||
| 397 | + */ | ||
| 398 | + synchronized (insertLock) { | ||
| 399 | + checkLength(); | ||
| 400 | + insert(getLength(), elementArray); | ||
| 401 | + checkLength(); | ||
| 402 | + } | ||
| 370 | 403 | ||
| 371 | 404 | } catch (BadLocationException e) { | |
| 372 | 405 | // ignore the error otherwise this will cause an infinite loop | |
| 373 | 406 | // maybe not a good idea in the long run? | |
| 374 | 407 | } | |
| 375 | 408 | elements.clear(); | |
| 376 | - hasAppendage = false; | ||
| 409 | + // hasAppendage = false; | ||
| 410 | + } | ||
| 411 | + | ||
| 412 | + private void checkLength() throws BadLocationException { | ||
| 413 | + // set a limit on the number of characters in the console | ||
| 414 | + int docLength = getLength(); | ||
| 415 | + if (docLength > maxCharCount) { | ||
| 416 | + remove(0, docLength - maxCharCount); | ||
| 417 | + } | ||
| 418 | + // check how many lines have been used so far | ||
| 419 | + // if too many, shave off a few lines from the beginning | ||
| 420 | + Element element = super.getDefaultRootElement(); | ||
| 421 | + int lineCount = element.getElementCount(); | ||
| 422 | + int overage = lineCount - maxLineCount; | ||
| 423 | + if (overage > 0) { | ||
| 424 | + // if 1200 lines, and 1000 lines is max, | ||
| 425 | + // find the position of the end of the 200th line | ||
| 426 | + //systemOut.println("overage is " + overage); | ||
| 427 | + Element lineElement = element.getElement(overage); | ||
| 428 | + if (lineElement != null) { | ||
| 429 | + int endOffset = lineElement.getEndOffset(); | ||
| 430 | + // remove to the end of the 200th line | ||
| 431 | + super.remove(0, endOffset); | ||
| 432 | + } | ||
| 433 | + } | ||
| 377 | 434 | } | |
| 378 | 435 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -184,7 +184,8 @@ console.auto_clear = true | |||
| 184 | 184 | ||
| 185 | 185 | # set the maximum number of lines remembered by the console | |
| 186 | 186 | # the default is 500, lengthen at your own peril | |
| 187 | - console.length = 500 | ||
| 187 | + console.lines = 500 | ||
| 188 | + console.chars = 40000 | ||
| 188 | 189 | ||
| 189 | 190 | # Any additional Java options when running. | |
| 190 | 191 | # If you change this and can't run things, it's your own durn fault. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,10 @@ | |||
| 1 | 1 | 0261 (3.3.4 or 3.4) | |
| 2 | + X redo console handling to not use Timer, fixing freeze-up problems | ||
| 3 | + o https://github.com/processing/processing/pull/4935 | ||
| 4 | + X https://github.com/processing/processing/issues/4825 | ||
| 5 | + | ||
| 6 | + | ||
| 7 | + _ should ant run launch the .app so that launchsvcs stuff works properly? | ||
| 2 | 8 | ||
| 3 | 9 | _ sketch.properties not being written if initial mode is p5.js? | |
| 4 | 10 | _ what to double-click when opening p5 projects | |
@@ -39,9 +45,6 @@ _ super easy given current code implementation, might help usability | |||
| 39 | 45 | ||
| 40 | 46 | ||
| 41 | 47 | contrib | |
| 42 | - _ redo console handling to not use Timer, fixing freeze-up problems | ||
| 43 | - _ https://github.com/processing/processing/pull/4935 | ||
| 44 | - _ https://github.com/processing/processing/issues/4825 | ||
| 45 | 48 | _ Make the change detector not reload the sketch | |
| 46 | 49 | _ https://github.com/processing/processing/pull/5021 | |
| 47 | 50 | _ https://github.com/processing/processing/issues/4713 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments