Skip to content

Commit a1e5271

Browse files
author
Federico Fissore
committed
EditorConsoleStream: got rid of "console" folder. Doesn't work for debugging, since blocking problems happen at startup, when that "console" is not yet initialized
1 parent fbb61ff commit a1e5271

File tree

1 file changed

+18
-102
lines changed

1 file changed

+18
-102
lines changed

app/src/processing/app/EditorConsoleStream.java

+18-102
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,38 @@
11
package processing.app;
22

3-
import cc.arduino.files.DeleteFilesOnShutdown;
4-
import org.apache.commons.compress.utils.IOUtils;
5-
6-
import static processing.app.I18n._;
7-
8-
import java.io.File;
9-
import java.io.FileOutputStream;
10-
import java.io.IOException;
113
import java.io.OutputStream;
124
import java.io.PrintStream;
135

146
class EditorConsoleStream extends OutputStream {
15-
static File tempFolder;
16-
static File outFile;
17-
static File errFile;
18-
19-
static EditorConsole currentConsole;
207

21-
static OutputStream stderrFile;
22-
static OutputStream stdoutFile;
23-
static PrintStream consoleErr;
24-
static PrintStream consoleOut;
25-
static public PrintStream systemErr;
26-
static public PrintStream systemOut;
8+
private static EditorConsole currentConsole;
9+
private static PrintStream systemErr;
10+
private static PrintStream systemOut;
2711

2812
public static void init() {
2913
if (systemOut == null) {
3014
systemOut = System.out;
3115
systemErr = System.err;
3216

33-
// Create a temporary folder which will have a randomized name. Has to
34-
// be randomized otherwise another instance of Processing (or one of its
35-
// sister IDEs) might collide with the file causing permissions problems.
36-
// The files and folders are not deleted on exit because they may be
37-
// needed for debugging or bug reporting.
38-
tempFolder = BaseNoGui.createTempFolder("console");
39-
DeleteFilesOnShutdown.add(tempFolder);
40-
try {
41-
String outFileName = PreferencesData.get("console.output.file");
42-
if (outFileName != null) {
43-
outFile = new File(tempFolder, outFileName);
44-
DeleteFilesOnShutdown.add(outFile);
45-
stdoutFile = new FileOutputStream(outFile);
46-
}
47-
48-
String errFileName = PreferencesData.get("console.error.file");
49-
if (errFileName != null) {
50-
errFile = new File(tempFolder, errFileName);
51-
DeleteFilesOnShutdown.add(errFile);
52-
stderrFile = new FileOutputStream(errFile);
53-
}
54-
} catch (IOException e) {
55-
Base.showWarning(_("Console Error"),
56-
_("A problem occurred while trying to open the\nfiles used to store the console output."),
57-
e);
58-
}
59-
consoleOut = new PrintStream(new EditorConsoleStream(false));
60-
consoleErr = new PrintStream(new EditorConsoleStream(true));
61-
6217
if (PreferencesData.getBoolean("console")) {
63-
try {
64-
System.setOut(consoleOut);
65-
System.setErr(consoleErr);
66-
} catch (Exception e) {
67-
e.printStackTrace(systemOut);
68-
}
18+
PrintStream consoleOut = new PrintStream(new EditorConsoleStream(false));
19+
PrintStream consoleErr = new PrintStream(new EditorConsoleStream(true));
20+
21+
System.setOut(consoleOut);
22+
System.setErr(consoleErr);
6923
}
7024
}
7125
}
7226

73-
/**
74-
* Close the streams so that the temporary files can be deleted.
75-
* <p/>
76-
* File.deleteOnExit() cannot be used because the stdout and stderr files are
77-
* inside a folder, and have to be deleted before the folder itself is
78-
* deleted, which can't be guaranteed when using the deleteOnExit() method.
79-
*/
80-
public static void quit() {
81-
// replace original streams to remove references to console's streams
82-
System.setOut(systemOut);
83-
System.setErr(systemErr);
84-
85-
// close the PrintStream
86-
IOUtils.closeQuietly(consoleOut);
87-
IOUtils.closeQuietly(consoleErr);
88-
89-
// also have to close the original FileOutputStream
90-
// otherwise it won't be shut down completely
91-
IOUtils.closeQuietly(stdoutFile);
92-
IOUtils.closeQuietly(stderrFile);
93-
94-
outFile.delete();
95-
errFile.delete();
96-
tempFolder.delete();
97-
}
98-
99-
final boolean err; // whether stderr or stdout
100-
PrintStream system;
101-
OutputStream file;
27+
private final boolean isStdErr; // whether stderr or stdout
28+
private final PrintStream system;
10229

103-
public EditorConsoleStream(boolean _err) {
104-
err = _err;
105-
if (err) {
30+
private EditorConsoleStream(boolean isStdErr) {
31+
this.isStdErr = isStdErr;
32+
if (this.isStdErr) {
10633
system = systemErr;
107-
file = stderrFile;
10834
} else {
10935
system = systemOut;
110-
file = stdoutFile;
11136
}
11237
}
11338

@@ -118,31 +43,22 @@ public void flush() {
11843
}
11944

12045
public void write(int b) {
121-
write(new byte[] { (byte) b });
46+
write(new byte[]{(byte) b});
12247
}
12348

12449
public void write(byte b[]) { // appears never to be used
12550
write(b, 0, b.length);
12651
}
12752

12853
public void write(byte b[], int offset, int length) {
129-
if (currentConsole != null)
130-
currentConsole.appendText(new String(b, offset, length), err);
54+
if (currentConsole != null) {
55+
currentConsole.appendText(new String(b, offset, length), isStdErr);
56+
}
13157

13258
system.write(b, offset, length);
133-
134-
if (file != null) {
135-
try {
136-
file.write(b, offset, length);
137-
file.flush();
138-
} catch (IOException e) {
139-
e.printStackTrace();
140-
file = null;
141-
}
142-
}
14359
}
14460

145-
static public void setCurrent(EditorConsole console) {
61+
public static void setCurrent(EditorConsole console) {
14662
currentConsole = console;
14763
}
14864

0 commit comments

Comments
 (0)